LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-23-2012, 08:21 PM   #1
sarenace
Member
 
Registered: Feb 2012
Posts: 57

Rep: Reputation: Disabled
Calling sort from within awk on multiline input


Hello,
I have a file with records that I'm filtering with the awk command

Quote:
awk '/PokerStars/,/\n\n\n/{if(/^Seat/)print; else if (/^$/)print}'
to give output like this:

Quote:
Seat 1 amkolino (13821 in chips)
Seat 2 Hed1ng (11044 in chips)
Seat 3 potiss... (5048 in chips)
Seat 4 [KPACABKA77] (8954 in chips)
Seat 5 Rob1991315 (10390 in chips)
Seat 6 remajr (25462 in chips)
Seat 7 mikelong11 (6286 in chips)
Seat 8 sarenace (14492 in chips)
Seat 9 nikoo-36 (12462 in chips)
Seat 1 amkolino (big blind) folded before Flop
Seat 2 Hed1ng folded before Flop (didn't bet)
Seat 3 potiss... folded before Flop (didn't bet)
Seat 4 [KPACABKA77] showed [As 7s] and won (20108) with two pair, Aces and Sevens
Seat 5 Rob1991315 folded before Flop (didn't bet)
Seat 6 remajr folded before Flop (didn't bet)
Seat 7 mikelong11 folded before Flop (didn't bet)
Seat 8 sarenace (button) showed [Ks Kc] and lost with a pair of Kings
Seat 9 nikoo-36 (small blind) folded before Flop



Seat 1 amkolino (11996 in chips)
Seat 2 Hed1ng (9619 in chips)
Seat 3 potiss... (4823 in chips)
Seat 4 [KPACABKA77] (22908 in chips)
Seat 5 Rob1991315 (10165 in chips)
Seat 6 remajr (25237 in chips)
Seat 7 mikelong11 (6061 in chips)
Seat 8 sarenace (5413 in chips)
Seat 9 nikoo-36 (11737 in chips)
Seat 1 amkolino (button) folded before Flop (didn't bet)
Seat 2 Hed1ng (small blind) folded before Flop
Seat 3 potiss... (big blind) showed [Kh 7d] and lost with high card Ace
Seat 4 [KPACABKA77] folded before Flop (didn't bet)
Seat 5 Rob1991315 folded before Flop (didn't bet)
Seat 6 remajr folded before Flop (didn't bet)
Seat 7 mikelong11 folded before Flop (didn't bet)
Seat 8 sarenace showed [6s 3s] and lost with high card Ace
Seat 9 nikoo-36 showed [Ts Ks] and won (16999) with a pair of Tens
What I want to do is sort each block of text by the seat number, i.e., the ouput I want should read:

Quote:
Seat 1 amkolino (13821 in chips)
Seat 1 amkolino (big blind) folded before Flop
Seat 2 Hed1ng (11044 in chips)
Seat 2 Hed1ng folded before Flop (didn't bet)
Seat 3 potiss... (5048 in chips)
Seat 3 potiss... folded before Flop (didn't bet)
Seat 4 [KPACABKA77] (8954 in chips)
Seat 4 [KPACABKA77] showed [As 7s] and won (20108) with two pair, Aces and Sevens
Seat 5 Rob1991315 (10390 in chips)
Seat 5 Rob1991315 folded before Flop (didn't bet)
Seat 6 remajr (25462 in chips)
Seat 6 remajr folded before Flop (didn't bet)
Seat 7 mikelong11 (6286 in chips)
Seat 7 mikelong11 folded before Flop (didn't bet)
Seat 8 sarenace (14492 in chips)
Seat 8 sarenace (button) showed [Ks Kc] and lost with a pair of Kings
Seat 9 nikoo-36 (12462 in chips)
Seat 9 nikoo-36 (small blind) folded before Flop



Seat 1 amkolino (11996 in chips)
Seat 1 amkolino (button) folded before Flop (didn't bet)
Seat 2 Hed1ng (9619 in chips)
Seat 2 Hed1ng (small blind) folded before Flop
Seat 3 potiss... (4823 in chips)
Seat 3 potiss... (big blind) showed [Kh 7d] and lost with high card Ace
Seat 4 [KPACABKA77] (22908 in chips)
Seat 4 [KPACABKA77] folded before Flop (didn't bet)
Seat 5 Rob1991315 (10165 in chips)
Seat 5 Rob1991315 folded before Flop (didn't bet)
Seat 6 remajr (25237 in chips)
Seat 6 remajr folded before Flop (didn't bet)
Seat 7 mikelong11 (6061 in chips)
Seat 7 mikelong11 folded before Flop (didn't bet)
Seat 8 sarenace (5413 in chips)
Seat 8 sarenace showed [6s 3s] and lost with high card Ace
Seat 9 nikoo-36 (11737 in chips)
Seat 9 nikoo-36 showed [Ts Ks] and won (16999) with a pair of Tens
I tried to achieve this by calling the sort command from within awk:

Quote:
awk '/PokerStars/,/\n\n\n/{if(/^Seat/){
close("sort -k2,n")
print
}else if(/^$/)print}'
However this makes no change to the output. Would anyone know whats going on?
 
Old 11-23-2012, 09:03 PM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk '{ print $0 | "sort -n +2" }' input_file
but usually complicated things get easier in Perl.
 
Old 11-23-2012, 10:55 PM   #3
sarenace
Member
 
Registered: Feb 2012
Posts: 57

Original Poster
Rep: Reputation: Disabled
Tried your solution linsaurus, and got the following error:

Quote:
sort: open failed: +2: No such file or directory
Any other suggestions?
 
Old 11-23-2012, 11:08 PM   #4
sarenace
Member
 
Registered: Feb 2012
Posts: 57

Original Poster
Rep: Reputation: Disabled
Just and addition, I managed to alter the above awk command so that it didnt return an error message:

awk 'BEGIN{RS=ORS="\n\n\n"}{print $0 |"sort -k2"}'

Unfortunately, the output looked like this:

Quote:






Seat 1 amkolino (11996 in chips)
Seat 1 amkolino (13821 in chips)
Seat 1 amkolino (big blind) folded before Flop
Seat 1 amkolino (button) folded before Flop (didn't bet)
Seat 2 Hed1ng (11044 in chips)
Seat 2 Hed1ng (9619 in chips)
Seat 2 Hed1ng (small blind) folded before Flop
Seat 2 Hed1ng folded before Flop (didn't bet)
Seat 3 potiss... (4823 in chips)
Seat 3 potiss... (5048 in chips)
Seat 3 potiss... (big blind) showed [Kh 7d] and lost with high card Ace
Seat 3 potiss... folded before Flop (didn't bet)
Seat 4 [KPACABKA77] (22908 in chips)
Seat 4 [KPACABKA77] (8954 in chips)
Seat 4 [KPACABKA77] folded before Flop (didn't bet)
Seat 4 [KPACABKA77] showed [As 7s] and won (20108) with two pair, Aces and Sevens
Seat 5 Rob1991315 (10165 in chips)
Seat 5 Rob1991315 (10390 in chips)
Seat 5 Rob1991315 folded before Flop (didn't bet)
Seat 5 Rob1991315 folded before Flop (didn't bet)
Seat 6 remajr (25237 in chips)
Seat 6 remajr (25462 in chips)
Seat 6 remajr folded before Flop (didn't bet)
Seat 6 remajr folded before Flop (didn't bet)
Seat 7 mikelong11 (6061 in chips)
Seat 7 mikelong11 (6286 in chips)
Seat 7 mikelong11 folded before Flop (didn't bet)
Seat 7 mikelong11 folded before Flop (didn't bet)
Seat 8 sarenace (14492 in chips)
Seat 8 sarenace (5413 in chips)
Seat 8 sarenace (button) showed [Ks Kc] and lost with a pair of Kings
Seat 8 sarenace showed [6s 3s] and lost with high card Ace
Seat 9 nikoo-36 (11737 in chips)
Seat 9 nikoo-36 (12462 in chips)
Seat 9 nikoo-36 (small blind) folded before Flop
Seat 9 nikoo-36 showed [Ts Ks] and won (16999) with a pair of Tens
The idea is that only one block of text is sorted, then it sorts the next block of text and so on.
 
Old 11-24-2012, 12:13 AM   #5
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

You need to close pipe explicitly:
Code:
$ awk '{s="sort -k2"; print|s; close(s) }' RS="\n\n\n"  infile
Seat 1 amkolino (13821 in chips)
Seat 1 amkolino (big blind) folded before Flop
Seat 2 Hed1ng (11044 in chips)
Seat 2 Hed1ng folded before Flop (didn't bet)
Seat 3 potiss... (5048 in chips)
Seat 3 potiss... folded before Flop (didn't bet)
Seat 4 [KPACABKA77] (8954 in chips)
Seat 4 [KPACABKA77] showed [As 7s] and won (20108) with two pair, Aces and Sevens
Seat 5 Rob1991315 (10390 in chips)
Seat 5 Rob1991315 folded before Flop (didn't bet)
Seat 6 remajr (25462 in chips)
Seat 6 remajr folded before Flop (didn't bet)
Seat 7 mikelong11 (6286 in chips)
Seat 7 mikelong11 folded before Flop (didn't bet)
Seat 8 sarenace (14492 in chips)
Seat 8 sarenace (button) showed [Ks Kc] and lost with a pair of Kings
Seat 9 nikoo-36 (12462 in chips)
Seat 9 nikoo-36 (small blind) folded before Flop


Seat 1 amkolino (11996 in chips)
Seat 1 amkolino (button) folded before Flop (didn't bet)
Seat 2 Hed1ng (9619 in chips)
Seat 2 Hed1ng (small blind) folded before Flop
Seat 3 potiss... (4823 in chips)
Seat 3 potiss... (big blind) showed [Kh 7d] and lost with high card Ace
Seat 4 [KPACABKA77] (22908 in chips)
Seat 4 [KPACABKA77] folded before Flop (didn't bet)
Seat 5 Rob1991315 (10165 in chips)
Seat 5 Rob1991315 folded before Flop (didn't bet)
Seat 6 remajr (25237 in chips)
Seat 6 remajr folded before Flop (didn't bet)
Seat 7 mikelong11 (6061 in chips)
Seat 7 mikelong11 folded before Flop (didn't bet)
Seat 8 sarenace (5413 in chips)
Seat 8 sarenace showed [6s 3s] and lost with high card Ace
Seat 9 nikoo-36 (11737 in chips)
Seat 9 nikoo-36 showed [Ts Ks] and won (16999) with a pair of Tens
 
1 members found this post helpful.
Old 11-24-2012, 07:07 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
As an alternative you could also use the seat number as an array argument and output the data.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
....How to use awk to sort.... nisson Linux - General 12 11-02-2012 03:51 PM
Extarct tags with multiline values from XML file using sed/Awk gbms Linux - Newbie 3 03-27-2012 10:18 AM
[SOLVED] Awk & multiline webhope Programming 9 05-24-2010 03:03 AM
Awk sort problem Zmyrgel Programming 2 09-30-2009 02:39 AM
Issues calling sort from perl using pipe as delimiter amytys Programming 1 10-20-2004 09:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:43 AM.

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