AWK vs SED vs GREP Commands in Linux!πŸš€

AWK vs SED vs GREP Commands in Linux!πŸš€

Β·

3 min read

🌟AWK

Awk is a scripting language used for manipulating data and generating reports. TShe awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators.

1. awk {print} app.log:- It will prints every line of data from the specified file.

  1. awk '{print $1}' app.log :- It will print all the first column of the file.

  1. awk '{print $1,$2}' app.log :- It will print first and second columns of the files.

  1. awk '{print $1,$2,$4}' app.log:- It will print the First, Second and Fourth columns only.

  1. awk '/INFO/ {print $1,$2,$3}' app.log:- It will filter "INFO" from 1,2 &3 columns.

  1. awk '/INFO/ {count++} END {print count}' app.log:- It will help to count the "INFO" from the File.

  1. awk '$2 < "08:52:50" {print $2}' app.log :- It will print the time of all column where time is less than "08:52:50".

Options Available in awk Command:

πŸ’»SED

SED command in UNIX stands for stream editor and it can perform lots of functions on file like searching, find and replace, insertion or deletion. Though most common use of SED command in UNIX is for substitution or for find and replace. By using SED you can edit files even without opening them, which is much quicker way to find and replace something in file, than first opening that file in VI Editor and then changing it.

  • SED is a powerful text stream editor. Can do insertion, deletion, search and replace(substitution).

  • SED command in unix supports regular expression which allows it perform complex pattern matching.

  1. sed -n '/INFO/p' app.log:- It will highlight all the "INFO" keywords from the log file.

  2. sed 's/INFO/LOG/g' app.log:- It will change all the "INFO" keyword into the "LOG" keyword in the log file.

  3. sed -n -e '/INFO/=' app.log:- It will print the line numbere where "INFO" occur.

  4. sed '1,15 s/INFO/LOG/g' app.log:- It will change "INFO" keyword form line 1 to 15 into the "LOG" keyword in the log file.

Options Available in sed Command:

πŸ”GREP

The 'grep' command stands for "global regular expression print". grep command filters the content of a file which makes our search easy. It filter searches a file for a particular pattern of characters and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression.

  1. grep -i info app.log:- It will filter all the "INFO" keyword from the log log file.

  2. grep -i -c info app.log:- It will count all the "INFO" keyword form the log file.

  3. ps aux | grep ubuntu:- It will show only the "Ubuntu" active processes.

    Options Available in grep Command

    Mastering AWK, SED and GREP elevates your command-line prowess, making you a text-processing maestro in the Linux kingdom. πŸš€πŸŒŸ

Β