LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 07-15-2011, 07:27 AM   #1
sysadm1n
LQ Newbie
 
Registered: Sep 2009
Posts: 1

Rep: Reputation: 0
awk group 2 lines


Hello,

I have a file with n number of lines where every 2 lines are grouped. So I want to print the items in a line and the next line in a single line.

For example:
[~]# cat temp
aaa bbb ccc
123 145
fgdfg fg fgdf
34 232

I can print the line and the adjacent together and print the required items like this:
[~]# cat temp|awk '{print $1":"$3;getline;print $2}'
aaa:ccc
145
fgdfg:fgdf
232

But that's not the order I want. How can I get it printed like below:
aaa:145:ccc
fgdfg:232:fgdf
 
Old 07-15-2011, 07:33 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

This should work:
Code:
awk '{ one = $1 ; three = $3 ; getline ; print one ":" $2 ":" three }' infile
Hope this helps.
 
1 members found this post helpful.
  


Reply

Tags
awk


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why does AWK split my lines into two? keenboy Linux - General 1 08-05-2010 12:34 PM
[SOLVED] awk - How to get a block of lines webhope Programming 37 04-25-2010 07:51 AM
Delete lines using awk kkjegan Programming 13 09-11-2007 08:36 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 10:19 AM
awk text that is on several lines homey Programming 2 10-31-2004 10:27 AM


All times are GMT -5. The time now is 10:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration