Toying Common Linux Tools
# grep, egrep, cut, sed, sort, uniq, diff, awk
5 min readJan 23, 2023
grep
Used to search for a specific string or pattern in a file or multiple files.
Example: grep "error" log.txt # searches for the string "error" in the file log.txt
grep -rnw '/path/to/somewhere/' -e 'pattern'
-r or -R is recursive,
-n is line number, and
-w stands for match the whole word.
-l (lower-case L) can be added to just give the file name of matching files.
-e is the pattern used during the search
grep | cut: grep can be used in combination with cut to extract specific columns from the output of grep.
Example: grep "error" log.txt | cut -d " " -f 1,3 # extract the 1st and 3rd fields from the output of grep
function seek {
if [ "$1" == "" ] # not specified
then
echo 'Search key word required'
echo "Usage : $0 seek $1"
else
echo $1
GREP_COLORS='ms=01;32' egrep -inr --color=always $1 $2; # $2 := path_start
fi;
time_stamp=$(date +"%Y-%m-%d_%H%Mhr_%S"sec);
echo $time_stamp;
}
# invert the match using the -v flag:
# print every line in the log.txt, except those lines matching the pattern `INFO`. -i indicates case insensitive.
grep -vi "INFO" log.txt
egrep (extended grep)
A variation of the grep command that supports additional regular expression syntax. It is used to search for a specific pattern in a file or multiple files.
# search for multiple patterns at once.
Example: egrep "error|warning" log.txt searches for…