Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing new line after a particular text via bash/awk/sed/perl

Tags:

bash

sed

awk

I would like to remove all the newline character that occurs after a partiular string and replace it with a tab space. Say for instance my sample.txt is as follows

foo
bar bar bar bar some text

I would like it to be

foo    bar bar bar bar some text

How do I do this via bash/awk/sed. Do help.

like image 253
Greenhorn Avatar asked Dec 15 '11 11:12

Greenhorn


People also ask

How do I get rid of the new line in awk?

Use printf() when you want awk without printing newline AWK printf duplicates the printf C library function writing to screen/stdout.


3 Answers

In awk:

awk '/foo$/ { printf("%s\t", $0); next } 1'
like image 143
Michael J. Barber Avatar answered Oct 22 '22 05:10

Michael J. Barber


Here is how:

cat input.txt | sed ':a;N;$!ba;s/foo\n/foo\t/g'

More about why simple sed 's/foo\n/foo\t/g' does not work here: http://linuxtopia.org/online_books/linux_tool_guides/the_sed_faq/sedfaq5_009.html

like image 39
DejanLekic Avatar answered Oct 22 '22 06:10

DejanLekic


perl -pe 's/(?<=foo)\n/\t/' input
like image 5
codaddict Avatar answered Oct 22 '22 06:10

codaddict