4 ms·
If you're just looking for a single column, then as zamfi mentioned, cut does the job: $ paste <(printf "a\nb\nc\n") <(printf "1\n2\n3\n") | cut -f2 1 2
by quicklime 5y ago
If you're just looking for a single column, then as zamfi mentioned, cut does the job:
$ paste <(printf "a\nb\nc\n") <(printf "1\n2\n3\n") | cut -f2
1
2
3
It makes sense that "cut" and "paste" would be inverse operations, but too bad that GUIs repurposed these words for clipboard operations.
But awk can do both, and does so in a single pass:
$ paste <(printf "a\nb\nc\n") <(printf "1\n2\n3\n") | awk '{ print $1 > "letters"; print $2 > "numbers" }'
$ cat letters
a
b
c
$ cat numbers
1
2
3
- sillysaurusx 5y agoWhat! I didn’t know awk could do that within single quotes. The heck, it implements a subset of sh? Thanks!
- camhutch 5y agoawk has some basic redirection features. gawk documentation: https://web.mit.edu/gnu/doc/html/gawk_6.html#SEC41 https://web.mit.edu/gnu/doc/html/gawk_6.html#SEC41 posix documentation: https://pubs.opengroup.org/onlinepubs/009695299/utilities/awk.html#tag_04_06_13_10 https://pubs.opengroup.org/onlinepubs/009695299/utilities/aw...
- stewartbutler 5y agoKinda-sorta related, you can pipe to a multivariable loop: paste <(printf "a\nb\nc\n") <(printf "1\n2\n3\n") | while read x y; do echo "$y -- $x"; done Kinda silly for this example, but very useful in things like `kubectl` pipelines -- you can do something like this: kubectl get pods --all-namespaces | awk '{print $1 " " $2}' | grep "doesntexist" | while read x y; do kubectl delete pod $y --namespace $x --wait=false; done This lets you list all pods in a cluster, grep a selection of them by name, and delete them in a batch, regardless of which namespace they are in, while shuffling the order of args into different flags.
- sillysaurusx 5y agoMost useful comment I’ve read in months, for me. Cheers!
- forty 5y agoSorry, but my shellcheck OCD forces me to say "read without -r will mangle backslashes." ;)
- CornCobs 5y agoTIL you can pipe into files in awk. I've never come across this feature somehow. Is it commonly used?