0
votes

I use the following AWK command to sort the contents after the first 24 lines of a file:

awk 'NR <= 24; NR > 24 {print $0 | "sort -k3,3 -k4,4n"}' file > newFile

Now I want to join two files first (now simply discard the first 24 lines for both files) and then sort the merged file. Is there any way to do it without generating a temporary merged file?

1
FYI: the NR <= 24; is redundant; simply filtering with NR > 24 would only print the records you want.Jonathan Leffler
Thanks! Just curious, why awk 'NR > 24 {"sort -k3,3 -k4,4n"}' doesn't work at all? Seems that I have to include the first command (print $0) and "|" even when I don't need to print out the first 24 lines. @JonathanLefflerRunner
The {"sort..."} notation simply lists a string literal as 'the action'. There's no I/O redirection; no print; no assignment; no anything active -- so the statement is a no-op and the whole script is a no-op (the lines less than 25 are ignored and the lines greater than 24 are no-opped (is that a word? I guess it is, now), so the net result is the same as 'cat > /dev/null'.Jonathan Leffler

1 Answers

1
votes
awk 'FNR > 24' file1 file2 | sort -k3,3 -k4,4n > newFile

FNR is the file record number (resets to 1 for the first line of each file). If you insist on having the sort inside the awk script, you can use:

awk 'FNR > 24 { print $0 | "sort -k3,3 -k4,4n" }' file1 file2 > newFile

but I prefer the shell to do my piping.