Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

How to remove BOM from UTF-8 using sed?

Answer:

# sed -e '1s/^\xef\xbb\xbf//' text.txt

Convert DOS newlines to Unix format

Answer:

To convert DOS newlines (CR/LF) to Unix (LF only), use sed, which can be found on most platforms.

sed "s/\r//" input.txt > output.txt

How to add line number to a file?

Answer:

The easiest way is to use the "sed" command

sed = test.txt | sed 'N; s/^/ /; s/ *\(.\{6,\}\)\n/\1 /'

Convert spaces into new lines using sed

Answer:

If you have a text file like:

1 2 3 4 5

How to convert the spaces into new lines?

Use sed!

# sed 's/ /\n/g' text.txt

How to replace multiple strings using sed?

Answer:

If you have multiple strings to replace, you can either using the "-e" option in sed, e.g.

sed -e 's/a/A/' -e 's/b/B/' < old.txt > new.txt

or, you can use a sed script file to store all the replace conditions

e.g. vi replace.sed

s/a/A/g
s/b/B/g
s/c/C/g

and execute like

sed -f replace.sed < old.txt > new.txt