-2

I have the following file :

/
/FUSE
//
Bank S.A / N.V
/F

I want to remove all forward slashes (/), so that the result looks like this:


FUSE

Bank S.A  N.V
F

How to do so using sed? Ive tried using tr, but tr is not what I need, because I want to edit the file in-place without printing it to console.

1
  • You appear to have deleted the last line in the example output. Is that a requirement of any solution here too?
    – Kusalananda
    May 10 at 15:21

3 Answers 3

4

You can use tr for this:

$ tr -d '/' < file

FUSE

Bank S.A  N.V
F

To save the output, just redirect to a new file:

tr -d '/' < file > file.fixed
2
  • 1
    An advantage of tr is that it works even for non-text files, while YMMV for sed (depending on the sed implementation). May 10 at 12:39
  • 1
    Note that / is not (thanfully!) special to the shell; it doesn't need to be quoted. May 10 at 12:39
3

Use a different separator than / into the sed expression, for example ::

sed 's:/::g' file

to remove all / irrespective of where they are on the line.

If you want to replace the existing file, use -i, meaning "inplace", which can be followed by an extension to backup the old file, for example this will keep a copy of the old file named file.old and modify file:

sed -i.old 's:/::g' file
0
0

You can also use sed this way:

sed --in-place 's/\///g' file

You just ask sed to substitute (s) pattern / with void space.

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