问题
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.
回答1:
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.
回答2:
sed 'N;s/\n/ /' ip.txt
Nadd next line to pattern spaces/\n/ /change first newline character to space (or whatever is the separator)- Usage of
\nmight depend onsedimplementation. This was tested onGNU 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-n2two lines together as argument (space is used to combine them)
回答3:
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'".
来源:https://stackoverflow.com/questions/45412782/how-do-i-remove-the-line-break-from-alternate-lines-in-an-ascii-file