Jimmix
Jimmix

Reputation: 6536

SH CLI sed remove all characters besides path from listing

There is a file that has results of rsync files listing:

drwxrwxrwx          4,096 2018/12/10 15:27:39 test/dir/one
drwxrwxrwx          4,096 2018/12/10 15:27:39 best/folder/two

how to use sed to get rid of everything besides paths?

wanted result:

test/dir/one
best/folder/two

I tried this regex: that works as it should for finding preceding of paths as base for sed but it did not have any effect when used:

cat listing.txt | sed 's/.*[0-9]+:[0-9]+:[0-9]+ //' | less

What am I missing?

Upvotes: 2

Views: 56

Answers (2)

Ed Morton
Ed Morton

Reputation: 204311

This will work using any POSIX sed even if your file names contain blanks:

$ sed 's/\([^ ]* *\)\{4\}//' file
test/dir/one
best/folder/two

or any POSIX awk:

$ awk '{sub(/([^ ]* *){4}/,"")}1' file
test/dir/one
best/folder/two

If your file names can contain newlines then we should talk....

Upvotes: 0

tripleee
tripleee

Reputation: 189749

Your sed probably doesn't support the + repetition operator in this form. Try

sed 's/.*[0-9]\+:[0-9]\+:[0-9]\+ //' listing.txt

(which also does away with that pesky useless cat).

Recall that sed predates many of the frills of modern regex. Your sed might support an -r or -E flag to enable extended regex support (whioh is still far from the modern regex dialect many newcomers are most familiar with) but this is not portable.

Of course, if the listing uses a fixed field width, maybe simply try

cut -c47- listing.txt

(Not in a place where I can verify the precise number - play around with different values.)

Upvotes: 2

Related Questions