LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Calling sort from within awk on multiline input (https://www.linuxquestions.org/questions/programming-9/calling-sort-from-within-awk-on-multiline-input-4175438497/)

sarenace 11-23-2012 08:21 PM

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?

linosaurusroot 11-23-2012 09:03 PM

Code:

awk '{ print $0 | "sort -n +2" }' input_file
but usually complicated things get easier in Perl.

sarenace 11-23-2012 10:55 PM

Tried your solution linsaurus, and got the following error:

Quote:

sort: open failed: +2: No such file or directory
Any other suggestions?

sarenace 11-23-2012 11:08 PM

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.

firstfire 11-24-2012 12:13 AM

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


grail 11-24-2012 07:07 AM

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 04:10 AM.