0

I grab the contents of a directory with du -a /test1 | cut -f2 and get this:

/test1/file1
/test1/directory1
/test1/directory1/file2

My hope is to use something like cut, sed, or grep to say "whatever $(pwd) is, chop that off the front of the output."

For instance, if /test1/ contained another directory named 'test1', I'd want to retain that sub-directory in my output, so if I had:

/test1/test1/

my output would ideally be

test1/

Thank you for your help.

EDIT: I have tried the tips outlined in Use sed on a string variable rather than a file. I can get output similar to what my example looks for, but it either takes out more than one instance of /test1 per line or it only operates on the first line of the variable. It's not acting the same as "For the beginning of each line, remove $(pwd), but only once per-line"

The use sed on a string answer is only working on the very first line of my multi-line variable. The topic of multi-line variables are not addressed in the answer given. Their solution works because the PATH variable doesn't contain newlines. In my case, it only works on the first line of the variable which is not the desired output.

wjandrea
  • 14,236
  • 4
  • 48
  • 98
  • This is easy to implement in pure Bash. LMK when the question is reopened and I'll post an answer. I voted to reopen it myself. – wjandrea Dec 29 '17 at 21:51
  • 1
    Why do you use du to use directories? One would normally use either shell globbing or find for that. Why don't you simply change the working directory to the "prefix" that you want to remove? – David Foerster Dec 30 '17 at 00:38
  • 1
    Also, could you please explain what exactly you tried unsuccessfully and to what effect? – David Foerster Dec 30 '17 at 00:47

1 Answers1

0

Read man bash, especially the part about "Parameter Expansion" and do something like:

a="/a/b/c"
echo "$a"
b="${a%/*}"
echo "$b"
c="${b%/*}"
echo "$c"
waltinator
  • 36,399
  • that only works on the first line of my multi-line variable, I wondered if I was passing the changes back wrongly, but when I try different things I get bad substitution errors – Volumetricsteve Dec 28 '17 at 21:09