How to replace newlines with tab characters?
Asked Answered
E

8

23

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.

Ecthyma answered 24/10, 2009 at 11:23 Comment(0)
H
13

not sure about output you want

# awk -vRS="\n" -vORS="\t" '1' file
hi      hello   hallo   greetings       salutations     no more hello for you 
Hadji answered 24/10, 2009 at 11:42 Comment(0)
J
52

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.

Jarrodjarrow answered 24/10, 2009 at 11:26 Comment(5)
@Nifle: Also no cat here, but who's counting? :)Jarrodjarrow
@Tele I'm a cat overuser, I admit that.Pollypollyanna
is there a way to do this in sed itself?Ecthyma
-1 as i already know that we can do it with tr but i need how to do it in sedEcthyma
@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.Jarrodjarrow
H
13

not sure about output you want

# awk -vRS="\n" -vORS="\t" '1' file
hi      hello   hallo   greetings       salutations     no more hello for you 
Hadji answered 24/10, 2009 at 11:42 Comment(0)
H
11
sed '$!{:a;N;s/\n/\t/;ta}' file
Hadji answered 24/3, 2010 at 12:29 Comment(4)
could you please also explain about the command you are using?Ecthyma
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 lineHadji
+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.Cattima
doesn't work i cshDorcasdorcea
H
7
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.

Harber answered 2/7, 2017 at 11:9 Comment(0)
H
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.

Hectocotylus answered 24/10, 2009 at 15:12 Comment(0)
F
0

Also if using Variables, Here Strings or Here Documents:

tr "\n" "\t" <<< "a
b
c
d
"
paste -s <<EOF
a
b
c
d
EOF
Forget answered 12/3, 2024 at 11:1 Comment(0)
C
0

Similar to the Text Processing challenge on HackerRank, here is the solution along with the description:

paste -sd'\t' data.txt
  • -s: lists the lines side by side.

  • -d'\t': sets the delimiter to a tab.


The short version of the command is:

paste -s data.txt

Note that tab is the default delimiter when using the -s option.

Chardin answered 20/7, 2024 at 12:37 Comment(0)
S
-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.

Smelt answered 12/1, 2018 at 1:57 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.