Understanding the Power of Pipeline in Unix: Examples and Functions
Table of Contents
Pipeline (Unix)
Reading
- https://softwareengineering.stackexchange.com/questions/304878/what-is-the-exact-ingenuity-of-unix-pipe/304881
- https://unix.stackexchange.com/questions/301426/bash-function-that-accepts-input-from-parameter-or-pipe/301432
- https://en.wikipedia.org/wiki/Pipeline_(Unix)
- https://www.gnu.org/software/bash/manual/html_node/Process-Substitution.html
Examples
Functions
nato (Parameter)
export lookup="a:alfa b:bravo c:charlie d:delta e:echo f:foxtrot g:golf h:hotel i:india j:juliett k:kilo l:lima" function nato() { input=$1 for C in `echo $input | sed 's/./& /g'` do # lookup for L in $lookup do k=$(echo $L | cut -d ':' -f 1) v=$(echo $L | cut -d ':' -f 2) # echo $c $k $v if [ "x$k" == "x$C" ] then echo $v continue fi done done | tr '\n' ' ' } nato glad
echo glad | nato | nato | nato
function nato() { input=$1 echo $input | sed -e 's/a/alpha/g; s/b/bravo/g; s/c/charlie/g; s/d/delta/g; s/e/echo/g; s/f/foxtrot/g; s/g/golf/g; s/h/hotel/g; s/i/india/g; s/j/juliett/g; s/k/kilo/g; s/l/lima/g; s/m/mike/g; s/n/november/g; s/o/oscar/g; s/p/papa/g; s/q/quebec/g; s/r/romeo/g; s/s/sierra/g; s/t/tango/g; s/u/uniform/g; s/v/victor/g; s/w/whiskey/g; s/x/xray/g; s/y/yankee/g; s/z/zulu/g; s/0/zero/g; s/1/one/g; s/2/two/g; s/3/three/g; s/4/four/g; s/5/five/g; s/6/six/g; s/7/seven/g; s/8/eight/g; s/9/nine/g;' } nato 'a'
xer (Pipemill)
xer () { while read -r line; do echo ${line}x; done } echo a | xer | xer | xer
type -a xer
/dev/stdin: ASCII text
w | file -
dd if=/dev/stdin of=/dev/stdout
Process Substitution
mkdir {a,b}
for C in `jot 10`; do echo $C > a/$C.txt ; done
for C in `jot 11`; do echo $C > b/$C.txt ; done
diff <(ls a) <(ls b)
standard output is a terminal -- ignoring
time sleep 2 | gzip
time sleep 2 | gzip > timed.gz
time sleep 2 | gzip | cat
time sleep 2 | gzip | tee timed.gz | gunzip
Pipeline
cat /etc/passwd | grep -vE '^#' | cut -d : -f 1 | grep ^_ | tee example.txt | sort | uniq -c
cat- file, stdin, domain socketgrep- searches any given input filescut- cuts out selected portions of each linetee- copies standard input to standard outputsort- sorts text and binary files by linesuniq- reads the specified inputfile comparing adjacent lines, and writes a copy of each unique input line to the outputfile
Wikipedia
curl -s "https://en.wikipedia.org/wiki/Pipeline_(Unix)" \ | sed 's/[^a-zA-Z ]/ /g' \ | tr 'A-Z ' 'a-z\n' \ | grep '[a-z]' \ | sort -u \ | comm -23 - <(sort /usr/share/dict/words) \ | head -n 20
echo Non-pipe
ls > files.txt cat files
ls > files.txt
cat files.txt | echo