L12 Basic Linux Extras Sed and Awk Intros Slides
L12 Basic Linux Extras Sed and Awk Intros Slides
Linux® is the registered trademark of Linus Torvalds in the U.S. and other countries.
Linux Course
Introduction to ‘sed’
Sarfraaz Ahmed
Linux Course
sed
• Stream Editor
• Non-interactive
Sarfraaz Ahmed
Linux Course
sed Operations
• Print specific lines of a file
Sarfraaz Ahmed
Linux Course
Printing Lines
Printing with p
command command
Sarfraaz Ahmed
fi
fi
Linux Course
Sarfraaz Ahmed
fi
fi
fi
Linux Course
Sarfraaz Ahmed
fi
fi
fi
fi
Linux Course
Sarfraaz Ahmed
fi
fi
fi
fi
Linux Course
Printing lines
Printing with p
Sarfraaz Ahmed
fi
fi
fi
fi
fi
Linux Course
Sarfraaz Ahmed
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
Linux Course
Deleting Lines
Delete with d Delete lines
Delete line that contain the
number 2 text xxx
Here, delete means, skip. Don’t show the lines. Original le is una ected
Sarfraaz Ahmed
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
ff
Linux Course
point one
point two
point three
…
last point
Sarfraaz Ahmed
fi
Linux Course
Sarfraaz Ahmed
fi
fi
fi
fi
Linux Course
line onE
line thrEe
line vE
Sarfraaz Ahmed
fi
fi
fi
fi
Linux Course
Sarfraaz Ahmed
fi
fi
fi
Linux Course
sed Examples
• Delete blank lines : sed ‘/^$/ d’ file.txt
Sarfraaz Ahmed
Linux Course
Introduction to ‘awk’
Sarfraaz Ahmed
Linux Course
awk
• Aho, Weinberger and Kernighan
• Generation of reports
Sarfraaz Ahmed
Linux Course
awk format
Check if Perform actions
pattern is present for lines that match
in current line the pattern.
If no “action” is given, then print the lines that contain the pattern.
Sarfraaz Ahmed
fi
fi
Linux Course
awk format
Fields NF
NR
{
Current records
ordinal
$1
john
$2
python
$3
ex01
$4
100
no. of elds
in current
record
{
number ben shell ex01 90
carl shell ex02 90 $0
data.txt
Sarfraaz Ahmed
fi
Linux Course
awk format
• NR : Record Number
awk pattern { actions }
• NF : Number of Fields
Sarfraaz Ahmed
fi
Linux Course
Printing Lines
$ awk ‘ { print } ’ data.txt Same
output
$ awk ‘ { print $0 } ’ data.txt
Print each
record john python ex01 100
ben shell ex01 90
carl shell ex02 90
ben shell ex02 95
john python ex02 90
ben python ex01 80
ben shell ex03 100
carl shell ex03 80
john shell ex01 90
ben python ex02 75
Sarfraaz Ahmed
Linux Course
Printing Fields
$ awk ‘ { print $1, “\t: ”, $4 } ’ data.txt Same
output
$ awk ‘ { print $1, “\t: ”, $NF } ’ data.txt
Print Field john : 100
numbers 1 and 4 ben : 90
in each record carl : 90 john python ex01 100
ben shell ex01 90
ben : 95 carl shell ex02 90
ben shell ex02 95
john : 90 john python ex02 90
ben python ex01 80
ben : 80 ben shell ex03 100
carl shell ex03 80
ben : 100 john shell ex01 90
ben python ex02 75
carl : 80
john : 90
ben : 75
Sarfraaz Ahmed
Linux Course
Print
Record number
Printing Fields
as well
Sarfraaz Ahmed
Linux Course
awk Script
$ awk ‘ { print NR, $1, “\t: ”, $NF } ’ data.txt
$ cat names.awk
{ Same
print NR " " $1 "\t: " $NF; output
}
Sarfraaz Ahmed
Linux Course
Sarfraaz Ahmed
fi
Linux Course
Comparing values
$ cat names.awk
$NF > 80 {
print NR, " “, $1, "\t: “, $NF;
}
Print only
those lines where $ awk -f names.awk data.txt
marks are > 80
1 john : 100 john
ben
python
shell
ex01
ex01
100
90
2 ben : 90 carl
ben
shell
shell
ex02
ex02
90
95
3 carl : 90 john
ben
python
python
ex02
ex01
90
80
4 ben : 95 ben
carl
shell
shell
ex03
ex03
100
80
5 john : 90 john
ben
shell
python
ex01
ex02
90
75
7 ben : 100
9 john : 90
Sarfraaz Ahmed
Linux Course
Comparing values
$ cat names.awk
$2 ~ /py/ {
print NR, " “, $1, "\t: “, $NF;
~ [ tilde ] }
is used to
match $ awk -f names.awk data.txt
patterns in
john python ex01 100
elds ben shell ex01 90
carl shell ex02 90
5 john : 90 ben
ben
python
shell
ex01
ex03
80
100
6 ben : 80 carl
john
shell
shell
ex03
ex01
80
90
Sarfraaz Ahmed
fi
Linux Course
BEGIN
BEGIN and END
$ cat names.awk
is executed BEGIN {
before any line count = 0; # initialisation
is read from } Executed
input for every line
$2 ~ /py/ { read from
count++; input
print NR, " “, $1, "\t: “, $NF;
}
1 john : 100
5 john : 90
6 ben : 80
10 ben : 75
=================
Total entries : 4
Sarfraaz Ahmed
Linux Course
Summation
Add up
$ cat names.awk marks from
/john/ {
each matched
total is total += $NF; line
initialised print NR, " “, $1, "\t: “, $NF;
to 0 by }
default END {
print "===================="; john python ex01 100
ben shell ex01 90
print "Total marks : ", total; carl shell ex02 90
ben shell ex02 95
} john python ex02 90
ben python ex01 80
ben shell ex03 100
carl shell ex03 80
$ awk -f names.awk data.txt john
ben
shell
python
ex01
ex02
90
75
1 john : 100
5 john : 90
9 john : 90
=================
Total marks : 280
Sarfraaz Ahmed
Linux Course
Average
$ cat names.awk
/py/ {
pc++;
ptotal += $NF;
Multiple };
pattern and /sh/ {
action pairs sc++;
stotal += $NF;
are separated by john python ex01 100
} ben shell ex01 90
semicolon ; END { carl shell ex02 90
ben shell ex02 95
print "================================"; john
ben
python
python
ex02
ex01
90
80
print "Avg marks for Python: ", ptotal/pc; ben shell ex03 100
carl shell ex03 80
print "Avg marks for Shell : ", stotal/sc; john shell ex01 90
ben python ex02 75
}
Linux Course
Field Separator
john,python,ex01,100
ben,shell,ex01,90
FS Input Field Separator $ cat names.awk carl,shell,ex02,90
ben,shell,ex02,95
BEGIN { john,python,ex02,90
OFS Output Field Separator ben,python,ex01,80
FS=“," ; OFS=" : ";
ben,shell,ex03,100
RS="\n"; ORS=",\t";
carl,shell,ex03,80
} john,shell,ex01,90
RS Input Record Separator /john/ { ben,python,ex02,75
print NR,$1,$NF;
john python ex01 100
} ben shell ex01 90
ORS Output Record Separator carl shell ex02 90
ben shell ex02 95
john python ex02 90
ben python ex01 80
$ awk -f names.awk data.csv ben
carl
shell
shell
ex03
ex03
100
80
john shell ex01 90
ben python ex02 75
OFS ORS
Sarfraaz Ahmed
Linux Course
Conditional checks
$ cat names.awk
john python ex01 100
ben shell ex01 90
carl shell ex02 90
/ben/ { ben shell ex02 95
john python ex02 90
if ($2 == "shell") { ben python ex01 80
ben shell ex03 100
print $0; carl shell ex03 80
john shell ex01 90
total += $NF; ben python ex02 75
Linux Course
awk Examples
Date
[10/Jul/1995:22:18:17
Sarfraaz Ahmed
Linux Course
awk Examples
[10/Jul/1995
Sarfraaz Ahmed
Linux Course
awk Examples
awk Examples
Bytes
Sarfraaz Ahmed
Linux Course
awk Examples
Bytes
Sarfraaz Ahmed
Linux Course
awk Examples
john
Number only lines that have some text 1. john
ben
2. ben
3. carl
carl
4. ben
ben
5. john
john
awk 'NF {print $0}' names.txt | awk '{print NR " : " $0}'
Sarfraaz Ahmed
Linux Course
awk Examples
Find the filename with the maximum size
Get rid of
the “total”
line
Sarfraaz Ahmed
Linux Course
awk Examples
Find the file with the maximum size
$ ls -l | awk -f maxlines.awk
-rw-r--r-- 1 apple dev 544 Mar 7 2017 twelve.txt
$
But, fails to print all lines that have same file size !!!
Sarfraaz Ahmed
Linux Course
$ ls -l | awk -f maxlines.awk
-rw-r--r-- 1 apple dev 544 Mar 7 2017 twelve.txt
-rw-r--r-- 1 apple dev 544 Oct 2 22:05 twelve.txt.bkp
$
Sarfraaz Ahmed
Linux Course
THANK YOU
Sarfraaz Ahmed