I would like to sort file in parts delimeted by string, but it is a larger part of processing in the awk.
I have read that it could be possible to store lines between delimeter and then use asort for sorting, but I need to sort it using the second column.
I have decided to store output between delimeter in a variable (k), then pass the value of variable into command using pipe |, and, at the end read the output, process it and print.
However, using the print variable|"sort -k2,2"|getline v
makes the getline(?) to stuck.
I can execute command in uglier way using system()
function, but then I think that I need to use echo to pass variable to command, and the output is not stored in variable v, but the error code.
$ awk 'BEGIN{ cmd="sort -k2,2"; k="1\tB\n2\tA"; v=system("echo \""k"\" | "cmd); print "OK: "v}'
2 A
1 B
OK: 0
The nicer way with print k|command
(removing |getline v
) executes command, but the print "OK"
is done before output from subcommand. The output is also not stored in variable v.
$ awk 'BEGIN{ cmd="sort -k2,2"; k="1\tB\n2\tA"; print k|cmd; print "OK: "v}'
OK:
2 A
1 B
How can I store command output in variable or at least to wait for the previous subcommand to finish before continuing?
sort -k2,2 inputfile
directly? because nothing you validate for the command result or anything in awk. what is your input and expected output? can you edit and show these instead? – αғsнιη Feb 17 at 12:31close(cmd)
statement to preventsort
from waiting for more input. See for example close() function – steeldriver Feb 17 at 12:46squeue
), and I don't need full information about the other users' jobs, and I want to collapse them, but I still need to leave my submitted jobs in right place to know, when they will be eligible for the run. – faramir Feb 17 at 20:55