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
 
LinkBack Search this Thread
Old 07-15-2011, 06: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, 06:33 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,514
Blog Entries: 1

Rep: Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139
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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are 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 11:34 AM
[SOLVED] awk - How to get a block of lines webhope Programming 37 04-25-2010 06:51 AM
Delete lines using awk kkjegan Programming 13 09-11-2007 07:36 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
awk text that is on several lines homey Programming 2 10-31-2004 09:27 AM


All times are GMT -5. The time now is 03:12 AM.

Main Menu
 
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
Open Source Consulting | Domain Registration