2

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

Input file:

$ cat File1
NAME1
N1_info
NAME2
N2_info 

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
NAME1*
+N1_info
NAME2*
+N2_info

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 
  • 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. – dovah Jul 29 '14 at 15:40
4

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
  • Wow, that's incredible! It works... hopefully I could condense the real "N1_info" line into one only string (I'll split it later). – dovah 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). – dovah 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
2

With sed:

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

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.