Search String or substring in a file using shell

By | 5th July 2019

We will see the usage of grep command in different cases. The command can be used in different ways based on the need to search a string or substring in a file or in multiple files in a folder.

The grep command stands for “global regular expression print”

General format

grep [-options] [directory/single file] [pattern/-e] [string to search]

To see the full syntax and various options, try “grep –help

To search the string in a single file,

grep file1.txt -e ‘techieshouts’

To search the full word and not the substring,

grep -w file1.txt -e ‘techieshouts’

To search the full word by ignoring case sensitiveness,

grep -wi file1.txt -e ‘techieshouts’

To search all the files recursively in the directory for the same word,

grep -wir /directory -e ‘techieshouts’

To search and get the line number of the word in all the files

grep -wirn /directory -e ‘techieshouts’

To search and get only the file names that contain the word

grep -wirl /directory -e ‘techieshouts’

Also, check “Export Teradata table to file using BTEQ