Posts tagged bash
bash one-liners: grep, sed, awk, find
Filed under Linux
#READ ARGS IN CMD LINE: for arg in $@ do printf $arg done #READ LINES OF A FILE while read line; do printf $line done < in.txt #FIND MIN, MAX, AVERAGE USING AWK awk 'NR == 1 { max=$1; min=$1; sum=0 } { if ($1>max) max=$1; if ($1<min) min=$1; sum+=$1;} END {if(NR>0) printf "%d,%d,%d", min, max, sum/NR}' #COUNT OF EACH UNIQ ITEM sort| uniq -c sed 's/:/ /g' grep -v 'patt' file #!/bin/sh for filename in /tmp/* do echo $filename done; awk '{ sum += $6 } END { print sum }' filename awk '{sub(/[ \t]+$/, "");print}' filename awk '/Dog/,/Cat/' filename awk '/virtual/{n++}; END {print n+0}' filename awk '{print FNR "\t" $0}' files* #Convert Windows/DOS newlines (CRLF) to Unix newlines (LF) from Unix awk '{ sub(/\r$/,""); print }' #Convert Unix newlines (LF) to Windows/DOS newlines (CRLF) from Unix awk '{ sub(/$/,"\r"); print }' #Print the sum of fields in every line awk '{ s = 0; for (i = 1; i <= NF; i++) s = s+$i; print s }' #Print the sum of fields in all lines awk '{ for (i = 1; i <= NF; i++) s = s+$i }; END { print s+0 }' sed -n '45,50p' filename # print line nos. 45-50 of a file #Calculate total size of a directory in Mb ls –al |awk '{total +=$5};END {print "Total size: " total/1024/1024 " Mb"}' #find examples find . -name '*OR*.xls' -exec du '{}' \; #Finding the Top 5 Big Files find . -type f -exec ls -s {} \; | sort -n -r | head -5 #Find all directories find . -type d #Find files bigger than the given size # find ~ -size +100M