grep is used to display specific line/lines of file based on a filter.
Syntax: grep -<switch> <"filter"> <Filename> cat <Filename> | grep -<switch> <"filter">
Note: with grep command you can define only one filter however with egrep you can define multiple filter followed by | such as "file1|filter2|etc."
egrep is used to display specific line/lines of file based on multiple filters.
Syntax: egrep -<switch> <"filter1|fiter2|etc."> <Filename> cat <Filename> | egrep -<switch> <"filter1|fiter2|etc.">
Below are the list of important switches that can be used with grep and egrep command.
Switch | Description |
---|---|
-i | Display case insensitive output. |
-w | Display only those lines that matches exact word. |
-v | Display all lines except filtered items. |
-q | Quite mode (i.e do not display any output) - Used in scripting. |
-n | Return line that contain given string/filer with its line number. |
-c | Display count of line only. |
-r | Display filtered entry/string recursivly i.e if one of more files under same directory or subdirectory contains that entry/string. |
-lr | List filename from current directory or subdirectory that contains filtered entry/string. |
[user1@localhost ~]$ cat Winner-List.txt Name, Sport, Medal Amar, Cricket, Bronze Akbar, Cricket, Silver Amar, Kabaddi, Silver Akbar, Kabaddi, Broze Anthony, Cricket, Gold Anthony, Kabaddi, Gold [user1@localhost ~]$ grep "cricket" Winner-List.txt [user1@localhost ~]$ grep -i "cricket" Winner-List.txt Amar, Cricket, Bronze Akbar, Cricket, Silver Anthony, Cricket, Gold [user1@localhost ~]$ grep -v "cricket" Winner-List.txt Name, Sport, Medal Amar, Cricket, Bronze Akbar, Cricket, Silver Amar, Kabaddi, Silver Akbar, Kabaddi, Broze Anthony, Cricket, Gold Anthony, Kabaddi, Gold [user1@localhost ~]$ cat Winner-List.txt | grep -w "old" [user1@localhost ~]$ cat Winner-List.txt | grep -i "old" Anthony, Cricket, Gold Anthony, Kabaddi, Gold [user1@localhost ~]$ cat Winner-List.txt | egrep -i "gold|bronze" Amar, Cricket, Bronze Anthony, Cricket, Gold Anthony, Kabaddi, Gold [user1@localhost ~]$ if grep -iq "gold" Winner-List.txt > then > echo "gold found" > fi gold found [user1@localhost ~]$ if grep -i "gold" Winner-List.txt ; then echo "gold found"; fi Anthony, Cricket, Gold Anthony, Kabaddi, Gold gold found [user1@localhost ~]$ grep -n cricket Winner-List.txt [user1@localhost ~]$ grep -in cricket Winner-List.txt 2:Amar, Cricket, Bronze 3:Akbar, Cricket, Silver 6:Anthony, Cricket, Gold [user1@localhost ~]$ grep -ic cricket Winner-List.txt 3 [user1@localhost Folder]$ ls -ltR .: total 16 drwxr-xr-x. 2 user1 user1 22 Mar 23 16:47 Folder1 drwxr-xr-x. 2 user1 user1 6 Mar 23 16:46 Folder2 -rw-r--r--. 1 user1 user1 169 Mar 23 16:45 Winner-List.txt -rw-r--r--. 1 user1 user1 10 Mar 23 16:45 file3 -rw-r--r--. 1 user1 user1 51 Mar 23 16:45 file2 -rw-r--r--. 1 user1 user1 411 Mar 16 16:33 file1.txt ./Folder1: total 4 -rw-r--r--. 1 user1 user1 8 Mar 23 16:47 test.txt ./Folder2: total 0 [user1@localhost Folder]$ grep -ir "cricket" * Folder1/test.txt:cricket Winner-List.txt:Amar, Cricket, Bronze Winner-List.txt:Akbar, Cricket, Silver Winner-List.txt:Anthony, Cricket, Gold [user1@localhost Folder]$ grep -ilr "cricket" * Folder1/test.txt Winner-List.txt [user1@localhost Folder]$
awk While grep displays entires line, awk displays only specific entry/column of line.
Syntax: awk -F <"delimiter" '{print $1}'> <Filename> awk -F <"delimiter" '/<search>/{print $1}'> <Filename>
Note: Here in second example, just replace "search" with any case sensitive word to filter some specific line.
[user1@localhost ~]$ cat Winner-List.txt Name, Sport, Medal Amar, Cricket, Bronze Akbar, Cricket, Silver Amar, Kabaddi, Silver Akbar, Kabaddi, Broze Anthony, Cricket, Gold Anthony, Kabaddi, Gold [user1@localhost ~]$ awk -F ", " '{print $1 $3}' Winner-List.txt NameMedal AmarBronze AkbarSilver Amar AkbarBroze AnthonyGold AnthonyGold [user1@localhost ~]$ awk -F ", " '{print $1": " $3}' Winner-List.txt Name: Medal Amar: Bronze Akbar: Silver Amar: Akbar: Broze Anthony: Gold Anthony: Gold [user1@localhost ~]$ awk -F ", " '/gold/{print $1": " $3}' Winner-List.txt [user1@localhost ~]$ awk -F ", " '/Gold/{print $1": " $3}' Winner-List.txt Anthony: Gold Anthony: Gold [user1@localhost ~]$ cat Winner-List.txt | awk -F " " '/Silver/{print $1 $2}' Akbar,Cricket, Amar,Kabaddi, [user1@localhost ~]$ cat Winner-List.txt | awk -F "," '/Silver/{print $1 $2}' Akbar Cricket Amar Kabaddi [user1@localhost ~]$ cat Winner-List.txt | awk -F "," '/Silver/{print $1 "--" $2}' Akbar-- Cricket Amar-- Kabaddi [user1@localhost ~]$ cat Winner-List.txt | awk -F "," '/Silver/{print $1 " --" $2}' Akbar -- Cricket Amar -- Kabaddi [user1@localhost ~]$
cut command is used to display specific entry/column/field of each line based on delimiter.
Switch | Description |
---|---|
-d | Used as delimiter. Such as -d: or -d, etc. |
-f | Used with -d to display sepecific field or column. Such as: -d: -f1,3 - to display first and third column defined by : delimiter -d, -f1-3 - to display from first to third column defined by , delimiter |
-c | Display only specific character or range of characters. Such as -c1-5 or c5 |
-s | Used with -d to display only delimited line. |
[user1@localhost ~]$ cat Winner-List.txt Name, Sport, Medal Amar, Cricket, Bronze Akbar, Cricket, Silver Amar, Kabaddi, Silver Akbar, Kabaddi, Broze Anthony, Cricket, Gold Anthony, Kabaddi, Gold Hello; Hi; Bye [user1@localhost ~]$ cut -d, -f2 Winner-List.txt Sport Cricket Cricket Kabaddi Kabaddi Cricket Kabaddi Hello; Hi; Bye [user1@localhost ~]$ cut -d, -f1,2 Winner-List.txt Name, Sport Amar, Cricket Akbar, Cricket Amar, Kabaddi Akbar, Kabaddi Anthony, Cricket Anthony, Kabaddi Hello; Hi; Bye [user1@localhost ~]$ cut -sd, -f1,2 Winner-List.txt Name, Sport Amar, Cricket Akbar, Cricket Amar, Kabaddi Akbar, Kabaddi Anthony, Cricket Anthony, Kabaddi [user1@localhost ~]$ cut -c1-5 Winner-List.txt Name, Amar, Akbar Amar, Akbar Antho Antho Hello [user1@localhost ~]$