21

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.

6 Answers 6

51

tr is better here, I think:

tr "\n" "\t" < newlines 

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.

5
  • 1
    @Nifle: Also no cat here, but who's counting? :)
    – Telemachus
    Oct 24, 2009 at 11:28
  • @Tele I'm a cat overuser, I admit that.
    – Nifle
    Oct 24, 2009 at 11:30
  • is there a way to do this in sed itself?
    – Vijay
    Oct 24, 2009 at 11:31
  • -1 as i already know that we can do it with tr but i need how to do it in sed
    – Vijay
    Oct 24, 2009 at 11:34
  • 4
    @John: someone may be able to tell you how to do it in sed, but given that sed is so line-oriented, that's a bit like hammering in a screw.
    – Telemachus
    Oct 24, 2009 at 11:49
13

not sure about output you want

# awk -vRS="\n" -vORS="\t" '1' file
hi      hello   hallo   greetings       salutations     no more hello for you 
11
sed '$!{:a;N;s/\n/\t/;ta}' file
4
  • 4
    could you please also explain about the command you are using?
    – Vijay
    Mar 24, 2010 at 12:34
  • if its not the last line , get the next line join with pattern space, substitute the newline and then branch to :a and do the same for every line
    – ghostdog74
    Mar 29, 2010 at 4:39
  • +1! This worked for me, even when the file only had one line then a new, empty line. Most of the other solutions negatively surprised me. I don't like such surprises. Aug 1, 2021 at 13:10
  • doesn't work i csh
    – CervEd
    Sep 26, 2021 at 10:57
6

You can't replace newlines on a line-by-line basis with sed. You have to accumulate lines and replace the newlines between them.

text abc\n    <- can't replace this one

text abc\ntext def\n    <- you can replace the one after "abc" but not the one at the end

This sed script accumulates all the lines and eliminates all the newlines but the last:

sed -n '1{x;d};${H;x;s/\n/\t/g;p};{H}'

By the way, your sed script sed -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 the s command so that slashes can be more easily used as characters for searching and replacing.

6
paste -s

-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.

-2

You are almost there with your sed script, you'd just need to change it to:

sed -e "s/\n/\t/g"

The \ is enough for escape, you don't need to add _ And you need to add the / before g at the end to let sed know that this is the last part of the script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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