0

I have a file that looks like this

line one
line two
line three
line four

and I want it to look like this (I want lines 2 and 3 merged into one)

line one
line two line three
line four

I've tried the following, that based on my research SHOULD do what I want:

$ sed '2s/\n/ /' test.txt > test2.txt

However test2.txt looks like this

line one
line two
line three
line four

I've seen some references to it being different on Solaris than Linux. Here's my server's details

$ uname -a
SunOS myserver 5.10 Generic_142909-17 sun4u sparc SUNW,Sun-Fire-V490

How can I make this give the results I want?

BIBD
  • 15,107
  • 25
  • 85
  • 137
  • See [this question](http://stackoverflow.com/questions/1251999/sed-how-can-i-replace-a-newline-n) which explains why it's a bit difficult with sed. – Tom Fenech Aug 12 '14 at 17:04
  • Do you want to merge the second line with the third, or the line with `line two` with `line three` regardless of position they have in the file? – Jotne Aug 13 '14 at 08:22
  • it was based upon line number, not the values of the line – BIBD Aug 13 '14 at 13:42

1 Answers1

3

Based on the answers in the linked question, this sed should work:

sed '2N;s/\n/ /' file

2N means on the second line, append the next line to the pattern space. This results in the pattern space containing line two\nline three. The substitution (which replaces the newline \n with a space) applies to every line in the file but only has an effect here.

Otherwise, this awk would do the trick:

awk 'NR==2{printf("%s ",$0);next}1' file

On line two, use printf to print the contents of the line, followed by a space. next skips to the next line. For all other lines, the 1 at the end is effectively a {print $0} block (which prints the line).

Alternatively:

awk '{printf("%s%s",$0,NR==2?OFS:ORS)}' file

Where OFS by default is a space and ORS is a newline

Or a bit of perl:

perl -pe 's/\n/ / if $. == 2' file

$. is the line number. Substitute the newline for a space on the 2nd line.

Output (using any of the above approaches on my system):

line one
line two line three
line four
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • I get 'awk: syntax error near line 1' 'awk: bailing out near line 1' – BIBD Aug 12 '14 at 17:04
  • @CodeSlave Since you are on solaris, use `/usr/xpg4/bin/awk` instead of regular `awk`. Also, the `sed` won't work as solaris `sed` does not recognize `\n` character like GNU and other variants do. – jaypal singh Aug 12 '14 at 17:19
  • @jaypal so would a `^M` (ctrl-v return) work on Solaris? – Tom Fenech Aug 12 '14 at 17:20
  • Ok, the sed and the perl examples work which is good enough for my purposes. – BIBD Aug 12 '14 at 17:21
  • I'm assuming the 2N; is indicating to work on line 2 and the next line, and my first try was just working on the line 2 (and not including the new line when I was trying to replace) – BIBD Aug 12 '14 at 17:22
  • @TomFenech No idea don't have a solaris box handy, but I remember adding a newline was a big pain and had to force an implicit newline (hitting enter). Replacing a newline might not be that difficult. UPDATE: and so it seems! – jaypal singh Aug 12 '14 at 17:22
  • 1
    @CodeSlave I have added to my explanation in the answer, hopefully that clarifies. – Tom Fenech Aug 12 '14 at 17:32
  • @TomFenech thanks for going above and beyond. I can only up-vote you once, so here's another on anotherone of your good answers as a bonus :-) – BIBD Aug 12 '14 at 17:42