Filter Commands:

Filter commands are used to filter the output so that the required things can easily be picked up. The commands which are used to filter the output are

  • less
  • more
  • head
  • tail
  • sort
  • cut
  • sed
  • less:-
The less command is used to see the output line wise or page wise.

Ex: less /etc/passwd

[root@localhost ~]# 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
:q

Note:- press Enter key to scroll down line by line (or)
             Use d to go to next page
             Use b to go to previous page
             Use / to search for a word in the file
             Use v to go vi mode where you can edit the file and once you save it you will back to less command

  • more:-
more is exactly same like less
Ex: #more /etc/passwd
Note:- press Enter key to scroll down line by line (or)
             Use d to go to next page
             Use / to search for a word in the file
             Use v to go vi mode where you can edit the file and once you save it you will back to less command
  • head:
It is used to display the top 10 lines of the file.
Ex:# head /etc/passwd
[root@localhost ~]# head /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]#
To display the custom lines:
#head -n /etc/passwd (where n can be any number)
[root@localhost ~]# head -3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@localhost ~]# 

 

  • tail:
It is used to display the last 10 lines of the file
#tail /etc/passwd
[root@localhost ~]# tail /etc/passwd
ipaapi:x:384:384:IPA Framework User:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:383:383:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
lb:x:1000:1000:lb:/home/lb:/bin/bash
[root@localhost ~]#

 

To display the custom lines
#tail -n /etc/passwd (where n can be any number)

[root@localhost ~]# tail -3 /etc/passwd
oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
lb:x:1000:1000:lb:/home/lb:/bin/bash
[root@localhost ~]#
  • sort:-

It is used to sort the output in numeric or alphabetic order
#sort filename

[root@localhost ~]# sort linuxfile
Hello World
linux - centos
linux - RedHat
linux - ubuntu
[root@localhost ~]#
To sort the file according to numbers
#sort –d linuxfile or #sort –h linuxfile
[root@localhost ~]# cat linuxfile
4. linux - RedHat
2. linux - centos
3. linux - ubuntu
1. Hello World
[root@localhost ~]# sort -h linuxfile
1. Hello World
2. linux - centos
3. linux - ubuntu
4. linux - RedHat
[root@localhost ~]#
To remove the duplicate entries from the output
#sort –u linuxfile
[root@localhost ~]# cat linuxfile
linux - RedHat
linux - centos
linux - ubuntu
Hello World
linux - RedHat
linux - ubuntu
[root@localhost ~]# sort -u linuxfile
Hello World
linux - centos
linux - RedHat
linux - ubuntu
[root@localhost ~]#
  • cut:
The cut command is used to pick the given expression (in columns) and display the output.
# cut -d -f filename (where d stands for delimiter ex. : ,“ “ etc and f stands for field)
[root@localhost ~]# cut -d: -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
[root@localhost ~]#
To delimit commas and print the field
#cut –d, -f1 filename
[root@localhost ~]# cat hello
hello,how,are,you
[root@localhost ~]# cut -d, -f1 hello
hello
[root@localhost ~]#
  • sed:
sed stands for stream editor, which is used to search a word in the file and replace it with the word required to be in the output
Note: it will only modify the output, but there will be no change in the original file.

#sed ‘s/searchfor/replacewith/g’ filename
[root@localhost ~]# cat linuxfile
linux - RedHat
linux - centos
linux - ubuntu
Hello World
[root@localhost ~]# sed 's/RedHat/RedHat(RHEL)/g' linuxfile
linux - RedHat(RHEL)
linux - centos
linux - ubuntu
Hello World
[root@localhost ~]#

 

Leave a Reply

Your email address will not be published. Required fields are marked *