Unix System Basics | Codio

sed

Delete all blank lines.

sed '/^$/d' myfile.txt 

From a specific line to the end of the file.

sed '/So I tried/,$d' myfile.txt

Delete everything after a specific line and save it “in place”.

sed -i '1,/So I tried/!d' myfile.txt

awk

Using awk to count the number of occurrences of the word “Pooh” in the text

awk '/Pooh/{x++}END{print x}' myfile.txt

Print the number of words in a file.

awk '{ total = total + NF }; END {print total}' myfile.txt 

Print the number of lines in a file.

awk 'END{print NR}' myfile.txt

Parse the comma-separated text and output two fields from each line. Don’t print the header line.

 awk -F"," '{if (NR!=1){ print $1 $2 $3 }}' data.csv

Print out just the second column of the file.

awk  -F"," '{print $2}' data.csv 

printf

Print the result of a math equation.

printf "%d\n" $((8+4))

Print only the first 4 digits beyond the decimal point of a floating point number.

printf "%.4f\n" 3.1415926535

dd

This command copies the contents of trydd.txt to outdd.txt using the conversion ucase which turned it all to upper case characters.

dd if=trydd.txt of=outdd.txt conv=ucase

while

Create a foreground job that prints out numbers to the console in a forever loop.

COUNTER=0; while true; do printf $COUNTER; sleep 1; let COUNTER++; done