![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Conditionally joining lines in vi | ifermon | UNIX for Dummies Questions & Answers | 0 | 06-04-2008 06:43 AM |
| Joining lines from two files - please help | chandra004 | Shell Programming and Scripting | 25 | 07-26-2006 11:39 PM |
| Joining 2 lines in a file together | m223464 | Shell Programming and Scripting | 3 | 05-12-2005 08:42 AM |
| Joining multiple lines | beilstwh | Shell Programming and Scripting | 4 | 03-02-2005 01:51 AM |
| Joining lines in log file | bubba112557 | Shell Programming and Scripting | 3 | 05-18-2004 04:10 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
Joining 3 lines at a time
Hi,
I have a file which has the contents as below : 07:38:36 EST date 20041117 07:39:06 EST 07:00:29 EDT date 20050504 07:25:16 EDT 07:00:40 EDT date 20050505 07:23:12 EDT I need to delete the new line character from all lines except 3rd,6th,9th etc. lines so that i get the output as below : 07:38:36 EST date 20041117 07:39:06 EST 07:00:29 EDT date 20050504 07:25:16 EDT 07:00:40 EDT date 20050505 07:23:12 EDT How can I achieve this ?? I tried some combinations of sed which I know but no success ...... for eg: sed 'N;s/\n/ /' can join pairs of lines ... but not 3 lines at a time .. I am searching for some kind of one liner like that .. not a script .. Can anyone pls help ?? |
| Forum Sponsor | ||
|
|
|
|||
|
Hi,
This gives the result like this : 07:38:36 EST date 20041117 07:00:29 EDT date 20050504 07:00:40 EDT date 20050505 Not showing the last time .... I got it done through another way by appending a | to lines other than 3,6,9 etc and then using sed like this ... cat filename | awk '(NR%3!="0") {print $0,"|"} (NR%3=="0") {print $0}' t1 | sed -e :a -e '/|$/N; s/|\n//; ta' Not sure of any other better way to do this ... |
|
||||
|
That awk statment should be
Code:
awk '($NR+1) % 3 !=0 {printf $0; printf " "} NR % 3 == 0 {print " "}'
Code:
[/tmp]$ echo "07:38:36 EST
date 20041117
07:39:06 EST
07:00:29 EDT
date 20050504
07:25:16 EDT
07:00:40 EDT
date 20050505
07:23:12 EDT
" | awk '($NR+1) % 3 !=0 {printf $0; printf " "} NR % 3 == 0 {print " "}'
07:38:36 EST date 20041117 07:39:06 EST
07:00:29 EDT date 20050504 07:25:16 EDT
07:00:40 EDT date 20050505 07:23:12 EDT
|