UNIT4

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 105

UNIT 4
✓ Loopin
g
Outline
• Advanced Shell Programming
• Filtering utilities: grep, sed etc.
• awk utility
• Splitting (cat, cut, head and tail), comparing
(cmp, comm., diff), Sorting(sort), Merging &
Ordering files (paste, uniq)
Shell Programming
• VI Editor:-
🞂 The default editor that comes with the UNIX operating 47 system is called vi

(visual editor). Using vi editor, we can edit an existing file or create a


new file from scratch. we can also use this editor to just read a text file.
• Syntax: vi <filename>
Creates a new file if it already does not
exist,
• E.X. $vi filename otherwise opens an existing file.
• File will be open in vi editor , tilde (~) on each line following the cursor. A
tilde represents an unused line. If a line does not begin with a tilde and
appears to be blank, there is a space, tab, newline, or some other non-
viewable character present.
Shell Programming
• VI Editor:-
47
Shell Programming
• Modes of Operation in VI Editor:-
47
Shell Programming
🞂 Command Mode:- The default mode of vi editor where every key is
pressed is interpreted as a command to run on a text. this mode enables
you to perform administrative tasks such as saving the files, executing
the. In this mode, whatever you type is interpreted as a command. To
enter into Command Mode from any other mode, it requires pressing the
[Esc] key. If we press [Esc] when we are already in Command Mode,
then vi will beep or flash the screen.
🞂 Insert mode:- This mode enables you to insert text into the file.
Everything that’s typed in this mode is interpreted as input and finally.
The vi always starts in command mode. To enter text, you must be in
insert mode. To come in insert mode you simply type I .To get out of
insert mode, press the Esc key, which will put you back into command
mode.
🞂 Last Line Mode(Escape Mode):- Line Mode is invoked by typing
a colon [:], while vi is in Command Mode. The cursor will jump to the
last line of the screen and vi will wait for a command. This mode enables
Shell Programming
🞂 Input mode : entering and replacing a text
🞂 i : Inserts text to the left of the cursor.
🞂 I : Inserts text at beginning of current line.
🞂 a : Inserts text to the right of the cursor.
🞂 A : Inserts text at end of current line.
🞂 o : open a line below
🞂 O : open a line above
🞂 r : Replace single character under the cursor with the next character
typed.
🞂 R : Replaces text from the cursor to right.
🞂 s : Replaces single character under the cursor with any number of
characters.
🞂 S :Replaces entire line.
Shell Programming
🞂 Escape mode(last line mode)- saving text and quiting
Shell Programming
🞂 Navigation in vi editor
🞂 (Movement in the four direction h , j , k, l )
🞂 hàTo move left. (20h moves the cursor at 20 character to the left)
🞂 l àTo move right.
🞂 JàTo move down
🞂 kàTo move up (4k—move the cursor 4 line up)

Word Navigation (b, e w)


b – to move the cursor to the left one word at a time.
e -to move the cursor to the last character of the current word.
w- to move the cursor to the right one word at a time.
Shell Programming
🞂 (Movement in the line)
🞂 Press ^ to move the cursor to the start of the current line.
🞂 Press $ to move the cursor to the end of the current line.
🞂 (Scrolling)
🞂 Page Forward One Screen --To scroll forward (move down) one
screenful, press Ctrl-F.
🞂 Scroll Forward One-Half Screen--To scroll forward one half of a
screen, press Ctrl-D.
🞂 Page Backward One Screen --To scroll backward (that is., move up)
one screenful, press
Ctrl-B.
🞂 Scroll Backward One-Half Screen --To scroll backward one half of a
screen, press Ctrl-U
Shell Programming
(Absolute Movement)
🞂ctrl+g à to show current line
number.
🞂40g à goes to line number 40
🞂 1g à goes to line number 1
🞂g à goes to end of file.

(Editing a text)
Yà copy yy--Copies the current line.
10yy à copy total 10 lines from the current line
P à Puts the copied text after the cursor.
Shell Programming
🞂 Deleteing and Joining a text
🞂 Dd à Deletes the line the cursor is on.
🞂 x à copy Deletes the character under the cursor location
🞂 X àDeletes the character before the cursor location
🞂 2x à deletes two characters under the cursor location and 2dd
deletes two lines at the current position.
🞂 J àJoins the current line with the next one. A count of j commands
join many lines.
🞂 j àJoins the current line with the next one. A count joins that many
lines.
🞂 U àRestores the current line to the state it was in before the
cursor entered the line.
🞂 u àThis helps undo the last change that was done in the file.
Typing 'u' again will re-do the change
Shell Programming
Pattern Matching and Wild card characters
🞂 * matches zero or more character(s) in a file (or directory) name.
🞂 ? It will match exactly one character.
🞂 [ijk] A single character either I, j or k
🞂 [x-z] a single character that is within the asci range of the
characters x and z.
🞂 [!ijk] a single character that is not an I , j or k
🞂 [!x-z] a single character that is not within the asci range of the
characters x and z.
Shell Programming
🞂 EX:-
🞂 $ ls chap1 chap2 chap3 à ls chap* (it will match all files and
directory start with
chap)
🞂 $ echo* à(list of all files in current directory)
🞂 $ ls ???.txt à (match .txt file having at least 3 character )
🞂 $ ls .??? à (all hidden file names having at least 3 character after .
(dot))
🞂 $ ls emp*.txt à (it will match all text files start with emp)
🞂 $ ls chap? à (it will display file name like , chap1, chapx , chap5,
chapy )
🞂 $ cp * bcaà (copy all files from cuurent directory into bca
directory)
🞂 $ rm * à(all the files will be deleted from current directory)
Shell Programming

🞂 ls *.c à list all files with extension c


🞂 mv *.. /bin à moves all files to bin subdirectory of parent directory.
🞂 cp ????? Progs à copies to progs directory all files with 5
character long.
🞂 ls chap0[124] à list chap01, chap02 and chap04
🞂 ls chap0[1-4] à list chap01, chap02 , chap03 and chap04
🞂 ls chap0[!124] à list all file except chap01, chap02 and chap04
🞂 lp chap[0-1][0-9] à print all files from chap00, chap01… to
chap19.
🞂 Ls chap[x-z] à list chapx, chapy, and chapz
🞂 *.[co] à mathches all file name with single character extension .c
and .o
Shell Programming
🞂 *.[!o] à mathches all file name with single character extension but
not .o
🞂 [a-zA-z]* à matching all file name beginning with an alphabets
🞂 [!a-zA-Z]* à matching all file name don’t begin with an alphabets
Shell Programming
Unix Environment Variables
An important Unix concept is the environment, which is defined by
environment variables. A variable is a string to which we assign a value.
The value assigned could be a number, text, filename, device, or any
other type of data.
🞂 For example, first we set a variable TEST and then we access its value
using the
🞂 echo command: (environment variable used with $)
🞂 E.g. TEST=" BCA"
echo $TEST
BCA
🞂 The environment variables are set without using the $ sign (TEST)but
while accessing them we use the $ sign as prefix.($TEST).
Shell Programming
🞂 Environment Variable PATH
🞂 When you type any command on the command prompt, the shell has
to locate the command before it can be executed.
🞂 The PATH variable specifies the locations in which the shell
should look for commands. Usually the Path variable is set as follows,
$PATH=/bin:/usr/bin
🞂 $ HOME HOME Indicates the home directory of the current user.
🞂 PWD
🞂 PWD Indicates the current working directory as set by the cd
command.
Shell Programming
UID
🞂 UID Expands to the numeric user ID of the current user, initialized at
the shell start-up.
🞂 E.G
🞂 $ echo $HOME
🞂 /root
🞂 $ echo $PATH
🞂 /usr/local/bin:/bin:/usr/bin:/home
Advance Filter Commands
• grep command:-
• The grep 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.
• grep stands for globally search for regular expression
and print out.
🞂 Syntax :
Optio pattern [files]
grep [options] Use
n
grep -c Prints only a count of the lines that match a pattern
Display the matched lines, but do not display the
grep -h
filenames
grep -l Displays list of a filenames only
grep -i Ignores, case for matching
grep Command Example
grep Command Example
Prints only a count of the lines that match a
grep -c
pattern
Display the matched lines, but do not display the
grep -h
filenames
grep -l Displays list of a filenames only
grep Command Example
grep -n Display the matched lines and their line numbers
grep Command Example
This prints out all the lines that do not matches
grep -v
the pattern
grep Command Example
grep -w Match Exact (whole) word
grep Command Example
grep -o Print only the matched parts of a matching line
grep Command Example
grep Command
• grep is used with multiple file :-

🞂 $ grep ‘Anita Patel’ u2.txt


-- quoting is necessary when the pattern contain multiple
words.
grep Command
🞂 $grep –e : matching multiple pattern
grep -e "Anita" -e "hiya" u2.txt
grep Command
🞂 $grep –f : taking a pattern from file and match with a
another file.
first create keyword.txt
grep Command
• Basic Regular Expression(BRE):-
Character Class :[ ]
A regular expression lets you specify a group of characters enclosed
within a pair of rectangular brackets, [ ].
e.g [aA]--it matches either a or A .
[aA]n[iI][tT]a – it will match Anita and anita

🞂 $ grep “[aA]n[iI][tT]a” u2.txt


grep Command
• Basic Regular Expression(BRE):-
Inverse (^) : regular expression use the (^) caret sign for invert the
operation. When the character class begins with this character , all
character other than the group are match.

$ grep “[^A-Za-z]”prof1-- it matches a single nonalphabetic character


string.(^)
grep Command
• Basic Regular Expression(BRE):-
To print the matching with the end with given keyword($)

Suppose we have 100 files and directory and we need to search a


keyword in all file.

grep -R -f keyword.txt .. /pritesh /home/ here R-recursive


grep Command
• Basic Regular Expression(BRE):-
The dot (.) : matches a single character.
Example : 2… --matches four character pattern beginning with a 2.
(2???)

The regular expression .* -- matches any number of character or


none.

$ grep “a. *patel” prof1

Matching multiple patterns( |, (and) )


| - is a delimiter of multiple patterns.
( ) – group of patterns.
grep Command
• egrep (extended grep) Command:-
We can use for the multiple keyword search.
egrep “keyword|key2|key3” filename

If you just want search but don’t want to print on terminal


grep –q “keyword” filename
Echo $? //check , it return 0 means process done
Fgrep:-fixed/ fast grep :-Only search fixed string
fgrep “keyword” filename
grep Command
• Options Description:-
• -c : This prints only a count of the lines that match a pattern
• -h : Display the matched lines, but do not display the filenames.
• -i : Ignores, case for matching
• -l : Displays list of a filenames only.
• -n : Display the matched lines and their line numbers.
• -v : This prints out all the lines that do not matches the pattern
• -e exp : Specifies expression with this option. Can use multiple times.
• -f file : Takes patterns from file, one per line.
• -E : Treats pattern as an extended regular expression (ERE)
• -w : Match whole word
• -o : Print only the matched parts of a matching line, with each such part
on a separate output line.
https://www.sanfoundry.com/unix-questions-answers-grep-command-1/
https://www.cranesvarsity.com/linux-mcq/
Advance Filter Commands
• sed command:- (Stream Editor)
🞂 It can perform lot’s of function 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.
🞂 It is a powerful text stream editor. Can do insertion, deletion, search and
replace(substitution).

• Sed command for Unix support regular expression which allow at perform
complex pattern matching.
🞂 Syntax: sed options ‘address action’ file(s)
- address and action are enclosed within a single quotes.
Advance Filter Commands
• sed command:- (Stream Editor)
🞂 Addressing in sed is done in two ways:

🞂 By one or two line numbers (like, 3,7)


By specifying a /-enclosed pattern which occurs in a line(/ and
/)
Line Addressing:-
Instruction is enclosed within quotes. First instruction is an address(line
number) and
second is action(q-quit, p-print).
Example: $ sed ‘3q’ emp.lst --- just like head –n 3 (after 3rd line
quit)
Advance Filter Commands
• sed command:- (Stream Editor)
🞂 Sed command output display a selected lines as well as all lines. So
suppress this behavior
use –n option.
🞂 $ sed –n ‘1,2p’ emp.lst -- print first, two lines only.
🞂 $ sed –n ‘9,11p’ emp.lst --print lines 9 to 11

🞂 $ sed –n ‘$p’ emp.lst --select only last line


Advance Filter Commands
• sed command:- (Stream Editor)
🞂 Negating the Action (!) (not operator)
🞂 $ sed –n ‘3,$!p’ emp.lst -- don’t print line 3 to last

🞂 Using multiple instruction (-e)


🞂 $ sed –n –e ‘2p’ -e ‘9p’ -e ‘$p’ emp.lst --multiple group of lines
Advance Filter Commands
• sed command:- (Stream Editor)
• Take instruction from file (-f) 163

$ cat >line_add //create file


• sed -n -f lin_add emp.lst
Advance Filter Commands
• sed command:- (Stream Editor)
🞂 Addressing in sed is done in two ways:
🞂 sed -n '/India/p' emp.lst // retrive data some specific
country

🞂 sed -n -e '/India/p' -e '/USA/p' emp.lst // retrive data some


multiple countries
🞂 retrive data from 2 line and then after print 2 line
Advance Filter Commands
• sed command:- (Stream Editor)
163
🞂 I want even line

🞂 I am modifying a name
🞂 sed 's/Anu/diyu/g' emp.lst
🞂 sed 's/India/USA/g' emp.lst
sed '/Anita/ s/USA/India/g' emp.lst //specific changing
Advance Filter Commands
• Changing in file
163
Advance Filter Commands
• sed command:- (Stream Editor)
163
🞂 Deleting a line:
🞂 $ sed '4d' emp.lst // 4th line deleting
• $ sed '$d' emp.lst //last line delete
• $ sed '2,4d' emp.lst //range 2 to 4 line delete
• $ sed '/UK/d' emp.lst //Specific coutry delete
• $ sed '/^$/d' emp.lst //empty line delete
• $ sed -i '/^$/d' emp.lst //permanently delete empty line

• $ sed '/India/ w IndianUser' emp.lst //new file created IndianUser


• sed '3 a Hello User welcome!!!!!!!!' emp.lst //Append content
• sed '/Krish/ a Hello' emp.lst
Advance Filter Commands
• sed command:- (Stream Editor)
163
🞂 Show hidden character:
🞂 $ sed -n 'l' emp.lst
🞂 $ sed -n ‘l 10' emp.lst //wrapping content
Advance Filter Commands
• sed command:- (Stream Editor)
163
🞂 Add external file content:

• $ sed '2 e date' emp.lst //add date in 2 line


• $ sed '=' emp.lst //retrive line number
• sed '/Krish/ a Hello' emp.lst
Advance Filter Commands
• awk command:- (Aho, Weinberger, Kernighan)
🞂 scripting language used for manipulating data and generating reports.
🞂 Version of awk-awk, nawk, mawk, pgawk, GNU awk.
🞂 What can you do with awk?
🞂 awk operation:
🞂 scans a file line by line
🞂 splits each input line into fields
🞂 compares input line/fields to pattern
🞂 performs action(s) on matched lines
🞂 Useful for:
🞂 transform data files
🞂 produce formatted reports
Advance Filter Commands
• awk command:-
• Programming constructs:-
• format output lines
• arithmetic and string operations and conditionals and loops

• Basic awk Syntax:-

• awk [options] ‘selection criteria {action}’ file(s)


• Options:
• -F to change input field separator
• -f to name script file
• Action :
• {print}
Advance Filter Commands
• awk command:-
• Simple awk filtering

• $ awk '{print $2,$4}' emp.lst // multiple field filtering


• $ awk '{print $NF}' emp.lst //last field
• ls -ltr | awk '{print $NF}’ // last field only file name
• $ awk '{print NR ":" $0}' name // Add delimeters like : (csv comma
sepeated value)
Advance Filter Commands
• awk command:-
• Find Specific field
Advance Filter Commands
• awk command:-
• Arithmetic Operators:-

• Operator Meaning Example

+ Add x +y
- Subtract x –y
* Multiply x *y
/ Divide x/y
% Modulus x %y
^ Exponentialx ^y
• Example:-

• % awk '$3 * $4 > 500 {print $0}' file


Advance Filter Commands
• awk command:-
• Relational Operators (comparision operator):-
• Operator Meaning Example

< Less than x<y


<= Less than or equal x <=y
== Equal to x == y
!= Not equal to x != y
> Greater than x >y
>= Greater than or equal to x>=y
~ Matched by reg exp x ~ /y/
!~ Not matched by req exp x !~ /y/
Example:-
Advance Filter Commands
• awk command:-

• $ awk ' NF==0 {print NR}' country.txt //print black(empty) line


number
$ awk 'END {print NR}' country.txt // print total line
number in file
Advance Filter Commands
• awk command:-
•$ awk '{if($2=="Anita"){$3=80000} print $0}'
emp.lst //change 2nd person salary

• Logical Operators:-

• Operator Meaning Example


&& Logical AND a && b
|| Logical OR a || b
! NOT !a
• Example:-

• $ awk '($2 > 5) && ($2 <= 15) {print $0}' file
• $ awk '$3 == 100 || $4 > 50' file
Advance Filter Commands
• awk command:-
• Output Statements:-
• print :- print easy and simple output
• printf:- print formatted (similar to C printf)
• sprintf:-format string (similar to C sprintf)

• Function: print
• Writes to standard output
• Output is terminated by ORS
• default ORS is newline
• If called with no parameter, it will print $0
• Printed parameters are separated by OFS,
• default OFS is blank
• Print control characters are allowed:
• \n \f \a \t \\ …
Advance Filter Commands
• awk command:-
• Some System Variables (Built-in variables ):-

• FS Field separator (default=whitespace)


• RS Record separator (default=\n)
• NF Number of fields in current record
• NR Number of the current record
• OFS Output field separator (default=space)
• ORS Output record separator (default=\n)
• FILENAME Current filename
Advance Filter Commands
• awk command:-
• Using for loop , while loop
cat Command
• It is used to create, display and concatenate file contents.
🞂 Syntax :
cat [OPTION] [FILE]
🞂 Example :
Option Use
cat -b Omits line numbers for blank space in the output
cat -E Displays a $ (dollar sign) at the end of each line
cat -n Line numbers for all the output lines
cat -s Suppress repeated empty output lines
cat -T Displays the tab characters as ^I in the output
cat Command
🞂 Example :
$ cat > file1.txt
🞂 It creates file1.txt and allow us to insert content for this file.
🞂 After inserting content you can use ctrl+c to exit the file.
$cat file.txt > newfile.txt
🞂 Read the contents of file.txt and write them to newfile.txt, overwriting
anything newfile.txt previously contained. If newfile.txt does not exist, it
will be created.
$cat file.txt >> newfile.txt
🞂 Read the contents of file.txt and append them to the end of newfile.txt.
If newfile.txt does not exist, it will be created.
cat Command
🞂 Example :
cat file1.txt file2.txt
• It will read the contents of file1.txt and file2.txt and display the result in
the terminal.
cat file1.txt file2.txt > combinedfile.txt
• It will concatenate the contents of file1.txt and file2.txt and write them to
a new file combinedfile.txt using the (>) operator.
• If the combinedfile.txt file doesn’t exist the command will create it.
Otherwise it will overwrite the file.
cat Command Example
🞂 $ cat > file1.txt
• It creates file1.txt and allow us to insert content for this file.
• After inserting content you can use ctrl+c to exit the file.
cat Command Example
cat -b Omits line numbers for blank space in the output
cat -E Displays a $ (dollar sign) at the end of each line
cat Command Example
🞂 $cat file.txt > newfile.txt
• Read the contents of file.txt and write them to newfile.txt, overwriting
anything newfile.txt previously contained.
• If newfile.txt does not exist, it will be created.
cat Command Example
🞂 $cat file.txt >> newfile.txt
• Read the contents of file.txt and append them to the end of newfile.txt.
If newfile.txt does not exist, it will be created.
cat Command Example
🞂 cat file1.txt file2.txt
• It will read the contents of file1.txt and file2.txt and display the result in
the terminal.
cat Command Example
🞂 cat file1.txt file2.txt > combinedfile.txt
• It will concatenate the contents of file1.txt and file2.txt and write them to
a new file combinedfile.txt using the (>) operator.
• If the combinedfile.txt file doesn’t exist the command will create it
otherwise it will overwrite the file.
cut Command
• The cut command extracts a given number of characters or columns from
a file.
🞂 Syntax :
cut [-options] [file]
🞂 Example :

Optio
Use
n
Select only the characters from each line as specified in
cut -c
LIST
cut -b Select only the bytes from each line as specified in LIST
Cuts the input file using list of field. The default field to be
cut -f used TAB. The default behavior can be overwritten by
use of -d option
Specifies a delimiter to by used as a field. Default field is
cut -d
TAB and this option overwrites this default behavior
cut Command Example
Select only the characters from each line as specified
cut -c
in LIST
cut Command Example
cut -b Select only the bytes from each line as specified in LIST
cut Command Example
Cuts the input file using list of field. The default field to be used TAB. The
cut -f
default behavior can be overwritten by use of -d option
Specifies a delimiter to by used as a field. Default field is TAB and this
cut -d
option overwrites this default behavior
head Command
• head makes it easy to output the first part (10 lines by default) of files.
🞂 Syntax :
head [OPTION]... [FILE]...
🞂 Example :

Optio
Use
n
head - Print the first n lines instead of the first 10; with the
n leading '-', print all but the last n lines of each file
Print the first n bytes of each file; with a leading '-', print
head -c
all but the last n bytes of each file
head -
Never print headers identifying file names
q
head Command Example
head Command Example
head - Print the first n lines instead of the first 10; with the
n leading '-', print all but the last n lines of each file
head Command Example
head - Print the first n bytes of each file; with a leading '-',
c print all but the last n bytes of each file
head Command Example
head -
Never print headers identifying file names
q
tail Command
• tail is a command which prints the last few number of lines (10 lines by
default) of a certain file, then terminates.
🞂 Syntax :
tail [OPTION]... [FILE]...

Optio
Use
n
tail -n Output the last num lines, instead of the default (10)
tail -c Output the last num bytes of each file
tail -q Never output headers
tail Command Example
tail Command Example
tail -n Output the last num lines, instead of the default (10)
tail Command Example
tail -c Output the last num bytes of each file
cmp Command
• cmp command in Linux/UNIX is used to compare the two files byte by
byte and helps you to find out whether the two files are identical or not.
• If a difference is found, it reports the byte and line number where the first
difference is found.
• If no differences are found, by default, cmp returns no output.
🞂 Syntax :
• cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]
cmp Command Example

cmp -b Print differing bytes


cmp -i Skip a particular number of initial bytes from both the files
Do not print anything; only return an exit status indicating
cmp -s
whether the files differ
cmp -n Compare at most LIMIT bytes
cmp -l Print byte position and byte value for all differing bytes
cmp Command Example
cmp -b Print differing bytes
cmp Command Example
Skip a particular number of initial bytes from both the
cmp -i
files
cmp Command Example
Do not print anything; only return an exit status
cmp -s
indicating whether the files differ
cmp Command Example
cmp -n Compare at most LIMIT bytes
cmp Command Example
Print byte position and byte value for all differing
cmp -l
bytes
comm Command
• Compare two sorted files line by line.
🞂 Syntax :
comm [OPTION]... FILE1 FILE2
🞂 Example :
comm Command Example
Option Use
comm -
Suppress column 1 (lines unique to FILE1)
1
comm -
Suppress column 2 (lines unique to FILE2)
2
comm -
Suppress column 3 (lines that appear in both files)
3
diff(difference) Command
• This command is used to display the differences in the files by comparing
the files line by line
• diff analyzes two files and prints the lines that are different. Essentially, it
outputs a set of instructions for how to change one file to make it identical
to the second file.
🞂 Syntax :
diff [options] File1 File2
🞂 Example :

Option Use
diff -b Ignores spacing differences
diff -i Ignores case
diff Command Example

Special symbols are:


• a : add
• c : change
• d : delete
sort Command
• sort command is used to sort a file, arranging the records in a particular
order.
• By default, the sort command sorts file assuming the contents are ASCII.
Using options in sort command, it can also be used to sort numerically.
🞂 Syntax : sort [OPTION]... [FILE]...

Option Use
sort -c To check if the file given is already sorted or not
sort -r Reverse the result of comparisons
sort -n Compare according to string numerical value
sort -nr To sort a file with numeric data in reverse order
sort -k Sorting a table on the basis of any column
sort -b Ignore leading blanks
sort Command Example
sort Command Example
sort -c To check if the file given is already sorted or not
sort Command Example
sort -r Reverse the result of comparisons
sort Command Example
sort -n Compare according to string numerical value
sort -nr To sort a file with numeric data in reverse order
sort Command Example
sort -k Sorting a table on the basis of any column
paste Command
• The paste command displays the corresponding lines of multiple files side-
by-side.
🞂 Syntax :
paste [-options] [file]
🞂 Example :

Optio
Use
n
paste -
Reuse characters from LIST instead of tabs
d
paste -
Paste one file at a time instead of in parallel
s
paste Command Example
paste - Reuse characters from LIST instead
d of tabs
paste Command Example
paste Paste one file at a time instead of in
-s parallel
uniq Command
• uniq reports or filters out repeated lines in a file.
• It can remove duplicates, show a count of occurrences, show only
repeated lines, ignore certain characters and compare on specific fields.
🞂 Syntax :
uniq [OPTION]... [INPUT [OUTPUT]]

Optio
Use
n
uniq -u Prints only unique lines
uniq -d Only print duplicated lines
uniq -D Print all duplicate lines
Prefix lines with a number representing how many times
uniq -c
they occurred
uniq -i Ignore case when comparing
uniq Command Example
uniq Command Example
uniq -u Prints only unique lines
uniq Command Example
uniq -d Only print duplicated lines

You might also like