Code Scrappers


Pattern matching - The Wild Cards


grep

~ Use grep to display lines containing the string "Sales" from the file listing.lst.

    saarthak@DESKTOP-N01EJET:~$ cat listing.lst
    2222|A K Shukla   | GM      | Sales| 2/12/52|6000
    1234| Chanchal      |Director|Sales|02/09/67|7000
    1267|S N Dasgupta|Manager|Sales|03/06/89|8000
    1245|P Jain |Manager |Technical| 2/12/52|6000
    1242|P Sharma |GM |Technical| 2/11/52|6000

    saarthak@DESKTOP-N01EJET:~$ grep "Sales" listing.lst
    2222|A K Shukla   | GM      | Sales| 2/12/52|6000
    1234| Chanchal      |Director|Sales|02/09/67|7000
    1267|S N Dasgupta|Manager|Sales|03/06/89|8000
        



grep - with 2 filenames

~ When grep is used with multiple filenames, it displays the filenames along with the output.

    saarthak@DESKTOP-N01EJET:~$ grep "Director" listing.lst listing2.lst
    listing.lst:1234| Chanchal      |Director|Sales|02/09/67|7000
    listing2.lst:1234| Chanchal      |Director|Sales|02/09/67|7000
        



grep -n

~ grep -n will print along with the line numbers.

    saarthak@DESKTOP-N01EJET:~$ grep -n "GM" listing.lst
    1:2222|A K Shukla   | GM      | Sales| 2/12/52|6000
    5:1242|P Sharma |GM |Technical| 2/11/52|6000
          



grep -c

~ It will print the number of occurances of that word.

    saarthak@DESKTOP-N01EJET:~$ grep -c "Sales" listing.lst
    3
          



grep -l

~ It will display all the filenames containing that word.

    saarthak@DESKTOP-N01EJET:~$ grep -l "Sales" *.lst
    listing.lst
    listing2.lst
          



'^'

~ '^' symbol with grep is used to print lines starting with a particular letter or number.

    saarthak@DESKTOP-N01EJET:~$ cat listing.lst
    2222|A K Shukla   | GM      | Sales| 2/12/52|6000
    1234| Chanchal      |Director|Sales|02/09/67|7000
    1267|S N Dasgupta|Manager|Sales|03/06/89|8000
    1245|P Jain |Manager |Technical| 2/12/52|6000
    1242|P Sharma |GM |Technical| 2/11/52|6000

    saarthak@DESKTOP-N01EJET:~$ grep "^1" listing.lst
    1234| Chanchal      |Director|Sales|02/09/67|7000
    1267|S N Dasgupta|Manager|Sales|03/06/89|8000
    1245|P Jain |Manager |Technical| 2/12/52|6000
    1242|P Sharma |GM |Technical| 2/11/52|6000
          



'$'

~ '$' symbol with grep is used to print lines ending with a particular letter or number.

    saarthak@DESKTOP-N01EJET:~$ grep "6...$" listing.lst
    2222|A K Shukla   | GM      | Sales| 2/12/52|6000
    1245|P Jain |Manager |Technical| 2/12/52|6000
    1242|P Sharma |GM |Technical| 2/11/52|6000
          



'^[^]'

~ '^[^]' symbol with grep is used to print lines other than the lines starting with a particular letter or number.

    saarthak@DESKTOP-N01EJET:~$ grep "^[^2]" listing.lst
    1234| Chanchal      |Director|Sales|02/09/67|7000
    1267|S N Dasgupta|Manager|Sales|03/06/89|8000
    1245|P Jain |Manager |Technical| 2/12/52|6000
    1242|P Sharma |GM |Technical| 2/11/52|6000
          



grep -E with |

~ Using it we can locate both Director and GM from the file .

    saarthak@DESKTOP-N01EJET:~$ grep -E "Director|GM" listing.lst
    2222|A K Shukla   | GM      | Sales| 2/12/52|6000
    1234| Chanchal      |Director|Sales|02/09/67|7000
    1242|P Sharma |GM |Technical| 2/11/52|6000
          



sed - q

~ It displays the lines but quits after the line no. specified .

    saarthak@DESKTOP-N01EJET:~$ cat listing.lst
    2222|A K Shukla   | GM      | Sales| 2/12/52|6000
    1234| Chanchal      |Director|Sales|02/09/67|7000
    1267|S N Dasgupta|Manager|Sales|03/06/89|8000
    1245|P Jain |Manager |Technical| 2/12/52|6000
    1242|P Sharma |GM |Technical| 2/11/52|6000

    saarthak@DESKTOP-N01EJET:~$ sed '3q' listing.lst
    2222|A K Shukla   | GM      | Sales| 2/12/52|6000
    1234| Chanchal      |Director|Sales|02/09/67|7000
    1267|S N Dasgupta|Manager|Sales|03/06/89|8000
          



sed - p

~ It displays the lines but quits after the line no. specified .

    saarthak@DESKTOP-N01EJET:~$ sed -n '2,3p' listing.lst

    1234| Chanchal      |Director|Sales|02/09/67|7000
    1267|S N Dasgupta|Manager|Sales|03/06/89|8000
          



sed with !

~ It performs neglecting action. It doesn't print lines 3 to end.

    saarthak@DESKTOP-N01EJET:~$ sed -n '3,$!p' listing.lst
    2222|A K Shukla   | GM      | Sales| 2/12/52|6000
    1234| Chanchal      |Director|Sales|02/09/67|7000
        



sed -e

~ -e allows you to enter as many instructions as you wish preceded by the option

    saarthak@DESKTOP-N01EJET:~$ sed -n '3,$!p' listing.lst
    2222|A K Shukla   | GM      | Sales| 2/12/52|6000
    1234| Chanchal      |Director|Sales|02/09/67|7000