user4914499
user4914499

Reputation: 354

how to substitute slash in a file using sed

I am in use of bash shell.

I have a file filelist:

    cat filelist
    ../1.txt
    ../2.txt
    ...
    ../100.txt

I want to remove "../" and I tried $ cat filelist | sed s/..\/// , but it gives an error message. How can i remove the slash?

Upvotes: 0

Views: 91

Answers (4)

NeronLeVelu
NeronLeVelu

Reputation: 10039

remove starting ../ (i guess it's the purpose but not specified)

sed 's#^\.\./##' filelist
  • escaping the dot for avoiding regex meaning
  • changing default separator /by # to allow a readible / in path
  • adding ^ for limiting to starting path and not changing something like bad/../folder. If not the wanted behavior, just remove this caret.
  • direct use of file from sed (no need of cat with sed in this case)

Upvotes: 1

Karoly Horvath
Karoly Horvath

Reputation: 96258

You need quotes:

sed 's/..\///'

. means any character so you have to escape it too:

sed 's/\.\.\///'

and for readability you can use another character for the separator:

sed 's|\.\./||'

Upvotes: 1

beerbajay
beerbajay

Reputation: 20270

Use an alternate separator.

sed 's|^\.\./||g' filelist

You can also use the -i flag to edit the file in-place.

sed -i 's|^\.\./||g' filelist

Upvotes: 0

arco444
arco444

Reputation: 22821

You need quotes around the sed argument and also need to include the g global flag. It is also not necessary to cat the file first. You should also escape the periods.

Use:

sed 's/\.\.\///g' filelist 

Gives:

1.txt
2.txt
...
100.txt

Upvotes: 1

Related Questions