Sitemap
Prashantchouhan

Prashantchouhan

Linux Basic Terminal Commands — [awg, grep, sed, xargs, sort]

3 min readJun 14, 2024
  1. AWK
    Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators.
    It enables programmers to write small but effective code in the form of statements that can be used to search a particular pattern in each line of the document and do some actions and operations on it.

AWK Operations:
— Scans a file line by line
— Splits each input line into fields
— Compares input line/fields to pattern
— Performs action(s) on matched lines

awk 'selection _criteria {action }' input-file > output-file

let's assume we have an output.txt file that has the following data stored

ajay manager account 45000
sunil clerk account 25000
varun manager sales 50000
amit manager account 47000
tarun peon sales 15000
deepak clerk sales 23000
sunil peon sales 13000
satvik director purchase 80000
$ awk '{print}' employee.txt
//if we do a action print in this we will print the entire file in this scenerio
ajay manager account 45000
sunil clerk account 25000
varun manager sales 50000
amit manager account 47000
tarun peon sales 15000
deepak clerk sales 23000
sunil peon sales 13000
satvik director purchase 80000

But if we want to print the files matching a certain pattern we like the designation of a manager the command for that will be:

$ awk '/manager/ {print}' employee.txt
//in the the selection criteria is manager and the action {action} is print
//and hence this command

2. grep Command

grep command is used for searching and manipulating text patterns in the file. It's derived from (global search for a regular expression and print matching lines)

grep [options] pattern [files]

//options - here modify the behaviour of grep commands

//patterns - it represents the regex you want to search for

//files - this is the name of the files you wan to search in
Press enter or click to view image in full size

Now let's take an example file as an input file:

unix is great os. unix was developed in Bell labs.

learn operating system.

Unix linux which one you choose.

uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
  1. If you want to search for a string case insensitive in the given file
grep -i "UNix" input.txt

2. If you are displaying the Count of the Number of Matches

grep -c "unix" geekfile.txt

3. Xargs

Get Prashantchouhan’s stories in your inbox

Join Medium for free to get updates from this writer.

Xargs is a great command that reads streams of data from standard input, then generates and executes command line code. It means it can take output of one command and pass it as input to another command.
Syntax of Xargs

xargs [options] [command]

4. Sort
Sort command is used for sorting a file, arranging the record in a order.

  • SORT command sorts the contents of a text file, line by line.
  • The sort command is a command-line utility for sorting lines of text files. It supports sorting alphabetically, in reverse order, by number, by month, and can also remove duplicates.
Press enter or click to view image in full size

5. Sed
Sed is a command in Unix that stands for stream editor and it is widely used for a lot of functions in files like searching, replacing, insertion, and deletion. Sed is a quicker way to replace or find strings or words than opening them in the Vim editor and then finding them and then inserting or replacing them.

sed OPTIONS... [SCRIPT] [INPUTFILE...]

some examples of scripts are:

$sed 's/unix/linux/' input.txt
//it subsitutes all the 'unix' strings to linux in the inpout txt file

similarly, an advanced version of this is when you want to replace the nth word in the text file

$sed 's/unix/linux/2' geekfile.txt

//here the n is 2, it means that replace thge second occurance of the unix
//with linux

and a bit advanced version of this is when you want to replace the entire file like global replacement of that string

$sed 's/unix/linux/g' input.txt
//here the g represent the global scope of that subsituation

--

--