SlideShare a Scribd company logo
3
Most read
6
Most read
7
Most read
Introduction to Unix and OS
Module 3
sort, uniq, tr, grep and sed
Dr. Girisha G S
Dept. of CSE
SoE,DSU, Bengaluru
1
Agenda
- sort
- uniq
- Tr
- grep
- sed
2
3
sort – ordering a file
- sort command sorts the contents of the text file line by line
Syntax:
sort [options] [file]
options
-n sort numerically
-r reverse the order of the sort
-t char uses delimiter character to identify fields
-k n sort on the nth field
-k m,n starts sort on the m filed & ends sort on nth filed
-u removes repeated line
-o flname places output in the file flname
4
Example: consider the following file
$ cat file1.txt
Zimbabwe
Serbia
Norway
Australia
• Sort the file file1.txt
$ sort file1.txt
Australia
Norway
Serbia
Zimbabwe
• Numeric sorting
Example: sort the file marks.txt
$ cat marks.txt
22
33
11
77
55
$ sort -n marks.txt
11
22
33
55
77
5
Example : Sort a colon delimited text file on second field
$ cat names.txt
Alex Jason:200:Sales
Emma Thomas:100:Marketing
Madison Randy:300:Product Development
Nisha Singh:500:Sales
Sanjay Gupta:400:Support
$ sort -t: -k 2 names.txt
Emma Thomas:100:Marketing
Alex Jason:200:Sales
Madison Randy:300:Product Development
Sanjay Gupta:400:Support
Nisha Singh:500:Sales
6
uniq command – locate repeated and non repeated lines
- uniq command reports or filters out the repeated lines in a file
Syntax:
uniq [option] filename
Example : consider the following example.txt file
$ cat example.txt
unix operating system
unix operating system
unix dedicated server
linux dedicated server
• To suppress duplicate lines
$ uniq example.txt
unix operating system
unix dedicated server
linux dedicated server
options
-u lists only lines that are unique
-d lists only the lines that are duplicate
-c counts the frequency of occurences
7
tr command – translating characters
- the tr command automatically translates (substitutes, or maps) one set
of characters to another.
- Input always comes from standard input
- Arguments don’t include filenames
Syntax:
tr [options] "set1" "set2"
Example : Convert lower case letters to upper case
$ echo "linux dedicated server" | tr a-z A-Z
LINUX DEDICATED SERVER
options
-d deletes a specified range of characters
-s squeezes multiple occurrences of a character into a single word
Example: delete specific characters
$ echo "Welcome To GeeksforGeeks" | tr -d ‘W‘
elcome To GeeksforGeeks
8
Example: convert multiple continuous spaces with a single space
$ echo 'too many spaces here' | tr -s '[:space:]'
too many spaces here
9
Filters Using Regular Expression : grep and sed
grep – searching for a pattern
- It scans the file / input for a pattern and displays lines containing the pattern
Syntax:
grep options pattern filename(s)
Example: To demonstrate this, let’s create a text file welcome.txt. Display
lines containing the string “Linux” from the file welcome.txt
$ cat welcome.txt
Welcome to Linux !
Linux is a free and open source Operating system that is mostly used by
developers and in production servers for hosting crucial components such as web
and database servers. Linux has also made a name for itself in PCs.
Beginners looking to experiment with Linux can get started with friendlier linux
distributions such as Ubuntu, Mint, Fedora and Elementary OS.
$ grep “Linux” welcome.txt
Welcome to Linux !
Linux is a free and open source Operating system that is mostly used by
and database servers. Linux has also made a name for itself in PCs.
Beginners looking to experiment with Linux can get started with friendlier linux
10
Example 2: Display lines containing the string “sales “ from the file emp.lst
$ cat emp.lst
2233 | a.k.shukla | g.m | sales | 12/12/52 | 6000
9876 | jai sharma | director | production | 12/03/50 | 7000
5678 | sumit chakrobarty | d.g.m. | marketing | 19/04/43 | 6000
2365 | barun sengupta | director | personnel | 11/05/47 | 7800
5423 | n.k.gupta | chairman | admin | 30/08/56 | 5400
1006 | chanchal singhvi | director | sales | 03/09/38 | 6700
6213 | karuna ganguly | g.m. | accounts | 05/06/62 | 6300
1265 | s.n. dasgupta | manager | sales | 12/09/63 | 5600
4290 | jayant choudhury | executive | production | 07/09/50 | 6000
2476 | anil aggarwal | manager | sales | 01/05/59 | 5000
6521 | lalit chowdury | directir | marketing | 26/09/45 | 8200
3212 | shyam saksena | d.g.m. | accounts | 12/12/55 | 6000
3564 | sudhir agarwal | executive | personnel | 06/07/47 | 7500
2345 | j. b. sexena | g.m. | marketing | 12/03/45 | 8000
0110 | v.k.agrawal | g.m.| marketing | 31/12/40 | 9000
$ grep “sales” emp.lst
2233 | a.k.shukla | g.m | sales | 12/12/52 | 6000
1006 | chanchal singhvi | director | sales | 03/09/38 | 6700
1265 | s.n. dasgupta | manager | sales | 12/09/63 | 5600
2476 | anil aggarwal | manager | sales | 01/05/59 | 5000
11
grep options
-i ignores case for matching
Example : Display lines containing the string “agarwal “ from the file emp.lst
$ cat emp.lst
2233 | a.k.shukla | g.m | sales | 12/12/52 | 6000
9876 | jai sharma | director | production | 12/03/50 | 7000
5678 | sumit chakrobarty | d.g.m. | marketing | 19/04/43 | 6000
2365 | barun sengupta | director | personnel | 11/05/47 | 7800
5423 | n.k.gupta | chairman | admin | 30/08/56 | 5400
1006 | chanchal singhvi | director | sales | 03/09/38 | 6700
6213 | karuna ganguly | g.m. | accounts | 05/06/62 | 6300
1265 | s.n. dasgupta | manager | sales | 12/09/63 | 5600
4290 | jayant choudhury | executive | production | 07/09/50 | 6000
2476 | anil aggarwal | manager | sales | 01/05/59 | 5000
6521 | lalit chowdury | directir | marketing | 26/09/45 | 8200
3212 | shyam saksena | d.g.m. | accounts | 12/12/55 | 6000
3564 | sudhir agarwal | executive | personnel | 06/07/47 | 7500
2345 | j. b. sexena | g.m. | marketing | 12/03/45 | 8000
0110 | v.k.agrawal | g.m.| marketing | 31/12/40 | 9000
$ grep -i “agarwal” emp.lst
3564 | sudhir agarwal | executive | personnel | 06/07/47 | 7500
12
-v Does not display lines matching the expression, select non matching lines
Example: $ grep -v “director” emp.lst
2233 | a.k.shukla | g.m | sales | 12/12/52 | 6000
5678 | sumit chakrobarty | d.g.m. | marketing | 19/04/43 | 6000
5423 | n.k.gupta | chairman | admin | 30/08/56 | 5400
6213 | karuna ganguly | g.m. | accounts | 05/06/62 | 6300
1265 | s.n. dasgupta | manager | sales | 12/09/63 | 5600
4290 | jayant choudhury | executive | production | 07/09/50 | 6000
2476 | anil aggarwal | manager | sales | 01/05/59 | 5000
6521 | lalit chowdury | directir | marketing | 26/09/45 | 8200
3212 | shyam saksena | d.g.m. | accounts | 12/12/55 | 6000
3564 | sudhir agarwal | executive | personnel | 06/07/47 | 7500
2345 | j. b. sexena | g.m. | marketing | 12/03/45 | 8000
0110 | v.k.agrawal | g.m.| marketing | 31/12/40 | 9000
13
-n displays line numbers along with lines
-c displays count of number of occurrences
-l displays list of filenames only
-e exp specifies expression with this option
-E treats pattern as an extended RE
Examples:
$ grep -n “marketing” emp.lst
$ grep -c “director” emp.lst
$ grep –l “manager” *.lst
$ grep -e “agarwal” –e “aggarwal” -e “agrawal” emp.lst

More Related Content

DOCX
Ai unit3 planing
PDF
Linux Performance Tools 2014
DOCX
Advanced file permissions in linux
PDF
Challenges in GPU compilers
PPT
Memory management in linux
PDF
SAP HANA Database
PPT
Lecture 4 text
PDF
Introduction to CUDA
Ai unit3 planing
Linux Performance Tools 2014
Advanced file permissions in linux
Challenges in GPU compilers
Memory management in linux
SAP HANA Database
Lecture 4 text
Introduction to CUDA

Similar to unix- Sort, uniq,tr,grep (20)

PPTX
3 Simple_Filters
PPTX
Basic unix commands
DOCX
15 practical grep command examples in linux
DOCX
15 practical grep command examples in linux
DOCX
Introduction to Unix - POS420Unix  Lab Exercise Week 3 BTo.docx
DOCX
Learning Grep
PPTX
Unix - Filters/Editors
DOCX
Unix
PPT
A Quick Introduction to Linux
RTF
Unix lab manual
PPT
L4_grep command ppt for unix linux programming
DOCX
Introduction to Unix - POS420Unix Lab Exercise Week 3 ATopics.docx
PPT
08 text processing_tools
PPTX
Unix Trainning Doc.pptx
DOC
58518522 study-aix
PPT
grep and egrep linux presentation for lecture
PPTX
Grep - A powerful search utility
PPTX
File systems and inodes
PPSX
Unix environment [autosaved]
3 Simple_Filters
Basic unix commands
15 practical grep command examples in linux
15 practical grep command examples in linux
Introduction to Unix - POS420Unix  Lab Exercise Week 3 BTo.docx
Learning Grep
Unix - Filters/Editors
Unix
A Quick Introduction to Linux
Unix lab manual
L4_grep command ppt for unix linux programming
Introduction to Unix - POS420Unix Lab Exercise Week 3 ATopics.docx
08 text processing_tools
Unix Trainning Doc.pptx
58518522 study-aix
grep and egrep linux presentation for lecture
Grep - A powerful search utility
File systems and inodes
Unix environment [autosaved]
Ad

More from Dr. Girish GS (13)

PPTX
Unix- the process
PPTX
unix- the process states, zombies, running jobs in background
PPTX
Unix - Filters
PPTX
Customizing the unix environment
PPTX
Basic regular expression, extended regular expression
PPTX
Loop instruction, controlling the flow of progam
PPT
Logic instructions part 1
PPTX
Bcd arithmetic instructions
PPTX
Bcd and ascii arithmetic instructions
PPTX
Ascii arithmetic instructions
PPT
Rotate instructions
PPTX
Program control instructions
PPTX
Memory interface
Unix- the process
unix- the process states, zombies, running jobs in background
Unix - Filters
Customizing the unix environment
Basic regular expression, extended regular expression
Loop instruction, controlling the flow of progam
Logic instructions part 1
Bcd arithmetic instructions
Bcd and ascii arithmetic instructions
Ascii arithmetic instructions
Rotate instructions
Program control instructions
Memory interface
Ad

Recently uploaded (20)

PPTX
UNIT 4 Total Quality Management .pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Sustainable Sites - Green Building Construction
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
UNIT 4 Total Quality Management .pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Automation-in-Manufacturing-Chapter-Introduction.pdf
Safety Seminar civil to be ensured for safe working.
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Model Code of Practice - Construction Work - 21102022 .pdf
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Sustainable Sites - Green Building Construction
Operating System & Kernel Study Guide-1 - converted.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Internet of Things (IOT) - A guide to understanding
CYBER-CRIMES AND SECURITY A guide to understanding

unix- Sort, uniq,tr,grep

  • 1. Introduction to Unix and OS Module 3 sort, uniq, tr, grep and sed Dr. Girisha G S Dept. of CSE SoE,DSU, Bengaluru 1
  • 2. Agenda - sort - uniq - Tr - grep - sed 2
  • 3. 3 sort – ordering a file - sort command sorts the contents of the text file line by line Syntax: sort [options] [file] options -n sort numerically -r reverse the order of the sort -t char uses delimiter character to identify fields -k n sort on the nth field -k m,n starts sort on the m filed & ends sort on nth filed -u removes repeated line -o flname places output in the file flname
  • 4. 4 Example: consider the following file $ cat file1.txt Zimbabwe Serbia Norway Australia • Sort the file file1.txt $ sort file1.txt Australia Norway Serbia Zimbabwe • Numeric sorting Example: sort the file marks.txt $ cat marks.txt 22 33 11 77 55 $ sort -n marks.txt 11 22 33 55 77
  • 5. 5 Example : Sort a colon delimited text file on second field $ cat names.txt Alex Jason:200:Sales Emma Thomas:100:Marketing Madison Randy:300:Product Development Nisha Singh:500:Sales Sanjay Gupta:400:Support $ sort -t: -k 2 names.txt Emma Thomas:100:Marketing Alex Jason:200:Sales Madison Randy:300:Product Development Sanjay Gupta:400:Support Nisha Singh:500:Sales
  • 6. 6 uniq command – locate repeated and non repeated lines - uniq command reports or filters out the repeated lines in a file Syntax: uniq [option] filename Example : consider the following example.txt file $ cat example.txt unix operating system unix operating system unix dedicated server linux dedicated server • To suppress duplicate lines $ uniq example.txt unix operating system unix dedicated server linux dedicated server options -u lists only lines that are unique -d lists only the lines that are duplicate -c counts the frequency of occurences
  • 7. 7 tr command – translating characters - the tr command automatically translates (substitutes, or maps) one set of characters to another. - Input always comes from standard input - Arguments don’t include filenames Syntax: tr [options] "set1" "set2" Example : Convert lower case letters to upper case $ echo "linux dedicated server" | tr a-z A-Z LINUX DEDICATED SERVER options -d deletes a specified range of characters -s squeezes multiple occurrences of a character into a single word Example: delete specific characters $ echo "Welcome To GeeksforGeeks" | tr -d ‘W‘ elcome To GeeksforGeeks
  • 8. 8 Example: convert multiple continuous spaces with a single space $ echo 'too many spaces here' | tr -s '[:space:]' too many spaces here
  • 9. 9 Filters Using Regular Expression : grep and sed grep – searching for a pattern - It scans the file / input for a pattern and displays lines containing the pattern Syntax: grep options pattern filename(s) Example: To demonstrate this, let’s create a text file welcome.txt. Display lines containing the string “Linux” from the file welcome.txt $ cat welcome.txt Welcome to Linux ! Linux is a free and open source Operating system that is mostly used by developers and in production servers for hosting crucial components such as web and database servers. Linux has also made a name for itself in PCs. Beginners looking to experiment with Linux can get started with friendlier linux distributions such as Ubuntu, Mint, Fedora and Elementary OS. $ grep “Linux” welcome.txt Welcome to Linux ! Linux is a free and open source Operating system that is mostly used by and database servers. Linux has also made a name for itself in PCs. Beginners looking to experiment with Linux can get started with friendlier linux
  • 10. 10 Example 2: Display lines containing the string “sales “ from the file emp.lst $ cat emp.lst 2233 | a.k.shukla | g.m | sales | 12/12/52 | 6000 9876 | jai sharma | director | production | 12/03/50 | 7000 5678 | sumit chakrobarty | d.g.m. | marketing | 19/04/43 | 6000 2365 | barun sengupta | director | personnel | 11/05/47 | 7800 5423 | n.k.gupta | chairman | admin | 30/08/56 | 5400 1006 | chanchal singhvi | director | sales | 03/09/38 | 6700 6213 | karuna ganguly | g.m. | accounts | 05/06/62 | 6300 1265 | s.n. dasgupta | manager | sales | 12/09/63 | 5600 4290 | jayant choudhury | executive | production | 07/09/50 | 6000 2476 | anil aggarwal | manager | sales | 01/05/59 | 5000 6521 | lalit chowdury | directir | marketing | 26/09/45 | 8200 3212 | shyam saksena | d.g.m. | accounts | 12/12/55 | 6000 3564 | sudhir agarwal | executive | personnel | 06/07/47 | 7500 2345 | j. b. sexena | g.m. | marketing | 12/03/45 | 8000 0110 | v.k.agrawal | g.m.| marketing | 31/12/40 | 9000 $ grep “sales” emp.lst 2233 | a.k.shukla | g.m | sales | 12/12/52 | 6000 1006 | chanchal singhvi | director | sales | 03/09/38 | 6700 1265 | s.n. dasgupta | manager | sales | 12/09/63 | 5600 2476 | anil aggarwal | manager | sales | 01/05/59 | 5000
  • 11. 11 grep options -i ignores case for matching Example : Display lines containing the string “agarwal “ from the file emp.lst $ cat emp.lst 2233 | a.k.shukla | g.m | sales | 12/12/52 | 6000 9876 | jai sharma | director | production | 12/03/50 | 7000 5678 | sumit chakrobarty | d.g.m. | marketing | 19/04/43 | 6000 2365 | barun sengupta | director | personnel | 11/05/47 | 7800 5423 | n.k.gupta | chairman | admin | 30/08/56 | 5400 1006 | chanchal singhvi | director | sales | 03/09/38 | 6700 6213 | karuna ganguly | g.m. | accounts | 05/06/62 | 6300 1265 | s.n. dasgupta | manager | sales | 12/09/63 | 5600 4290 | jayant choudhury | executive | production | 07/09/50 | 6000 2476 | anil aggarwal | manager | sales | 01/05/59 | 5000 6521 | lalit chowdury | directir | marketing | 26/09/45 | 8200 3212 | shyam saksena | d.g.m. | accounts | 12/12/55 | 6000 3564 | sudhir agarwal | executive | personnel | 06/07/47 | 7500 2345 | j. b. sexena | g.m. | marketing | 12/03/45 | 8000 0110 | v.k.agrawal | g.m.| marketing | 31/12/40 | 9000 $ grep -i “agarwal” emp.lst 3564 | sudhir agarwal | executive | personnel | 06/07/47 | 7500
  • 12. 12 -v Does not display lines matching the expression, select non matching lines Example: $ grep -v “director” emp.lst 2233 | a.k.shukla | g.m | sales | 12/12/52 | 6000 5678 | sumit chakrobarty | d.g.m. | marketing | 19/04/43 | 6000 5423 | n.k.gupta | chairman | admin | 30/08/56 | 5400 6213 | karuna ganguly | g.m. | accounts | 05/06/62 | 6300 1265 | s.n. dasgupta | manager | sales | 12/09/63 | 5600 4290 | jayant choudhury | executive | production | 07/09/50 | 6000 2476 | anil aggarwal | manager | sales | 01/05/59 | 5000 6521 | lalit chowdury | directir | marketing | 26/09/45 | 8200 3212 | shyam saksena | d.g.m. | accounts | 12/12/55 | 6000 3564 | sudhir agarwal | executive | personnel | 06/07/47 | 7500 2345 | j. b. sexena | g.m. | marketing | 12/03/45 | 8000 0110 | v.k.agrawal | g.m.| marketing | 31/12/40 | 9000
  • 13. 13 -n displays line numbers along with lines -c displays count of number of occurrences -l displays list of filenames only -e exp specifies expression with this option -E treats pattern as an extended RE Examples: $ grep -n “marketing” emp.lst $ grep -c “director” emp.lst $ grep –l “manager” *.lst $ grep -e “agarwal” –e “aggarwal” -e “agrawal” emp.lst

Editor's Notes

  • #2: We use commands that filter data to select only the portion of data that we wish to view or operate on. t can be used to process information in powerful ways such as restructuring output to generate useful reports, modifying text in files and many other system administration tasks.
  • #4: The sort tool will sort lines alphabetically by default. Sort command compares the first character in every line of file to implement the specified order. If the first character of the 2 line are identical, the command compare the second character
  • #7: When we concatenate or merge files, we will face the problem of duplicate entries creeping in. we saw how sort removes them with the –u option. UNIX offers a special tool to handle these lines – the uniq command. In simple words, uniq is the tool that helps to detect the adjacent duplicate lines and also deletes the duplicate lines. Uniq command in unix or linux system is used to suppress the duplicate lines from a file
  • #8: It can be used to convert uppercase to lowercase, squeeze repeating characters and deleting characters.
  • #10: It can be used for pattern matching Grep searches a file and displays the line containing the pattern . Grep searches for patterns in one or more filenames or the stad input if no filename is specified The grep command is used to search text
  • #14: -e match multiple pattern