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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
11-23-2012, 08:21 PM
#1
Member
Registered: Feb 2012
Posts: 57
Rep:
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?
11-23-2012, 09:03 PM
#2
Member
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Code:
awk '{ print $0 | "sort -n +2" }' input_file
but usually complicated things get easier in Perl.
11-23-2012, 10:55 PM
#3
Member
Registered: Feb 2012
Posts: 57
Original Poster
Rep:
Tried your solution linsaurus, and got the following error:
Quote:
sort: open failed: +2: No such file or directory
Any other suggestions?
11-23-2012, 11:08 PM
#4
Member
Registered: Feb 2012
Posts: 57
Original Poster
Rep:
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.
11-24-2012, 12:13 AM
#5
Member
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709
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.
11-24-2012, 07:07 AM
#6
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005
As an alternative you could also use the seat number as an array argument and output the data.
All times are GMT -5. The time now is 11:43 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News