0

I have a file like:

72810 82:28:1
this is text
hello world

72810 82:28:12
some more text
hello wolrd again

I need to replace the newline characters in just the lines containing 72810* But there's a catch: I have tried the following:

sed 's/\n/ /g' myfile.txt

But this doesn't work.

I have also tried:

cat myfile.txt | perl -p -e 's/72810.*\n/72810 /g'

But this doesn't save the full original text (72810 82:28:1 or 72810 82:28:12 in this case).

What do I need to do to end up with the file looking like:

72810 82:28:1 this is text
hello world

72810 82:28:12 some more text
hello world again

By the way, I'm using solaris.

wizurd
  • 3,319
  • 3
  • 31
  • 48
  • possible duplicate of [sed: How can I replace a newline (\n)?](http://stackoverflow.com/questions/1251999/sed-how-can-i-replace-a-newline-n) – Biffen May 23 '14 at 16:08
  • I have already searched for this specific question to no avail. This question involves being able to replace the newline on only specific matches – wizurd May 23 '14 at 18:02

3 Answers3

2

Try this:

perl -ne 'if (m/72810/) {s/\n/ /; print} else { print }' input.txt

Or this even shorter version:

perl -pe 's/\n/ / if m/72810/' input.txt

You can use -i option to edit that input.txt in place.

Lee Duhem
  • 14,412
  • 3
  • 28
  • 46
2

It seems simplest just to substitute the newline on all the lines that contain the test string. Like this

perl -pe "s/\n/ / if /72810/" myfile.txt

output

72810 82:28:1 this is text
hello world

72810 82:28:12 some more text
hello wolrd again
Borodin
  • 125,056
  • 9
  • 69
  • 143
0

This might work for you (GNU sed):

sed '/^72810/{N;s/\n/ /}' file
potong
  • 51,370
  • 6
  • 49
  • 80