Managing Text Files
Viewing file contents
Use less %99.9 of the time
less {file}
{command} | less
Display last 10 lines with tail
tail -n 10 {file}
Display first 10 lines with head
head -n 10 {file}
Display 10th line from the bottom with head and tail
tail -n 10 {file} | head -n 1
cut, sort, tr
Extract the first field delimited by ’:’ from a file
cut -d ':' -f 1 /etc/passwd
Extract the first field delimited by ’:’ from a file and sort
cut -d ':' -f 1 /etc/passwd | sort
Sort numerically (-n) based on the third field/key (-k3) delimited by ’:’ (-t :)
sort -t : -k3n /etc/passwd
Translate tr lowercase to uppercase
echo hello | tr [:lower:][:upper:]
Using grep
Show matches and include 5 lines before
ps faux | grep -B5 bash
Show matches and include 5 lines after
ps faux | grep -A5 bash
Show matches and include 5 lines before and after
ps faux | grep -C5 bash
Search in all files in a directory
grep cjordan /etc/*
Search in all files in a directory and output files, not lines
grep -l cjordan /etc/*
Case insensitive search in all files in a directory
grep -i cjordan /etc/*
Recursive search and follow all symbolic links
grep -R cjordan /etc
Recursive search and follow all symbolic links only if they are on the command line
grep -r cjordan /etc
Prefix results with line number
grep -n cjordan /etc/*
Applying Regular Expressions
Regex man page
man 7 regex
^beginning of the line:grep '^|' myfile$end of the line:grep 'anna$' myfile\bend of word:grep '^lea\b' myfilewill find lines starting with lea, but not with leanne.one character:grep '^.$' myfilewill find lines with one of any character*zero or more times:grep 'n.*x' myfilewill match any amount of characters between n and x+one or more times (extended):grep -E 'bi+t' myfilewill match bit, biit, biiit, etc, but not bt?zero or one time (extended):grep -E 'bi?t' myfilewill match bt or bit, but not biit\n{3\}n occurs 3 times:grep 'bon\{3\}nen' myfilewill match bonnnnen- string must be a word:
grep '\banna\b' myfile - match on either option (extended):
grep -E 'foo|bar' myfile
Exploring awk
Extract the fourth field delimited by a colon
awk -F : '{ print $4 }' /etc/passwd
Extract the fourth field delimited by a colon of a line that contains cjordan
awk -F : '/cjordan/ { print $4 }' /etc/passwd
Extract the last field delimited by a colon that contains cjordan
awk -F : '/cjordan/ { print $NF }' /etc/passwd
Using sed
Print the 5th line
sed -n 5p {file}
Search and replace
sed 's|anna|otto|g' {file}
Search and replace modifying file in place
sed -i 's|anna|otto|g' {file}
Delete second line from file
sed -i -e '2d' {file}
Insert line after match (a\ append)
sed -i "/^foo/a\bar" {file}
Insert line before match (i\ insert)
sed -i "/^foo/i\bar" {file}
Lab Exercise
- First, create a file, ~/stuff with the following contents:
foo
bar
bing
bang
sally
susan
jeremy
- Use
headandtailto display the 5th line of the file /etc/passwd - Use
sedto display the 5th line of the file /etc/passwd - Use
sedto replace ‘foo’ with ‘fooski’ in ~/stuff. Only output the results to stdout. - Use
sedto insert ‘bob’ on a separate line above ‘bing’ in ~/stuff. Modify the file in place - Use
sedto insert ‘joe’ on a separate line below ‘bob’ in ~/stuff. Modify the file in place - Use
awkto filter the last column out ofps aux - Use
grepto show the names of all files in /etc that have lines that contain ‘root’ as a word - Use
grepto show all lines from all files in /etc that contain exactly 3 characters
Solution
head -n 5 /etc/passwd | tail -n 1sed -n 5p /etc/passwdsed "s|foo|fooski| stuffsed -i "/bing/i\bob" stuffsed -i "/bob/a\joe" stuffps aus | awk '{print $NF}'grep -rwl root /etcgrep -rx '...' /etc
- Clint Jordan