Home COMSC-171 <- Prev Next ->

Text Utilities

first or last lines

head /etc/passwd # first ten lines head -n5 /etc/passwd # first five lines tail /etc/passwd # last ten lines tail -n5 /etc/passwd # last five lines

word wrap

head /etc/services | fold -w20 # wrap lines at width 20 columns head /etc/services | fold -w20 -s # wrap lines at width 20 columns, on spaces

extract columns

tail -n25 /etc/passwd | cut -f1 -d: # output 1st field, delimiter : tail -n25 /etc/passwd | cut -f1,3-7 -d: # output all except 2nd field, in order tail -n25 /etc/passwd | cut -c1-6 # output 1st through 6th characters

duplicates

tail -n21 /etc/passwd | cut -c1-6 | uniq # remove consecutive duplicate lines tail -n21 /etc/passwd | cut -c1-6 | uniq -c # count unique lines tail -n21 /etc/passwd | cut -c1-6 | uniq -d # show only duplicate lines

combine columns

tail -n25 /etc/passwd | cut -f1 -d: > users # user names tail -n25 /etc/passwd | cut -f7 -d: > shells # login programs paste shells users # default delimiter Tab paste -d_ shells users # delimiter _ ls -d /* ls -d /* | paste - - # each - reads from STDIN

extract lines which match a regular expression

head -n25 /etc/passwd | grep 'user' # output lines containing: user head -n25 /etc/passwd | grep -i 'user' # case insensitive head -n25 /etc/passwd | grep -C 1 'spool' # context (before & after) 1 line head -n25 /etc/passwd | grep -B 2 'spool' # context (before) 2 lines head -n25 /etc/passwd | grep -A 2 'spool' # context (after) 2 lines head -n25 /etc/passwd | grep '[0-9][0-9]' # two consecutive numerals head -n25 /etc/passwd | grep '[0-9].*[0-9]' # two numerals anywhere head -n25 /etc/passwd | grep ':$' # ending with : head -n25 /etc/passwd | grep -v ':$' # not ending with : head -n25 /etc/passwd | grep '^s' # (literal ^ and s) beginning with: s head -n25 /etc/passwd | grep '^[a-m]' # beginning with a character in set head -n25 /etc/passwd | grep '^[^a-m]' # beginning with a character not in set head -n25 /etc/passwd | grep '^[^:][^:][^:]:' # first field three characters head -n25 /etc/passwd | grep -E '^[^:]{3}:' # extended regexp, multiplier head -n25 /etc/passwd | grep -E '^([^:]*:){3}[0-9]{2}:' # () group, 2-digit GID head -n25 /etc/passwd | grep '[[:upper:]]' # character class, only inside [ ]

sort

head /etc/passwd | sort # output lines sorted in ASCII order head /etc/passwd | sort -r # reverse order head /etc/passwd | sort -t: -k4 # sort from 4th field to end, delimiter : # default delimiter is space-to-nonspace transition head /etc/passwd | sort -t: -k4n # numeric (integer) head /etc/passwd | sort -t: -k4g # general numeric (also floating point) head /etc/passwd | sort -t: -k4,4n -k1r # 4th field only, then 1st field head /etc/passwd | sort -k1.4 # from 4th char of line (1st and only field) head /etc/passwd | sort -k1.4 -d # dictionary order (ignores punctuation) head -n20 /etc/passwd | sort -t: -k5 # look for uppercase letters head -n20 /etc/passwd | sort -t: -k5f # case insensitive (fold lower to upper)

translate one set of characters to another

tail -n25 /etc/passwd | tr 0: 9, # translate all 0 to 9 and all : to , tail -n25 /etc/passwd | tr '[:lower:]' '[:upper:]' # character classes tail -n25 /etc/passwd | tr -s : # squeeze : (remove consecutive duplicates) tail -n25 /etc/passwd | tr -d 9 # delete 9 tail -n25 /etc/passwd | tr -d '\r' # delete CR (no visible change)