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
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
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.
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
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
It can be used to convert uppercase to lowercase, squeeze repeating characters and deleting characters.
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