Professional Documents
Culture Documents
cut –d: -f1-3 file2 Extracts fields 1 thru 3 from file2. The
fields are separated by the : character
who | cut –d” “ –f1 Lists the names of all users logged in.
Joining Vertical Data: paste Command
paste vertically joins 2 files together. A delimiter (-d option)
may be defined to separate fields. Default delimiter is tab. The
-s option joins lines of a single file together.
Examples: paste file1 file2 Displays file1 and file2 side by side.
rick
rick@att.com >>>> rick:rick@att.com:1234567890
1234567890
Displaying Files: head and tail Commands
head displays the top of a file. (1st 10 lines, by default).
tail displays the end of the file (last 10 lines, by default).
sort –t: -k 5.7 –r list Sort file list in reverse order on the
7th character of 5th field. Fields separated by :
sort –m file1 file2 Sorted files file1 and file2 are merged.
cut –d: -f3 list | sort –u Extract the 3rd field from list &
sort that field, removing the repeated lines.
Removing Duplicates: uniq Command
uniq displays a presorted file, removing all the duplicate lines from it.
If 2 files are specified, uniq reads from the first and writes to the second.
Examples: sort list | uniq – xlist Sorts file list; uniq reads from
stdin and writes the output to xlist.
uniq –c list Displays count of each unique line in the file list.
Character Manipulation: tr Command
tr translates characters from one format to another. Input always comes
from standard input. Arguments don’t include filenames. General form is:
tr options expression1 expression2 standard input
Examples: tr ‘+-’ ‘*/’ < math In the file math, replace all +’s with *’s and
replace all -’s with /’s.
head –n 3 list | tr ‘[a-z]’ ‘[A-Z]’ The 1st 3 lines
of the file list are translated to uppercase.
tr Command Options
Options: -d delete characters from the input stream
-s compress multiple consecutive characters (squeeze)
-c complementing value of expression
Examples: tr –d ‘/’ < dates Remove all /’s from the file dates.
tr –cd ‘:’ < file1 In the file file1, delete everything that isn’t
a colon (:). All that’s left is a file full of :’s
Finding Patterns in Files with grep
grep searches a file and displays the lines containing a pattern. Form is:
grep options pattern files
If more than 1 file is listed, the filename is also displayed in the output.
grep “.*” file1 Displays all lines in file1 that contain nothing or any # of chars
grep “[1-3]” file1 Displays all lines in file1 that contain a digit between 1 & 3.
grep “[^a-zA-Z]” file1 Displays all lines in file1 that contain a non-alphabetic
character
grep “^Rick$” file1 Displays all lines in file1 that contain only Rick
grep “^$” file1 Displays all lines in file1 that contain nothing
grep “R[aeiou]ck” file1 Displays all lines in file1 that contain Rack, Reck,
Rick, Rock or Ruck
Putting It All Together
An author wants to count the frequency of words used in a book chapter.