I have pattern like below
hi
hello
hallo
greetings
salutations
no more hello for you
I am trying to replace all newlines with tab using the following command
sed -e "s_/\n_/\t_g"
but it's not working.
Could anybody please help? I'm looking for a solution in sed/awk.
You are almost there with your sed script, you'd just need to change it to:
The
\
is enough for escape, you don't need to add_
And you need to add the/
beforeg
at the end to let sed know that this is the last part of the script.Here
tr
is better, I think:As Nifle suggested in a comment,
newlines
here is the name of the file holding the original text.Because
sed
is so line-oriented, it's more complicated to use in a case like this.You can't replace newlines on a line-by-line basis with
sed
. You have to accumulate lines and replace the newlines between them.This
sed
script accumulates all the lines and eliminates all the newlines but the last:By the way, your
sed
scriptsed -e "s_/\n_/\t_g"
is trying to say "replace all slashes followed by newlines with slashes followed by tabs". The underscores are taking on the role of delimiters for thes
command so that slashes can be more easily used as characters for searching and replacing.not sure about output you want
-s Concatenate all of the lines of each separate input file in command line order. The newline character of every line except the last line in each input file is replaced with the tab character, unless otherwise specified by the -d option.