0

This question already has an answer here:

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.

marked as duplicate by Panther, karel, Fabby, Eric Carvalho, Charles Green Dec 28 '17 at 15:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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 0:38
  • 1
    Also, could you please explain what exactly you tried unsuccessfully and to what effect? – David Foerster Dec 30 '17 at 0:47
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"
  • 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

Not the answer you're looking for? Browse other questions tagged or ask your own question.