You are on page 1of 13

K. J.

Somaiya College of Engineering, Mumbai-77


(An Autonomous College Affiliated to University of Mumbai)

Batch: B4 Roll No.: 1913121


Experiment / assignment / tutorial No._10.
Grade: AA / AB / BB / BC / CC / CD /DD

Signature of the Staff In-charge with date

TITLE: SED ,AWK,GREP commands


AIM: Shell scripts command on SED,AWk,GREP

OUTCOME: Student will be able to perform basic operating system tasks using command-
line.

Shell scripts commands on awk. Shell script programs on awk builtin functions like len,
Toupper, etc. Shell scripts to perform various operations on given strings along with
grep command and sed command.

AWK:

Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 2021 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)

Code-
Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB
$ mkdir os10

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB


$ cd os10

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ cat>employee.txt
akash manager account 45555
raj head sales 77522
priya clerk sales 25475
sunil peon account 65488
raju director mange 99992

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ awk '{print}' employee.txt
akash manager account 45555
raj head sales 77522
priya clerk sales 25475
sunil peon account 65488
raju director mange 99992

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ awk '/manager/{print}' employee.txt
akash manager account 45555

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 2021 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)
$ awk '{print $1,$5}' employee.txt
akash
raj
priya
sunil
raju

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ awk '{print $1,$2}' employee.txt
akash manager
raj head
priya clerk
sunil peon
raju director

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ awk '{print NR,$0}' employee.txt
1 akash manager account 45555
2 raj head sales 77522
3 priya clerk sales 25475
4 sunil peon account 65488
5 raju director mange 99992

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ awk '{ if($1 ~ /a/) print}' employee.txt
akash manager account 45555
raj head sales 77522
priya clerk sales 25475
raju director mange 99992

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ awk '/[0-6]/ {print }' employee.txt
akash manager account 45555
raj head sales 77522
priya clerk sales 25475
sunil peon account 65488
raju director mange 99992

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ awk '{print NR,$1}' employee.txt
1 akash
2 raj
3 priya
4 sunil
5 raju

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ awk '{print NR,$2}' employee.txt
1 manager
2 head
3 clerk
4 peon
5 director

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ awk '{print $1,$NF}' employee.txt

akash 45555
raj 77522
priya 25475
sunil 65488
raju 99992

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ awk 'NR==3, NR==6 {print NR,$0}' employee.txt
3 priya clerk sales 25475
4 sunil peon account 65488
5 raju director mange 99992

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 2021 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)
$ awk 'BEGIN{sum=0}{sum=sum+$4}END {print sum}' employee.txt
314032

Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 2021 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)

Execute all SED buildin commands which we have completed in lab session.
SED:

Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 2021 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)

Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 2021 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ cat>seddemo.txt
A001|Anirban Roy Choudhary|computers|95|Pass|8000 A002|Ankit
Pilania|electronics|98|Pass|8900 C003|Ankit Saraf|civil|32|Fail|2000
S004|Goutam Kumar|chemical|77|Pass|7700 A005|Mani Chowdhari|electrical|23|Fail|2000
X006|Mohan Kumar|computers|67|Incomplete|0 W007|Sandeep|civil|2|Fail|0
A008|Yogesh|computers|91|Pass|8000
S009|Yojna Chaudhary|electronics|38|Incomplete|1000
A010|Zubin|computers|12|Fail|1500
C011|Mira Nair|electronics|66|Pass|7000 X012|Meera Banik|civil|76|Pass|6300
W013|Meera Roy|civil|41|Pass|4000

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ sed '2p' seddemo.txt
A001|Anirban Roy Choudhary|computers|95|Pass|8000 A002|Ankit
Pilania|electronics|98|Pass|8900 C003|Ankit Saraf|civil|32|Fail|2000
S004|Goutam Kumar|chemical|77|Pass|7700 A005|Mani Chowdhari|electrical|23|Fail|2000
X006|Mohan Kumar|computers|67|Incomplete|0 W007|Sandeep|civil|2|Fail|0
A008|Yogesh|computers|91|Pass|8000
S004|Goutam Kumar|chemical|77|Pass|7700 A005|Mani Chowdhari|electrical|23|Fail|2000
X006|Mohan Kumar|computers|67|Incomplete|0 W007|Sandeep|civil|2|Fail|0
A008|Yogesh|computers|91|Pass|8000
S009|Yojna Chaudhary|electronics|38|Incomplete|1000
A010|Zubin|computers|12|Fail|1500
C011|Mira Nair|electronics|66|Pass|7000 X012|Meera Banik|civil|76|Pass|6300
W013|Meera Roy|civil|41|Pass|4000

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ sed -n '2p' seddemo.txt
S004|Goutam Kumar|chemical|77|Pass|7700 A005|Mani Chowdhari|electrical|23|Fail|2000
X006|Mohan Kumar|computers|67|Incomplete|0 W007|Sandeep|civil|2|Fail|0
A008|Yogesh|computers|91|Pass|8000

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10
$ sed -n '3,6p' seddemo.txt
S009|Yojna Chaudhary|electronics|38|Incomplete|1000
A010|Zubin|computers|12|Fail|1500
C011|Mira Nair|electronics|66|Pass|7000 X012|Meera Banik|civil|76|Pass|6300
W013|Meera Roy|civil|41|Pass|4000

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ sed -n '3,6!p' seddemo.txt
A001|Anirban Roy Choudhary|computers|95|Pass|8000 A002|Ankit
Pilania|electronics|98|Pass|8900 C003|Ankit Saraf|civil|32|Fail|2000
Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10Chowdhari|electrical|23|Fail|2000
X006|Mohan Kumar|computers|67|Incomplete|0 W007|Sandeep|civil|2|Fail|0
A008|Yogesh|computers|91|Pass|8000
$ sed -n '/[cC]omputers/p' seddemo.txt
A001|Anirban Roy Choudhary|computers|95|Pass|8000 A002|Ankit
Pilania|electronics|98|Pass|8900 C003|Ankit Saraf|civil|32|Fail|2000
S004|Goutam Kumar|chemical|77|Pass|7700 A005|Mani Chowdhari|electrical|23|Fail|2000
X006|Mohan Kumar|computers|67|Incomplete|0 W007|Sandeep|civil|2|Fail|0
A008|Yogesh|computers|91|Pass|8000
S009|Yojna Chaudhary|electronics|38|Incomplete|1000
A010|Zubin|computers|12|Fail|1500

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ sed -n'/[cC]omputers/w computer_student.txt' seddemo.txt
sed: unknown option -- /
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

-n, --quiet, --silent


suppress automatic printing of pattern space
--debug
annotate program execution
-e script, --expression=script
add the script to the commands to be executed
-f script-file, --file=script-file
Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 2021 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)
add the contents of script-file to the commands to be executed
--follow-symlinks
follow symlinks when processing in place
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)
-b, --binary
open files in binary mode (CR+LFs are not processed specially)
-l N, --line-length=N
specify the desired line-wrap length for the `l' command
--posix
disable all GNU extensions.
-E, -r, --regexp-extended
use extended regular expressions in the script
(for portability use POSIX -E).
-s, --separate
consider files as separate rather than as a single,
continuous long stream.
--sandbox
operate in sandbox mode (disable e/r/w commands).
-u, --unbuffered
load minimal amounts of data from the input files and flush
the output buffers more often
-z, --null-data
separate lines by NUL characters
--help display this help and exit
--version output version information and exit
If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret. All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

GNU sed home page: <https://www.gnu.org/software/sed/>.


General help using GNU software: <https://www.gnu.org/gethelp/>.

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ sed -n '/[cC]omputers/w computer_student.txt' seddemo.txt

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ cat computer_student.txt
A001|Anirban Roy Choudhary|computers|95|Pass|8000 A002|Ankit
Pilania|electronics|98|Pass|8900 C003|Ankit Saraf|civil|32|Fail|2000
S004|Goutam Kumar|chemical|77|Pass|7700 A005|Mani Chowdhari|electrical|23|Fail|2000
X006|Mohan Kumar|computers|67|Incomplete|0 W007|Sandeep|civil|2|Fail|0
A008|Yogesh|computers|91|Pass|8000
S009|Yojna Chaudhary|electronics|38|Incomplete|1000
A010|Zubin|computers|12|Fail|1500

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ cat computer_student.txt
A001|Anirban Roy Choudhary|computers|95|Pass|8000 A002|Ankit
Pilania|electronics|98|Pass|8900 C003|Ankit Saraf|civil|32|Fail|2000
S004|Goutam Kumar|chemical|77|Pass|7700 A005|Mani Chowdhari|electrical|23|Fail|2000
X006|Mohan Kumar|computers|67|Incomplete|0 W007|Sandeep|civil|2|Fail|0
A008|Yogesh|computers|91|Pass|8000
S009|Yojna Chaudhary|electronics|38|Incomplete|1000
A010|Zubin|computers|12|Fail|1500

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ sed -n -e '/electronics/w electro.txt' -e '/civil/w civil.txt' seddemo.txt

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ cat electro.txt
A001|Anirban Roy Choudhary|computers|95|Pass|8000 A002|Ankit
Pilania|electronics|98|Pass|8900 C003|Ankit Saraf|civil|32|Fail|2000
S009|Yojna Chaudhary|electronics|38|Incomplete|1000
A010|Zubin|computers|12|Fail|1500
C011|Mira Nair|electronics|66|Pass|7000 X012|Meera Banik|civil|76|Pass|6300
W013|Meera Roy|civil|41|Pass|4000

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ cat civil.txt

Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 2021 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)
A001|Anirban Roy Choudhary|computers|95|Pass|8000 A002|Ankit
Pilania|electronics|98|Pass|8900 C003|Ankit Saraf|civil|32|Fail|2000
S004|Goutam Kumar|chemical|77|Pass|7700 A005|Mani Chowdhari|electrical|23|Fail|2000
X006|Mohan Kumar|computers|67|Incomplete|0 W007|Sandeep|civil|2|Fail|0
A008|Yogesh|computers|91|Pass|8000
C011|Mira Nair|electronics|66|Pass|7000 X012|Meera Banik|civil|76|Pass|6300
W013|Meera Roy|civil|41|Pass|4000

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ sed 's/[kK]umar/Roy/' seddemo.txt
A001|Anirban Roy Choudhary|computers|95|Pass|8000 A002|Ankit
Pilania|electronics|98|Pass|8900 C003|Ankit Saraf|civil|32|Fail|2000
S004|Goutam Roy|chemical|77|Pass|7700 A005|Mani Chowdhari|electrical|23|Fail|2000
X006|Mohan Kumar|computers|67|Incomplete|0 W007|Sandeep|civil|2|Fail|0
A008|Yogesh|computers|91|Pass|8000
S009|Yojna Chaudhary|electronics|38|Incomplete|1000
A010|Zubin|computers|12|Fail|1500
C011|Mira Nair|electronics|66|Pass|7000 X012|Meera Banik|civil|76|Pass|6300
W013|Meera Roy|civil|41|Pass|4000

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ sed 's/[kK]umar/Roy/g' seddemo.txt
A001|Anirban Roy Choudhary|computers|95|Pass|8000 A002|Ankit
Pilania|electronics|98|Pass|8900 C003|Ankit Saraf|civil|32|Fail|2000
S004|Goutam Roy|chemical|77|Pass|7700 A005|Mani Chowdhari|electrical|23|Fail|2000
X006|Mohan Roy|computers|67|Incomplete|0 W007|Sandeep|civil|2|Fail|0
A008|Yogesh|computers|91|Pass|8000
S009|Yojna Chaudhary|electronics|38|Incomplete|1000
A010|Zubin|computers|12|Fail|1500
C011|Mira Nair|electronics|66|Pass|7000 X012|Meera Banik|civil|76|Pass|6300
W013|Meera Roy|civil|41|Pass|4000

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ sed -e 's/electronics/electrical/g' -e 's/civil/metallurgy/g' seddemo.txt
A001|Anirban Roy Choudhary|computers|95|Pass|8000 A002|Ankit
Pilania|electrical|98|Pass|8900 C003|Ankit Saraf|metallurgy|32|Fail|2000
S004|Goutam Kumar|chemical|77|Pass|7700 A005|Mani Chowdhari|electrical|23|Fail|2000
X006|Mohan Kumar|computers|67|Incomplete|0 W007|Sandeep|metallurgy|2|Fail|0
A008|Yogesh|computers|91|Pass|8000
S009|Yojna Chaudhary|electrical|38|Incomplete|1000 A010|Zubin|computers|12|Fail|1500
C011|Mira Nair|electrical|66|Pass|7000 X012|Meera Banik|metallurgy|76|Pass|6300
W013|Meera Roy|metallurgy|41|Pass|4000

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ sed '/Anirban/s/computers/mathematics/g' seddemo.txt
A001|Anirban Roy Choudhary|mathematics|95|Pass|8000 A002|Ankit
Pilania|electronics|98|Pass|8900 C003|Ankit Saraf|civil|32|Fail|2000
S004|Goutam Kumar|chemical|77|Pass|7700 A005|Mani Chowdhari|electrical|23|Fail|2000
X006|Mohan Kumar|computers|67|Incomplete|0 W007|Sandeep|civil|2|Fail|0
A008|Yogesh|computers|91|Pass|8000
S009|Yojna Chaudhary|electronics|38|Incomplete|1000
A010|Zubin|computers|12|Fail|1500
C011|Mira Nair|electronics|66|Pass|7000 X012|Meera Banik|civil|76|Pass|6300
W013|Meera Roy|civil|41|Pass|4000

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ sed '/electronics/d' seddemo.txt>nonelectronics.txt

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ cat nonelectronics.txt
S004|Goutam Kumar|chemical|77|Pass|7700 A005|Mani Chowdhari|electrical|23|Fail|2000
X006|Mohan Kumar|computers|67|Incomplete|0 W007|Sandeep|civil|2|Fail|0
A008|Yogesh|computers|91|Pass|8000

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ sed 'li Student Information' seddemo.txt
sed: -e expression #1, char 2: extra characters after command

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ sed 'l i Student Information' seddemo.txt
sed: -e expression #1, char 3: extra characters after command

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 2021 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)
$ sed '1i Student Information' seddemo.txt
Student Information
A001|Anirban Roy Choudhary|computers|95|Pass|8000 A002|Ankit
Pilania|electronics|98|Pass|8900 C003|Ankit Saraf|civil|32|Fail|2000
S004|Goutam Kumar|chemical|77|Pass|7700 A005|Mani Chowdhari|electrical|23|Fail|2000
X006|Mohan Kumar|computers|67|Incomplete|0 W007|Sandeep|civil|2|Fail|0
A008|Yogesh|computers|91|Pass|8000
S009|Yojna Chaudhary|electronics|38|Incomplete|1000
A010|Zubin|computers|12|Fail|1500
C011|Mira Nair|electronics|66|Pass|7000 X012|Meera Banik|civil|76|Pass|6300
W013|Meera Roy|civil|41|Pass|4000

Dell@DESKTOP-U8877B2 MINGW64 ~/Desktop/OS LAB/os10


$ sed '1i Student Information\n2013' seddemo.txt
Student Information
2013
A001|Anirban Roy Choudhary|computers|95|Pass|8000 A002|Ankit
Pilania|electronics|98|Pass|8900 C003|Ankit Saraf|civil|32|Fail|2000
S004|Goutam Kumar|chemical|77|Pass|7700 A005|Mani Chowdhari|electrical|23|Fail|2000
X006|Mohan Kumar|computers|67|Incomplete|0 W007|Sandeep|civil|2|Fail|0
A008|Yogesh|computers|91|Pass|8000
S009|Yojna Chaudhary|electronics|38|Incomplete|1000
A010|Zubin|computers|12|Fail|1500
C011|Mira Nair|electronics|66|Pass|7000 X012|Meera Banik|civil|76|Pass|6300
W013|Meera Roy|civil|41|Pass|4000

Signature of faculty in-charge

Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 2021 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)

Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 2021 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)

Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 2021 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)

Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 2021 Page No

You might also like