VinAlencc
VinAlencc

Reputation: 41

How to use sed to remove 1 or 2 dots and 1 slash?

I am creating one script with is going to read a TXT file (which has many directory paths inside of it) and remove the beginning of each directory path. But, each directory path can start with ./ or ../. How can I use SED to remove the beginning?

I know I can use 2 command lines, one to remove ./ and another to remove ../, but how can I remove both patterns in one command line?

Upvotes: 2

Views: 1491

Answers (2)

Cyrus
Cyrus

Reputation: 88829

I suggest with GNU sed:

echo '../foo' | sed -E 's|^\.{1,2}/||'

Output:

foo

I switched from s/// to s||| to avoid escaping /.

Upvotes: 5

James Brown
James Brown

Reputation: 37464

Another. Using ^ to denote the beginning of string and \? to make latter . optional:

$ echo file
./foo
../bar
./period_ending./baz
$ sed 's/^\.\.\?\///g' file
foo
bar
period_ending./baz

Upvotes: 0

Related Questions