Pages

Efficient use of grep command

The command grep can be used in following ways.

grep "HELLO"  *    (to print the match lines)

grep -v "HELLO" * (to print the non-match lines)

grep -i "hello"  * (case insensitive grep)

grep -A2  (2 lines after the matching line)

grep -B2    (2 lines before the matching line)

grep -C2    (5 lines before and after the matching line

grep -l "converged" *     (to display only the file names with the match

grep -L "converged" *   ( to display the files without match)

grep -c "converged" *   (to count the number of occurrences of the match in each file)

grep -n "converged" fileName.txt (to get the line number)

grep -n "matchLine") fileName.txt | cut -d : -f1  ( to get the line number of the matching line)

grep -nr  (for search inside a directory)

grep -n "matchLine" fileName.txt | awk -F: '{print $1}'   (to get the line number)

grep -in "matchlLine" fileName.txt   (to get all the matching line with line number (no case sensitive)

grep -e " B " -e " C " fileName.txt (to get lines that contain B and C. useful to extract the coordinates)

More on this blog (related)
List of Linux commands and their common and efficient usage (how to use series)
List of high performance computing (HPC) skills you need to know (for a beginner)

No comments:

Post a Comment

You may be interested in these posts

Error in image file conversion: convert-im6.q16: not authorized `test.eps' @ error/constitute.c/WriteImage/1037.

This error is because of the vulnerability. This allows remote execution of code using image formats. So, some Linux distributions by defaul...