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 
3
  • My sed did not behave honoring your post. Please consider editing. Commented Jul 29, 2014 at 15:18
  • Try sed $'/^NAME/{N;s/\\n/\t/;}' Commented Jul 29, 2014 at 15:27
  • Sorry @galegosimpatico, I fixed it. Commented Jul 29, 2014 at 15:40

2 Answers 2

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
4
  • Wow, that's incredible! It works... hopefully I could condense the real "N1_info" line into one only string (I'll split it later). Commented Jul 29, 2014 at 15:49
  • Show us what you really have to work with. We can come up with something easy. Commented Jul 29, 2014 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). Commented Jul 29, 2014 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. Commented Jul 29, 2014 at 16:46
2

With sed:

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.