SlideShare ist ein Scribd-Unternehmen logo
1 von 13
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

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Oracle notes
Oracle notesOracle notes
Oracle notes
 
Linux Networking Commands
Linux Networking CommandsLinux Networking Commands
Linux Networking Commands
 
Introduction to the Compliance Driven Development (CDD) and Security Centric ...
Introduction to the Compliance Driven Development (CDD) and Security Centric ...Introduction to the Compliance Driven Development (CDD) and Security Centric ...
Introduction to the Compliance Driven Development (CDD) and Security Centric ...
 
Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013
 
SHELL PROGRAMMING
SHELL PROGRAMMINGSHELL PROGRAMMING
SHELL PROGRAMMING
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Linux crontab
Linux crontabLinux crontab
Linux crontab
 
Linux commands
Linux commandsLinux commands
Linux commands
 
Layouts in android
Layouts in androidLayouts in android
Layouts in android
 
Implementation of Pipe in Linux
Implementation of Pipe in LinuxImplementation of Pipe in Linux
Implementation of Pipe in Linux
 
Users and groups
Users and groupsUsers and groups
Users and groups
 
Embedded linux network device driver development
Embedded linux network device driver developmentEmbedded linux network device driver development
Embedded linux network device driver development
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
SQL report
SQL reportSQL report
SQL report
 
Android+init+process
Android+init+processAndroid+init+process
Android+init+process
 
Database Testing
Database TestingDatabase Testing
Database Testing
 
Introduction Linux Device Drivers
Introduction Linux Device DriversIntroduction Linux Device Drivers
Introduction Linux Device Drivers
 
Visual studio code
Visual studio codeVisual studio code
Visual studio code
 
Linux security
Linux securityLinux security
Linux security
 

Mehr von Dr. Girish GS

unix- the process states, zombies, running jobs in background
unix-  the process states, zombies, running jobs in backgroundunix-  the process states, zombies, running jobs in background
unix- the process states, zombies, running jobs in backgroundDr. Girish GS
 
Customizing the unix environment
Customizing the unix environmentCustomizing the unix environment
Customizing the unix environmentDr. Girish GS
 
File systems and inodes
File systems and inodesFile systems and inodes
File systems and inodesDr. Girish GS
 
Basic regular expression, extended regular expression
Basic regular expression, extended regular expressionBasic regular expression, extended regular expression
Basic regular expression, extended regular expressionDr. Girish GS
 
Loop instruction, controlling the flow of progam
Loop instruction, controlling the flow of progamLoop instruction, controlling the flow of progam
Loop instruction, controlling the flow of progamDr. Girish GS
 
Logic instructions part 1
Logic instructions part 1Logic instructions part 1
Logic instructions part 1Dr. Girish GS
 
Bcd arithmetic instructions
Bcd arithmetic instructionsBcd arithmetic instructions
Bcd arithmetic instructionsDr. Girish GS
 
Bcd and ascii arithmetic instructions
Bcd and ascii arithmetic instructionsBcd and ascii arithmetic instructions
Bcd and ascii arithmetic instructionsDr. Girish GS
 
Ascii arithmetic instructions
Ascii arithmetic instructionsAscii arithmetic instructions
Ascii arithmetic instructionsDr. Girish GS
 
Program control instructions
Program control instructionsProgram control instructions
Program control instructionsDr. Girish GS
 

Mehr von Dr. Girish GS (14)

Unix- the process
Unix-  the processUnix-  the process
Unix- the process
 
unix- the process states, zombies, running jobs in background
unix-  the process states, zombies, running jobs in backgroundunix-  the process states, zombies, running jobs in background
unix- the process states, zombies, running jobs in background
 
Unix - Filters
Unix - FiltersUnix - Filters
Unix - Filters
 
Customizing the unix environment
Customizing the unix environmentCustomizing the unix environment
Customizing the unix environment
 
File systems and inodes
File systems and inodesFile systems and inodes
File systems and inodes
 
Basic regular expression, extended regular expression
Basic regular expression, extended regular expressionBasic regular expression, extended regular expression
Basic regular expression, extended regular expression
 
Loop instruction, controlling the flow of progam
Loop instruction, controlling the flow of progamLoop instruction, controlling the flow of progam
Loop instruction, controlling the flow of progam
 
Logic instructions part 1
Logic instructions part 1Logic instructions part 1
Logic instructions part 1
 
Bcd arithmetic instructions
Bcd arithmetic instructionsBcd arithmetic instructions
Bcd arithmetic instructions
 
Bcd and ascii arithmetic instructions
Bcd and ascii arithmetic instructionsBcd and ascii arithmetic instructions
Bcd and ascii arithmetic instructions
 
Ascii arithmetic instructions
Ascii arithmetic instructionsAscii arithmetic instructions
Ascii arithmetic instructions
 
Rotate instructions
Rotate instructionsRotate instructions
Rotate instructions
 
Program control instructions
Program control instructionsProgram control instructions
Program control instructions
 
Memory interface
Memory interfaceMemory interface
Memory interface
 

Kürzlich hochgeladen

Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGSIVASHANKAR N
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 

Kürzlich hochgeladen (20)

Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 

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

Hinweis der Redaktion

  1. 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.
  2. 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
  3. 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
  4. It can be used to convert uppercase to lowercase, squeeze repeating characters and deleting characters.
  5. 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
  6. -e match multiple pattern