Skip to content

Instantly share code, notes, and snippets.

@babarot
Created May 31, 2014 10:19
Show Gist options
  • Save babarot/38346c78cf4779079a4b to your computer and use it in GitHub Desktop.
Save babarot/38346c78cf4779079a4b to your computer and use it in GitHub Desktop.
Behave like the GNU tair -r command or tac command
#!/usr/local/bin/gawk -f
BEGIN {
# Set delimiter as \034, avoid conflict with other characters
sort_exe = "sort -t \"\034\" -nr"
}
{
# Pass to the child process
printf("%d\034%s\n", NR, $0) |& sort_exe;
}
END {
# Close the input pipe only
close(sort_exe, "to");
# Read output of the child process by using getline
while ((sort_exe |& getline var) > 0) {
split(var, arr, /\034/);
print arr[2];
}
close(sort_exe);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment