The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-09-2005
Registered User
 

Join Date: Jul 2005
Location: Chennai
Posts: 39
Stumble this Post!
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 ??
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 12-09-2005
mona's Avatar
Registered User
 

Join Date: Nov 2005
Location: Singapore
Posts: 96
Stumble this Post!
I think the below awk command will help you

Code:
awk 'NR % 3 !=0 {printf $0;printf " "} NR % 3 ==0 {print " "}' join_three_lines.dat
Reply With Quote
  #3 (permalink)  
Old 12-09-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,658
Stumble this Post!
And a sed solution

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
" | sed -e 'N;N;s/\n/ /g'
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
Reply With Quote
  #4 (permalink)  
Old 12-09-2005
Registered User
 

Join Date: Jul 2005
Location: Chennai
Posts: 39
Stumble this Post!
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 ...
Reply With Quote
  #5 (permalink)  
Old 12-09-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,658
Stumble this Post!
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
Reply With Quote
  #6 (permalink)  
Old 12-09-2005
mona's Avatar
Registered User
 

Join Date: Nov 2005
Location: Singapore
Posts: 96
Stumble this Post!
Sorry. This will give you the expected result

awk '{printf $0;printf " "}NR % 3 ==0 {print " "}' join_three_lines.dat
Reply With Quote
  #7 (permalink)  
Old 12-09-2005
Registered User
 

Join Date: Jul 2005
Location: Chennai
Posts: 39
Stumble this Post!
Great Vino .. that worked fine ... One more doubt .... can u tell me whats the meaning of N in this sed command and how does that work here ?
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:46 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0