Introduction to Unix and OS Module 3 Simple filters Dr. Girisha G S Dept. of CSE SoE,DSU , Bengaluru 1
Agenda Commonly used Filter commands pr h ead t ail c ut p aste s ort uniq tr 2
Filters Filters are the commands which accept data from standard input manipulate it and write the results to standard output What are Filers? - The filters can read data from standard input when used without a filename as argument , and from the file otherwise pr : paginating files - Displays the specified file s on the standard output in a paginated form. - pr command adds suitable headers, footers and formatted text. - pr adds five lines of margin at the top and bottom. The header shows the date and time of last modification of the file along with the filename and page number. Syntax: pr option(s) filename(s) 3
Example: Before using pr , here are the contents of a sample file named food : Options -k prints k (integer) columns -h “header” to have a header of user’s choice -d double spaces the output -n will number each line and helps in debugging $ 4
Let’s use pr options to make a two-column report with the header “Restaurants.” $ 5
head – displaying the beginning of the file - head command displays the top of the file. - It displays the first 10 lines of the file by default Option -n num : Prints the first ‘ num ’ lines instead of first 10 lines Example – 1 : consider the file having name state.txt contains all the names of the Indian states . Without any option, head displays only the first 10 lines of the file specified. syntax: head [ option] [filename]…[filename] $ cat state.txt Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh Goa Gujarat Haryana Himachal Pradesh Jammu and Kashmir Jharkhand Karnataka Kerala Madhya Pradesh Maharashtra Manipur Meghalaya Mizoram Nagaland Odisha Punjab Rajasthan Sikkim Tamil Nadu Telangana Tripura Uttar Pradesh Uttarakhand West Benga l $ head state.txt Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh Goa Gujarat Haryana Himachal Pradesh Jammu and Kashmir 6 10 lines
Example2: display first 5 lines of the file state.txt $ head -n 5 state.txt Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh 7 $ cat state.txt Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh Goa Gujarat Haryana Himachal Pradesh Jammu and Kashmir Jharkhand Karnataka Kerala Madhya Pradesh Maharashtra Manipur Meghalaya Mizoram Nagaland Odisha Punjab Rajasthan Sikkim Tamil Nadu Telangana Tripura Uttar Pradesh Uttarakhand West Benga l First 5 lines
tail – displaying the end of a file - tail command displays the end of the file. - It displays the last 10 lines of the file by default Option -n num : Prints the last ‘ num ’ lines instead of first 10 lines syntax: tail [ option] [filename]…[filename] Example – 1 : consider the file having name state.txt contains all the names of the Indian states . Without any option, tail displays only the last 10 lines of the file specified. $ tail state.txt Odisha Punjab Rajasthan Sikkim Tamil Nadu Telangana Tripura Uttar Pradesh Uttarakhand West Bengal 8 $ cat state.txt Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh Goa Gujarat Haryana Himachal Pradesh Jammu and Kashmir Jharkhand Karnataka Kerala Madhya Pradesh Maharashtra Manipur Meghalaya Mizoram Nagaland Odisha Punjab Rajasthan Sikkim Tamil Nadu Telangana Tripura Uttar Pradesh Uttarakhand West Benga l Last 10 lines
Example2: display last 3 lines of the file state.txt $ tail -n 3 state.txt Uttar Pradesh Uttarakhand West Bengal - With +n option tail command prints the data starting from specified line number of the file instead of end, where n represents the line no from where the selection should begin $ tail +25 state.txt Telangana Tripura Uttar Pradesh Uttarakhand West Bengal Example: display the data starting from 25 th line onwards 9 $ cat state.txt Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh Goa Gujarat Haryana Himachal Pradesh Jammu and Kashmir Jharkhand Karnataka Kerala Madhya Pradesh Maharashtra Manipur Meghalaya Mizoram Nagaland Odisha Punjab Rajasthan Sikkim Tamil Nadu Telangana Tripura Uttar Pradesh Uttarakhand West Benga l 25 th line onwards
cut - select parts of a line - cut command is used to cut fields or columns of the text from a file and display it to standard output Syntax: C ut [options] [file] options: -c cu t characters from a file -f cut the fields from a file -d used to specify the delimeter Example: Let's say you have a file named data.txt which contains the following text: $ cat data.txt one two three four five alpha beta gamma delta epsilon To "cut" only the third field of each line, use the command: $ cut -f 3 data.txt three gamma 10
Example 2: "cut " only the second-through-fourth field of each line $ cut -f 2-4 data.txt two three four beta gamma delta Example 3: output only the third-through- twelth character of every line of data.txt $ cut -c 3-12 data.txt e two thre pha beta g Unix Cut by delimiter Example : To display values from 2 nd column of file s martphones.tx t The tab character is default delimiter for cut command and "-f" option is used to cut by a delimiter. You can override delimiter by providing the "-d" option . $ cat smartphones.txt Model:Company:Price:Camera:4G IPhone4:Apple:1000$: Yes:Yes Galaxy:Samsung:900$: Yes:Yes Optimus:LG:800$: Yes:Yes Sensation:HTC:400$: Yes:Yes IPhone4S:Apple:1100:Yes:Yes N9:Nokia:400:Yes:Yes $ cut -d: -f2 smartphones.txt Company Apple Samsung LG HTC Apple Nokia 11
paste – pasting files - paste command will paste the content of the file side by side - Uses the tab as default delimiter Example: consider two files file1, file2 and the task is to merge lines of these two files. $ cat file1 unix l inux solaris $ cat file2 os s erver system $ paste file1 file2 u nix os l inux server s olaris system Syntax: paste [options] [file] 12
options -d specify a list of delimiters -s joins lines of a single file together Example: Merge file using delimiter $ paste -d “|” file1 file2 unix|os linux|server solaris|system Example: join lines in a file1 $ paste -s file1 u nix linux solaris 13