Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • 4
    I like this, but it requires a bit of explanation - the pipe is inside the awk script. How does that work? Is it calling the sort command externally? Does anyone know of at least a link to a page explaining pipe use within awk? Commented Nov 7, 2015 at 1:24
  • @Wildcard you can check the official manual page or this primer. Commented Nov 2, 2016 at 19:52
  • This code fails when I use these arguments to sort: sort -n -k 2b,2 -t $'\t'. The problem is nesting '\t' inside 'NR...{print...}'. The explanation of how to escape the 's is here Commented Mar 28, 2020 at 17:30
  • For fixed-width output, use the -b option, as it will make sort ignore leading blanks in the sort key. The default field separator is non-blank-to-blank transitions, so fields will start with leading blanks. For example, this command lists installed Python packages first by location, then by package name: pip list -v | awk 'NR <= 2; NR > 2 { print $0 | "sort -b -k 3,3 -k 1,1" };' Commented May 13, 2021 at 16:38
  • 1
    Note, pipes inside awk may need to be followed by close("sort --exact-args...") to prevent buffering from printing this after later prints. Commented Dec 29, 2021 at 18:31