interview-prep

These come up constantly in scripting and interview questions:

grep "pattern" file.txt              # find lines matching pattern
grep -r "pattern" /path              # recursive search
grep -v "pattern"                    # invert (lines NOT matching)
grep -i "pattern"                    # case-insensitive
grep -E "regex"                      # extended regex
grep -c "pattern"                    # count matches

awk '{print $2}'                     # print second field
awk -F: '{print $1}' /etc/passwd     # custom field separator
awk '$3 > 100 {print $1}'            # conditional print

sed 's/old/new/g' file               # substitute
sed -i 's/old/new/g' file            # in-place edit (Linux)

sort file | uniq -c | sort -rn       # count and rank unique lines
cut -d',' -f1,3 file.csv             # cut columns from CSV
wc -l file                           # line count

head -n 20 file                      # first 20 lines
tail -n 20 file                      # last 20 lines
tail -f file                         # follow (live updates)

My notes