0% found this document useful (0 votes)
41 views

Textprocessingutilities Awk Command: Used To Print The Output Based On Our Requirement

The document discusses various Linux text processing utilities including awk, diff, comm, tr, tee, tail, head, sort, nl, uniq, cut, paste, join, grep, fgrep, and egrep. It provides the syntax and examples of how to use each utility, often showing the expected output. The utilities can be used to select, compare, modify, arrange, and search text in files.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Textprocessingutilities Awk Command: Used To Print The Output Based On Our Requirement

The document discusses various Linux text processing utilities including awk, diff, comm, tr, tee, tail, head, sort, nl, uniq, cut, paste, join, grep, fgrep, and egrep. It provides the syntax and examples of how to use each utility, often showing the expected output. The utilities can be used to select, compare, modify, arrange, and search text in files.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

TEXTPROCESSINGUTILITIES

Awk command: Used to print the output based on our requirement.

Syntax: awk options’selection criteria {action}’ file(s)


$N: Here, N denotes the field number.
NR: It denotes the line number

Initial Input:

Example 1: $awk ‘{print $2,$3}’ marks

Task: Displays 2nd field and 3rd field

Output :

Example 2: $awk ‘$2==”Neeraj”{print $2,$4}’ marks

Task: Displays 2nd field and 4thfield of lines whose 2nd field is Neeraj

Output:

Example 3: $awk ‘NR==3,NR==6{print $1,$2,$3}’ marks

Task: Display 1st, 2nd and 3rd field of rows 3 to 6.

Output:

Example 4: $awk ‘/Neeraj/ {print}’ marks


Task: prints the lines containing Neeraj

Output:

diff command: It displays difference between 2 files and the changes needed to be done to
make both the files same.

Syntax: $difffile1 file2

Example1: test1 and test2 are completely different

Output:

Explanation: m,ncp,q: change m to n lines in file1 and p to q lines in file2

Example2: test1 has 1 additional line compared to test2.

Output:

Explanation: md0: delete mth column in 1st file

Example 3: diff test2 test1

Output:
Explanation: 0am: Add mth column in file2.

Comm command: Compares 2 sorted files and displays the result in output.

Syntax: $comm [options] file1 file2

Example 1: comm test1 test2

Output:

Explanation: First column contains names only in file1, 2nd column contains names only
in file2 and 3rd column contains names common to both file 1 and file 2.

Example 2: comm test1 test2 //test1 and test2 are same.

Output:

Cmp command: Compares 2 files and writes the result to standard output

Syntax: $cmp[options file1 file2

Example1: cmp test1 test2

Output:

Example2: cmp –c test1 test2

Output:
tr command: It takes as parameters two sets of characters or strings, and replaces
occurrences of the characters in the first set with the corresponding elements from
the other set.
Syntax: tr options exp1 exp2 <filename //filename is optional
Example1: tr ‘e’ ‘a’ <marks
Output:

Example2: tr ‘a-z’ ‘A-Z’ <filename

Output:

Example 3: tr –d ‘ee’<filename

Output:

Explanation: Deletes specified characters

Example 4: tr –cd ‘ee’<<filename

Output:

Explanation: Displays specifies characters removing all other characters.


tee command:Displays the output of the command and copies or appends it into
another file.

Syntax: command|tee [options] filename

Example 1: cat test1|tee test2

Output:

Explanation: Output of cat test1 is displayed and that output is copied into
test2.
Example 2: cat test1|tee –a test2
Output:

Explanation: Output of cat test1 is displayed and it is appended to file test2.

Tail: To display last 10 lines of the file.

Syntax: tail [options][file]

Example: tail Neeraj


Output:

Head: Displays first 10 lines of the file.

Syntax: head [options][file]

Example: head Neeraj

Output:

Sort:Sorts the file

i) Sort by lines: Syntax: sort filename

Example: sort Neeraj Output:


ii) Sort in reverse order: Syntax:sort –r filename

Example:sort –r Neeraj

Output:

nl:For numberingthe lines

Syntax: nl filename

Example: nl Neeraj

Output:

uniq: Displays unique lines of given files

Syntax: uniq [OPTIONS] filename

Example1: uniq Neeraj

Output:
Example2: uniq-D Neeraj

Output: It displays only duplicate lines.

Example 3: uniq –u Neeraj

Output: It doesn’t print even a single copy of duplicate data

cut:Extract the required files or columns from file

Syntax: cut [options] filename

Example1: cut –f1,3 neeraj

Output:Prints field of 1st and 3rd of each line from the file.
Example2: cut –d”c” –f1,3 neeraj

Output: Prints field of 1st and 3rd of each line from the file with c as
delimiter.

Example3: cut –c3-6 neeraj

Output:prints characters from third to sixth of each line from the file.

Paste: Create new file by gluying together fields or columns from 2 or more files.

Syntax: paste filename1>filename2

Example: paste neeraj>n Output:


join: Joins lines of 2 files based on a common field.

Syntax: join -1N -2N file1 file 2

Example: join -11 -21 student.txt marks.txt

Output:

grep: Used to display the lines where entered pattern is there.

Syntax: grep [options] pattern filename

Example1: grep 90 marks

Output:

Example2: grep –c Neeraj marks

Output: Prints the number of lines containing the given word Neeraj

Example3: grep –v Neeraj marks

Output: Prints lines except the lines the lines containing Neeraj
Example4:grep –n Neeraj marks

Output:Prints the lines containing Neeraj along with line numbers

Example5: grep –i neeraj marks

Output: Ignores the case while matching the string

fgrep: To search 2 or more strings simultaneously

Syntax: fgrep pattern filename

Example: grep ‘1

>2

>3’ marks

Output:

egrep: To search for 2 or more strings simultaneous but in a single line only.

Syntax: egrep pattern filename

Example: egrep ‘1|2|3’ marks

Output:

You might also like