3

I have an ASCII file that contains data as follows

    459999XXX9998882      WTD CCD           3,500.00-         0.00         3,500.00-         0.00       0.00
       09/07/17  23:00:07   999999999999 000000000000 EONCO024 GTUR BR              AND PARDES  APIN
    459999XXX9998883      WTD CCD             400.00-         0.00           400.00-         0.00       0.00
       09/07/17  23:00:12   999999999999 000000000000 EOMCO015 P.G ROAD, ANNA HOT SECU  TLIN
    29999XXX99988829      INQ CCD               0.00          0.00             0.00          0.00       0.00
       09/07/17  23:00:41   999999999999 000000000000 EOMCO010 PUNA KUMBHARIA ROAD,   SUR        GJIN
    5459999XXX999888   INQ SAV               0.00          0.00             0.00          0.00       0.00    UNABLE TO PROC
       09/07/17  23:00:44   999999999999 000000000000 EONCO089 CTS NO 3985/5 ST STAND SOL      MHIN

I would like lines containing the dates to be contactenated to the line above it. Essenatilly I would like to remove all the line breaks from odd numbered lines. So the result is something like this

459999XXX9998882      WTD CCD           3,500.00-         0.00         3,500.00-         0.00       0.00 09/07/17  23:00:07   999999999999 000000000000 EONCO024 GTUR BR              AND PARDES  APIN
459999XXX9998883      WTD CCD             400.00-         0.00           400.00-         0.00       0.00 09/07/17  23:00:12   999999999999 000000000000 EOMCO015 P.G ROAD, ANNA HOT SECU  TLIN
29999XXX99988829      INQ CCD               0.00          0.00             0.00          0.00       0.00 09/07/17  23:00:41   999999999999 000000000000 EOMCO010 PUNA KUMBHARIA ROAD,   SUR        GJIN
5459999XXX999888      INQ SAV               0.00          0.00             0.00          0.00       0.00    UNABLE TO PROC 09/07/17  23:00:44   999999999999 000000000000 EONCO089 CTS NO 3985/5 ST STAND SOL      MHIN

I tried the solution provided here but then it removed all the linebreaks resulting in one single line! Would appreciate any ideas.

  • Possible duplicate of [How to merge every two lines into one from the command line?](https://stackoverflow.com/questions/9605232/how-to-merge-every-two-lines-into-one-from-the-command-line) – Thor Oct 29 '18 at 22:23

3 Answers3

3
sed 'N;s/\n/ /' ip.txt 
  • N add next line to pattern space
  • s/\n/ / change first newline character to space (or whatever is the separator)
  • Usage of \n might depend on sed implementation. This was tested on GNU sed

With perl, change newline character if line number is odd

perl -pe 's/\n/ / if $.%2' ip.txt


Can also use

xargs -d'\n' -n2 -a ip.txt
  • -d'\n' newline character as delimiter
  • -n2 two lines together as argument (space is used to combine them)
Sundeep
  • 23,246
  • 2
  • 28
  • 103
2

Print each line, alternating between a tab and a newline as the Output Record Separator ORS:

awk '{ ORS = NR % 2 ? "\t" : "\n" } 1' file

NR is the current record (line) number, so NR % 2 is true on odd-numbered lines.

Obviously, the tab "\t" can be changed to whatever you want to put between the lines that you're joining together.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • This command adds a tab to every 3rd, 6th , 9th rows. It seems to be indenting these lines with the TAB. – sridhar pandurangiah Jul 31 '17 at 10:56
  • I don't see why it would do anything every 3 rows - if there are already tabs at the start of your even-numbered lines, then you should change `"\t"` to `""`. – Tom Fenech Jul 31 '17 at 10:59
0

You can use command

awk 'NR % 2 == 1 { printf $0; getline; print $0 }' filename

Which means "for every odd line print it without '\n', get next line, print it with '\n'".

Binpord
  • 90
  • 8