Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I'd like to replace all the newlines after a line containing a given pattern with a tab.

Input file:

$ cat File1

I'm creating some flags at the ends of the future "tab"s:

sed '/^NAME/s/$/\*/g; /^NAME/!{s/^/+/g}' File1.txt > File2.txt

$ cat File2

Then I'm removing the characters between the "flags" in order to obtain the wished output. I tried two sed patterns (but none of them change my 'File2'):

head File2 | sed -e 's/\(\*\).*\(+\)/\1\t\2/g'
head File2 | sed -n '/\*/,/+/p'

This is the what the output should look like:

$ cat File3
NAME1   N1_info
NAME2   N2_info 
share|improve this question
My sed did not behave honoring your post. Please consider editing. – uprego Jul 29 '14 at 15:18
Try sed $'/^NAME/{N;s/\\n/\t/;}' – Stéphane Chazelas Jul 29 '14 at 15:27
Sorry @galegosimpatico, I fixed it. – Mark_R Jul 29 '14 at 15:40

2 Answers 2

up vote 4 down vote accepted

If this is your actual data, then

paste - - < File1 > File3

is all you need. paste uses a tab as a delimiter by default.

If "N1_info" is actually more than one line, then this isn't your solution. I'd do:

perl -0777 -pe 's/\*\n\+/\t/g' File2 > File3
share|improve this answer
Wow, that's incredible! It works... hopefully I could condense the real "N1_info" line into one only string (I'll split it later). – Mark_R Jul 29 '14 at 15:49
Show us what you really have to work with. We can come up with something easy. – glenn jackman Jul 29 '14 at 15:56
NAMEn is actually a gene identifier and Nn_info are a list of genome coordinates, basically a very long list of strings (but I could process it to a single string before applying the solution you provided). By the way, your method works great (much better than my silly idea). – Mark_R Jul 29 '14 at 16:10
My thought was that you should not have to pre-process your data at all: thus Stephane and Gnouc have given you better answers. – glenn jackman Jul 29 '14 at 16:46

With sed:

$ sed '/^NAME/{$!N;s/\n/\t/}' file
NAME1   N1_info
NAME2   N2_info
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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