Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace newlines with tab characters?

Tags:

shell

unix

sed

awk

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.

like image 717
Vijay Avatar asked Oct 24 '09 11:10

Vijay


People also ask

How do you replace a new line character in Java?

Line Break: A line break (“\n”) is a single character that defines the line change. In order to replace all line breaks from strings replace() function can be used.


2 Answers

Here tr is better, 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.

like image 97
Telemachus Avatar answered Sep 25 '22 15:09

Telemachus


not sure about output you want

# awk -vRS="\n" -vORS="\t" '1' file
hi      hello   hallo   greetings       salutations     no more hello for you 
like image 34
ghostdog74 Avatar answered Sep 25 '22 15:09

ghostdog74