Linux/Unix Pipes, Grep & Sort Command - STUDY NOTES

Post Top Ad

Linux/Unix Pipes, Grep & Sort Command


What are pipes in Linux?

The symbol '|' denotes a pipe. If we want to use two or more commands at the same time and run them consecutively, we can use pipes. Pipes enables Unix/Linux users to create powerful commands which can perform complex tasks in a moment.

example.

When 'cat' command is used to view a file which spans multiple pages, the prompt quickly jumps to the last page of the file and we unable to see the content in middle.

To avoid this, we can pipe the output of the 'cat' command to 'less' which will show only one scroll length of content at a time.

cat filename | less



'pg' and 'more' commands

Instead of 'less’, we can also use

cat Filename | pg

or

cat Filename | more

so, we can scroll down by simply hitting the enter key.

The 'grep' command

This command is use of search a particular data or information from a text file.

grep

Following options can be used with this command.

Option
Function
-v
Shows all the lines that do not match the searched string
-c
Displays only the count of matching lines
-n
Shows the matching line and its number
-i
Match both (upper and lower) case
-l
Shows just the name of the file with the string

Example:

grep1
 

The 'sort' command

This command helps in sorting out the contents of a file alphabetically.

The syntax for this command is:

sort Filename

Consider the contents of a file

sort1


Using the sort command

sort2

Following options can be used with this command.

Option
Function
-r
Reverses  sorting
-n
Sorts numerically
-f
Case insensitive sorting 

sort3


next1previous1

No comments:

Post a Comment

Post Bottom Ad