Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 497C – Introduction to UNIX Lecture 24: - Simple Filters Chin-Chih Chang

Similar presentations


Presentation on theme: "CS 497C – Introduction to UNIX Lecture 24: - Simple Filters Chin-Chih Chang"— Presentation transcript:

1 CS 497C – Introduction to UNIX Lecture 24: - Simple Filters Chin-Chih Chang chang@cs.twsu.edu chang@cs.twsu.edu

2 comm: What is Common? comm shows the lines that are common, and optionally shows you the lines unique to either or both files. When you run comm, it displays a three- columnar output. The first column contains lines unique to the first file, and the second column shows lines unique to the second file. The third column displays common lines to both files.

3 comm: What is Common? To drop a particular column, simply use its column number with the - sign. comm –3 foo[12] selects lines not command to both files. comm -12 foo1 foo2 selects lines common to both files.

4 head: Displaying the Beginning of a File The head command displays the top of the file. It displays the first 10 lines when used without an option. To display the first N lines, use – symbol followed by N. head -3 group To open last modified file for editing, use: vi `ls -t | head -1`

5 tail: Displaying the End of a File The tail command displays the end of the file. It displays the last 10 lines when used without arguments. To display last three lines, use: $ tail -3 file Tail can also be used with a line number (with the +k option) to start extraction from a specific line. tail +801 foo

6 tail: Displaying the End of a File To output the last 512 bytes, use tail -512c foo You can also use tail to monitor the growth of a file with the -f option. tail -f install.log

7 cut: Slitting a File Vertically The cut command is used to slice a file vertically. To extract the columns, use the -c (column) option followed by the column numbers: cut -c1-4 /etc/group cut -c -3,6-22,55- foo 1-4 indicates column 1 to 4. -3 indicates column 1 to 3. 55- indicates column 55 to the end of line.

8 cut: Slitting a File Vertically To cut fields, use the -d option for the delimiter and -f (field) option for specifying the field list. cut uses the tab as the default delimiter. This is how you cut out the first and third fields of the /etc/group file: cut -d: -f1,3 /etc/group cut can be used to extract the first word of a line by specifying the space as the delimiter. who | cut -d ‘ ‘-f1

9 paste: Pasting Files What you cut with cut can be pasted back with the paste command. paste also uses the –d option to specify the delimiter, which by default is also the tab. We can use paste to literally join the two files calc.lst and result.lst. paste -d= calc.lst result.lst paste is also a filter. We can use bc and paste in a pipeline: bc < calc.lst | paste –d= calc.lst -


Download ppt "CS 497C – Introduction to UNIX Lecture 24: - Simple Filters Chin-Chih Chang"

Similar presentations


Ads by Google