SSF
SSF

Reputation: 983

Using sed to delete a pattern from the line

I have a list.txt file that contains a list of file paths:

/old/file/path/directory
/old/file/path/mydirectory

I am trying to do the following:

/path/directory
/path/mydirectory

to do this, I am using sed:

OLDPATH=/old/file/


sed "\|$OLDPATH|d" list.txt > newlist.txt

However this deletes the line completely. How do I delete the part I want only using sed and the variable $OLDPATH?

Upvotes: 0

Views: 179

Answers (2)

Vytenis Bivainis
Vytenis Bivainis

Reputation: 2376

If all your lines start with OLDPATH, you can use

cut -c $(echo -n "$OLDPATH" | wc -c)-

or better yet:

cut -c "$((${#OLDPATH}+1))"-

Upvotes: 0

fedorqui
fedorqui

Reputation: 289495

Instead, you can use this syntax:

OLDPATH="/old/file"
sed "s#$OLDPATH##" list.txt > newlist.txt

This looks for $OLDPATH and removes it, whereas your |d removes the lines containing the pattern given.

With your given input data it returns:

/path/directory
/path/mydirectory

Upvotes: 1

Related Questions