0

I have a file with broken lines as follows,

C12321 net12415431 net41432143 +
 1.079879E-17
C12322 net2135 net648641 +
 3.4659E-17

I want to create a file from this file which has lines as follows

C12321 net12415431 net41432143 1.079879E-17
C12322 net2135 net648641 3.4659E-17

I need to do this using sed or awk with in a couple of lines.

regards, Asif

Ed Morton
  • 188,023
  • 17
  • 78
  • 185

4 Answers4

1

sed is an excellent tool for simple substitutions on a single line but for any other text manipulation just use awk - the solution will be clearer, more easily extensible, more robust, more portable, and in many cases briefer (not that that matters much).

Read the file and remove all occurrences of " +\n" (space-plus-newline):

$ cat file
C12321 net12415431 net41432143 +
 1.079879E-17
C12322 net2135 net648641 +
 3.4659E-17

$ awk -v RS= '{gsub(/ \+\n/,""); print}' file
C12321 net12415431 net41432143 1.079879E-17
C12322 net2135 net648641 3.4659E-17

There are many alternatives, some briefer, some using less memory, etc. this is just the clearest IMHO.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
0

You can do it like this:

$ sed ':a;N;$!ba;s/+\n/ /g' your_file

it will replace all + followed by a new line with empty string.

In case you want the file to be updated with this replacement, add the -i parameter:

$ sed -i ':a;N;$!ba;s/+\n/ /g' your_file

The explanation of the command can be found in https://stackoverflow.com/a/1252191/1983854 :

Update: explanation.

  1. create a label via :a
  2. append the current and next line to the pattern space via N
  3. if we are before the last line, branch to the created label $!ba ($! means not to do it on the last line (as there should be one final newline)).
  4. finally the substitution replaces every newline with a space on the pattern space (which is the whole file).
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
0

A longer but more readable alternative to a single sed command:

tr '+' '\\' < tmp.txt | while read line; do printf "%s\n" "$line"; done

This just replaces + with a line continuation character that the shell built-in read understands.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • This only replaced + with nothing. It didn't join the truncated lines in to single line. By the way what king of line truncation is +\n. – asif iqbal May 10 '13 at 14:25
  • `tr '+' '\' < tmp.txt` should simply replace each '+' with a literal backslash. The `read` command (without the `-r` option usually recommended) sees the literal "\" and continues reading until the next unescaped newline. – chepner May 10 '13 at 15:05
  • 1
    don't you need to escape the backslash in the tr so it doesn't look like escape-tick? You also need to add the variable REPLY to the read command. It's also worth mentioning that by not setting `IFS=` as usually recommended you're stripping off leading white space which in this case is desirable. – Ed Morton May 10 '13 at 22:44
  • I didn't realize `REPLY` as a default was a `bash`-specific extension to `read`. As for the backslash, *everything* is treated literally inside single quotes, so no escaping is necessary (or possible). That's a good point about `IFS`. – chepner May 11 '13 at 00:30
  • bash on cygwin: `echo "a+b" | tr '+' '\'` outputs `tr: warning: an unescaped backslash at end of string is not portable` and then `a\b` while `echo "a+b" | tr '+' '\\'` outputs `a\b`. – Ed Morton May 11 '13 at 15:58
  • Ah, OK. It seems different versions of `tr` will handle an unescaped backslash different. – chepner May 12 '13 at 15:07
0

This might work for you (GNU sed):

sed '$!N;s/ +\n//;P;D' file
potong
  • 55,640
  • 6
  • 51
  • 83