SlideShare a Scribd company logo
Introduction to Unix - POS420
Unix Lab Exercise Week 3 B
Topics :
find
grep
sort
uniq
diff
awk
1. find command
1. To find all files havingan extension of .h in the directory
/usr/include
$ find /usr/include -name *.h -print
2. To find all directories recursively (all sub directories also)
$ find . -type d -print
3. To print all the files in the current directory and its
subdirectories
$ find . -print
4. To show files that have not been modified in the last day in
/tmp directory
$ find /tmp -type f -mtime +1
5. To remove the above files
$ find /tmp -type f -mtime +1 -exec rm -f {} ;
or
$ find /tmp -type f -mtime +1 -ok rm -f {} ;
6. When you come out of vacation and If you forget where the
file you were editing, find is very handy (like windows find).
Let us search for the file "first.c" from root directory.
$ find / -name first.c -print
2. grep command
1. Get all the lines with the string -Smith” from the
phonebook:
$ grep Smith phonebook
2. Get all the lines with the string “ Tom Smith” from the
phonebook:
$ grep “Tom Smith” phonebook
3. Get the number of times the string “ksh” occurs in the file
/etc/passwd :
$ grep -c ksh /etc/passwd
4. Get the all the lines having the string “Ken” in all the files
in the directory :
$ grep Ken *
5. Search whether your friend tom has logged in :
$ who | grep tom
6. Get all the lines in the file intro not having the word
“Unix” :
$ grep -v Unix intro
7. Search lower case or upper case pattern "Unix".
$ grep -i Unix intro
8. The -l option gives a list of files having the pattern "Unix".
$ grep -l Unix intro
9. Search for the characters the or The:
$ grep ' [tT]he intro
10. '^' character denotes the beginning of a line and '$' is the
end of a line. Search for lines that start with the word 'The'.
$ grep ^The Unix intro
11. Search for lines that end in .pic
$ grep '.pic$' filename --The escape
sequence  is used for the dot as . is a wild character.
12. Search for containing five-character patterns that start with
a capital letter and end with a digit
$ grep '[A-Z]...[0-9]' filename
13. Th e-n option is used to display the line number that
matched the pattern.
$ grep -n Unix filename
3. sort command
You might have created the file phonebook from Item No 1
above.
1. Sort the file phonebook:
$ sort phonebook
2. Sort the file phonebook by second column :
$ sort +1 phonebook
3. Sort the file by second column but distinct names:
$ sort +1 phonebook | uniq
4. Sort the file and redirect the output to another file called
“sortedusers” :
$ sort phonebook > sortedusers
5. Display the file on the screen , sort and count lines
$ cat phonebook | sort | wc -l
6. Find duplicate entries in /etc/passwd :
$ sort /etc/passwd | uniq -d
4. uniq command
Create a file "myfile" with the following lines
This is first repeated line
Is it the second repeated line ?
This is first repeated line
Is it the second repeated line ?
Last not repeated line
1. Show all lines, with number of repeats counted
$ uniq -c myfile
1 This is first repeated line
1 Is it the second repeated line ?
1 This is first repeated line
1 Is it the second repeated line ?
1 Last not repeated line
Oops, it is now showing correct output, why ?
We need to sort the file before we use uniq command.
Two ways we can do, one by redirecting the output to another
file and then do 'uniq'
1st way :
$ sort myfile > sorted_myfile
$ uniq -d sorted_myfile
2 Is it the second repeated line ?
1 Last not repeated line
2 This is first repeated line
2nd way :
$ sort myfile | uniq -c
2 Is it the second repeated line ?
1 Last not repeated line
2 This is first repeated line
2. Show only duplicated lines
$ sort myfile | uniq -d
Is it the second repeated line ?
This is first repeated line
OR
$ uniq -d sorted_myfile
Is it the second repeated line ?
This is first repeated line
3. Show only unique lines in infile
$ sort myfile | uniq -u
Last not repeated line
OR
$ uniq -u sorted_myfile
Last not repeated line
5. diff command
If you have not created the files file1, file2, please create with
vi command and enter the following contents.
$ vi file1
wendy
ttyp0 Aug 3 18:24 (100.0.0.100)
alexx
ttyp1 Aug 31 14:34 (prokopev203.info)
newuser
ttyp2 Aug 23 23:20 (200.100.56.201)
john
ttyp3 Sep 1 08:05 (jatt.iitb.com)
smithr
ttyp4 Sep 1 09:18 (mnet.net.edu)
root
ttyp5 Aug 9 20:34 (gray.cyberspace.com)
santose
ttyp6 Sep 1 09:54 (www.optimus.com)
sandy
ttyp9 Sep 1 09:36 (140.10.00.00)
jyothika
ttypa Sep 1 09:50 (200.159.180.15)
$ vi file2
Wendy
ttyp0 Aug 3 18:24 (100.0.0.100)
alexx
ttyp1 Aug 31 14:34 (prokopev203.info)
newuser
ttyp2 Aug 23 23:20 (200.100.56.201)
John
ttyp3 Sep 1 08:05 (jatt.iitb.com)
smithr
ttyp4 Sep 1 09:18 (mnet.net.edu)
root
ttyp5 Aug 9 20:34 (gray.cyberspace.com)
santose
ttyp6 Sep 1 09:54 (www.optimus.com)
Sandy
ttyp9 Sep 1 09:36 (140.10.00.00)
jyothika
ttypa Sep 1 09:50 (200.159.180.15)
The lines in the above files looks like same, but diff can find
the differences.
$ diff file1 file2
1c1
< wendy
ttyp0 Aug 3 18:24 (100.0.0.100)
---
> Wendy
ttyp0 Aug 3 18:24 (100.0.0.100)
4c4
< john
ttyp3 Sep 1 08:05 (jatt.iitb.com)
---
> John
ttyp3 Sep 1 08:05 (jatt.iitb.com)
8c8
< sandy
ttyp9 Sep 1 09:36 (140.10.00.00)
---
> Sandy
ttyp9 Sep 1 09:36 (140.10.00.00)
6. awk command
I introduce awk here as it is a powerful tool as well as easy to
learn once we know what awk stands for.
(This part of the lab is optional, but I suggest every one to
practice awk commands).
The awk is a programming language designed to search for,
match patterns, and perform actions on files. awk programs are
generally quite small, and are interpreted. This makes it a good
language for prototyping.
THE STRUCTURE OF AN AWK PROGRAM
awk scans input lines one after the other, searching each line to
see if it matches a set of patterns or conditions specified in the
awk program.
For each pattern, an action is specified. The action is performed
when the pattern matches that of the input line.
Thus, an awk program consists of a number of patterns and
associated actions. Actions are enclosed using curly braces, and
separated using semi-colons.
pattern { action }
pattern { action }
Here are some examples on awk.
1. Create a file emp.data with the following data
$ vi emp.dat
Beth
4.00
0
Dan
3.75
0
Kathy
4.00
10
Mark
5.00
20
Mary
5.50
22
Susie
4.25
18
2. Print the name and pay for everyone who worked
$ awk '$3 > 0 { print $1, $2 * $3 }' emp.dat
3. Print the name of those employees who did not work
$ awk '$3 == 0 { print $1 }' emp.dat
4. Print each line with the pattern match $3 == 0
$ awk '$3 == 0' emp.dat
5. Print an action with no pattern
$ awk '{ print $1 }' emp.dat
6. Error example, this will give error and redirect error to stdout
also
$ awk '$3 == 0 [ print $1 }' emp.dat 2>&1
7. Print every line of input,
$ awk '{ print }' emp.dat
8. Same as above
$ awk '{ print $0 }' emp.dat
9. Printing certain fields
$ awk '{ print $1, $3 }' emp.dat
10. Print the number of fields
$ awk '{ print NF, $1, $NF }' emp.dat
11. Computing and printing
$ awk '{ print $1, $2 * $3 }' emp.dat
12 . Printing line numbers
$ awk '{ print NR, $0 }' emp.dat
13. Putting text in the output
$ awk '{ print "total pay for", $1, "is", $2 * $3 }' emp.dat
14. Using printf to print total pay for every employee
$ awk '{ printf("total pay for %s, is $%.2fn", $1, $2 * $3) }'
emp.dat
Or
$ awk '{ printf("%-8s $%6.2fn", $1, $2 * $3 ) }' emp.dat
15. Print all the data sorted in order of increasing pay
$ awk '{ printf("%6.2f %sn", $2 * $3, $0 ) }' emp.dat
16. Print all the data sorted in order of increasing pay web fix
$ awk ' { printf("%6.2ft%st%6.2ft%dn", $2 * $3, $1, $2, $3)
} ‘ emp.dat | sort
17. Selection by comparison
$ awk '$2 >= 5 ‘ emp.dat
18. Selection by computation
$ awk ' $2 * $3 > 50 { printf ("$%.2f for %sn", $2 * $3, $1) }
‘ emp.dat
19. Selection by text content
$ awk ' $1 == "Susie" ' emp.dat
20 .Selection by text content
$ awk ' /Susie/ ' emp.dat
21. Print those lines where $2 is at least 4 or $3 is at least 20
$ awk ' $2 >= 4 || $3 >= 20 ' emp.dat
22 . This program prints and input line twice if it satisfies both
conditions
$ awk ' $2 >= 4; $3 >= 20 ' emp.dat
or
$ awk ' !($2 < 4 && $3 < 20) ' emp.dat
23. Print the total number of input lines
$ awk '{ }END { print NR }' emp.dat
24. Print the tenth input line
$ awk '{if (NR == 10) { print } }' emp.dat
25. Print the last field of every input line
$ awk '{ print $NF }' emp.dat
27. Print the last field of the last input line
$ awk '{ field = $NF } END { print field }' emp.dat
28. Print every input line in which the last field is more than 4
$ awk '{if (NF > 4) { print } }' emp.dat
29. Print every line in which the last field is more than 4
$ awk '{ if ($NF > 4) { print } }' emp.dat
30. Print the total number of fields in all input lines
$ awk '{ nf = nf + NF } END { print nf }' emp.dat
31. Print the total number of lines that contain Beth
$ awk '{ if (/Beth/) { nlines = nlines + 1 } } END { print nlines
}' emp.dat
32 .Print the largest first field and the line that contains it
(assumes some $1 is positive)
$ awk '{ if ($1 > max) { max = $1; maxline = $0 } } END {
print max, maxline }' emp.dat
33. Print every line that has at least one field
$ awk '{ if (NF > 0) { print } }' emp.dat
34. Print every line longer than 80 characters
$ awk '{ if (length($0) > 80) { print } }' emp.dat
35. Print number of fields in every line followed by the line
itself
$ awk '{ print NF, $0 }' emp.dat
36. Print the first two fields, in opposite order, of every line
awk '{ print $2, $1 }' emp.dat
37. Exchange the first two fields of every line and then print the
line
$ awk '{ temp = $1; $1 = $2; $2 = temp; print }' emp.dat
38. Print every line with the first field replaced by the line
number
$ awk '{ $1 = NR; print }' emp.dat
39. Print every line after erasing the second field
$ awk '{ $2 = ""; print }' emp.dat
Syam Tangirala
University of Phoenix Online Faculty
[email protected]
732-397-4997(CEL)

More Related Content

Similar to Introduction to Unix - POS420Unix  Lab Exercise Week 3 BTo.docx

Perl.predefined.variables
Perl.predefined.variablesPerl.predefined.variables
Perl.predefined.variables
King Hom
 
Unix lab manual
Unix lab manualUnix lab manual
Unix lab manual
Chaitanya Kn
 
Lecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administrationLecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administration
Mohammed Farrag
 
PERL for QA - Important Commands and applications
PERL for QA - Important Commands and applicationsPERL for QA - Important Commands and applications
PERL for QA - Important Commands and applications
Sunil Kumar Gunasekaran
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
Raghu nath
 
Bozorgmeh os lab
Bozorgmeh os labBozorgmeh os lab
Bozorgmeh os lab
FS Karimi
 
Awk essentials
Awk essentialsAwk essentials
Awk essentials
Logan Palanisamy
 
Perl6 one-liners
Perl6 one-linersPerl6 one-liners
Perl6 one-liners
Andrew Shitov
 
Unix Tutorial
Unix TutorialUnix Tutorial
Unix Tutorial
Sanjay Saluth
 
32 shell-programming
32 shell-programming32 shell-programming
32 shell-programming
kayalkarnan
 
Unix 5 en
Unix 5 enUnix 5 en
Unix 5 en
Simonas Kareiva
 
Mips1
Mips1Mips1
Hex file and regex cheat sheet
Hex file and regex cheat sheetHex file and regex cheat sheet
Hex file and regex cheat sheet
Martin Cabrera
 
Cheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regexCheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regex
Kasper de Waard
 
awkbash quick ref for Red hat Linux admin
awkbash quick ref for Red hat Linux adminawkbash quick ref for Red hat Linux admin
awkbash quick ref for Red hat Linux admin
ZoumanaDiomande1
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdf
srxerox
 
Unix Basic Commands
Unix Basic CommandsUnix Basic Commands
Unix Basic Commands
varshagokhale2
 
Examples -partII
Examples -partIIExamples -partII
Examples -partII
Kedar Bhandari
 
Five
FiveFive
Shell实现的windows回收站功能的脚本
Shell实现的windows回收站功能的脚本Shell实现的windows回收站功能的脚本
Shell实现的windows回收站功能的脚本
Lingfei Kong
 

Similar to Introduction to Unix - POS420Unix  Lab Exercise Week 3 BTo.docx (20)

Perl.predefined.variables
Perl.predefined.variablesPerl.predefined.variables
Perl.predefined.variables
 
Unix lab manual
Unix lab manualUnix lab manual
Unix lab manual
 
Lecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administrationLecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administration
 
PERL for QA - Important Commands and applications
PERL for QA - Important Commands and applicationsPERL for QA - Important Commands and applications
PERL for QA - Important Commands and applications
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Bozorgmeh os lab
Bozorgmeh os labBozorgmeh os lab
Bozorgmeh os lab
 
Awk essentials
Awk essentialsAwk essentials
Awk essentials
 
Perl6 one-liners
Perl6 one-linersPerl6 one-liners
Perl6 one-liners
 
Unix Tutorial
Unix TutorialUnix Tutorial
Unix Tutorial
 
32 shell-programming
32 shell-programming32 shell-programming
32 shell-programming
 
Unix 5 en
Unix 5 enUnix 5 en
Unix 5 en
 
Mips1
Mips1Mips1
Mips1
 
Hex file and regex cheat sheet
Hex file and regex cheat sheetHex file and regex cheat sheet
Hex file and regex cheat sheet
 
Cheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regexCheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regex
 
awkbash quick ref for Red hat Linux admin
awkbash quick ref for Red hat Linux adminawkbash quick ref for Red hat Linux admin
awkbash quick ref for Red hat Linux admin
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdf
 
Unix Basic Commands
Unix Basic CommandsUnix Basic Commands
Unix Basic Commands
 
Examples -partII
Examples -partIIExamples -partII
Examples -partII
 
Five
FiveFive
Five
 
Shell实现的windows回收站功能的脚本
Shell实现的windows回收站功能的脚本Shell实现的windows回收站功能的脚本
Shell实现的windows回收站功能的脚本
 

More from mariuse18nolet

IRM 3305 Risk Management Theory and PracticeFall 2014Proje.docx
IRM 3305 Risk Management Theory and PracticeFall 2014Proje.docxIRM 3305 Risk Management Theory and PracticeFall 2014Proje.docx
IRM 3305 Risk Management Theory and PracticeFall 2014Proje.docx
mariuse18nolet
 
Ironwood Company manufactures cast-iron barbeque cookware. During .docx
Ironwood Company manufactures cast-iron barbeque cookware. During .docxIronwood Company manufactures cast-iron barbeque cookware. During .docx
Ironwood Company manufactures cast-iron barbeque cookware. During .docx
mariuse18nolet
 
IRM 3305 Risk Management Theory and PracticeGroup Project.docx
IRM 3305 Risk Management Theory and PracticeGroup Project.docxIRM 3305 Risk Management Theory and PracticeGroup Project.docx
IRM 3305 Risk Management Theory and PracticeGroup Project.docx
mariuse18nolet
 
Iranian Women and GenderRelations in Los AngelesNAYEREH .docx
Iranian Women and GenderRelations in Los AngelesNAYEREH .docxIranian Women and GenderRelations in Los AngelesNAYEREH .docx
Iranian Women and GenderRelations in Los AngelesNAYEREH .docx
mariuse18nolet
 
IRB HANDBOOK IRB A-Z Handbook E.docx
IRB HANDBOOK IRB A-Z Handbook  E.docxIRB HANDBOOK IRB A-Z Handbook  E.docx
IRB HANDBOOK IRB A-Z Handbook E.docx
mariuse18nolet
 
IQuiz # II-Emerson QuizGeneral For Emerson, truth (or.docx
IQuiz # II-Emerson QuizGeneral For Emerson, truth (or.docxIQuiz # II-Emerson QuizGeneral For Emerson, truth (or.docx
IQuiz # II-Emerson QuizGeneral For Emerson, truth (or.docx
mariuse18nolet
 
iPython 2For Beginners OnlyVersion 1.0Matthew .docx
iPython 2For Beginners OnlyVersion 1.0Matthew .docxiPython 2For Beginners OnlyVersion 1.0Matthew .docx
iPython 2For Beginners OnlyVersion 1.0Matthew .docx
mariuse18nolet
 
Iranian Journal of Military Medicine Spring 2011, Volume 13, .docx
Iranian Journal of Military Medicine  Spring 2011, Volume 13, .docxIranian Journal of Military Medicine  Spring 2011, Volume 13, .docx
Iranian Journal of Military Medicine Spring 2011, Volume 13, .docx
mariuse18nolet
 
IoT Referenceshttpswww.techrepublic.comarticlehow-to-secur.docx
IoT Referenceshttpswww.techrepublic.comarticlehow-to-secur.docxIoT Referenceshttpswww.techrepublic.comarticlehow-to-secur.docx
IoT Referenceshttpswww.techrepublic.comarticlehow-to-secur.docx
mariuse18nolet
 
IP Subnet Design Project- ONLY QUALITY ASSIGNMENTS AND 0 PLAG.docx
IP Subnet Design Project- ONLY QUALITY ASSIGNMENTS AND 0 PLAG.docxIP Subnet Design Project- ONLY QUALITY ASSIGNMENTS AND 0 PLAG.docx
IP Subnet Design Project- ONLY QUALITY ASSIGNMENTS AND 0 PLAG.docx
mariuse18nolet
 
IranAyatollahTheocracyTwelver ShiismVilayat-e Faghih (jur.docx
IranAyatollahTheocracyTwelver ShiismVilayat-e Faghih (jur.docxIranAyatollahTheocracyTwelver ShiismVilayat-e Faghih (jur.docx
IranAyatollahTheocracyTwelver ShiismVilayat-e Faghih (jur.docx
mariuse18nolet
 
ipopulation monitoring in radiation emergencies a gui.docx
ipopulation monitoring in radiation emergencies a gui.docxipopulation monitoring in radiation emergencies a gui.docx
ipopulation monitoring in radiation emergencies a gui.docx
mariuse18nolet
 
In Innovation as Usual How to Help Your People Bring Great Ideas .docx
In Innovation as Usual How to Help Your People Bring Great Ideas .docxIn Innovation as Usual How to Help Your People Bring Great Ideas .docx
In Innovation as Usual How to Help Your People Bring Great Ideas .docx
mariuse18nolet
 
Investor’s Business Daily – Investors.comBloomberg Business – Blo.docx
Investor’s Business Daily –  Investors.comBloomberg Business – Blo.docxInvestor’s Business Daily –  Investors.comBloomberg Business – Blo.docx
Investor’s Business Daily – Investors.comBloomberg Business – Blo.docx
mariuse18nolet
 
Invitation to Public Speaking, Fifth EditionChapter 8 Introdu.docx
Invitation to Public Speaking, Fifth EditionChapter 8 Introdu.docxInvitation to Public Speaking, Fifth EditionChapter 8 Introdu.docx
Invitation to Public Speaking, Fifth EditionChapter 8 Introdu.docx
mariuse18nolet
 
Invitation to the Life SpanRead chapters 13 and 14.Objectives.docx
Invitation to the Life SpanRead chapters 13 and 14.Objectives.docxInvitation to the Life SpanRead chapters 13 and 14.Objectives.docx
Invitation to the Life SpanRead chapters 13 and 14.Objectives.docx
mariuse18nolet
 
IOBOARD Week 2 Lab BPage 2 of 4Name _________________ Gr.docx
IOBOARD Week 2 Lab BPage 2 of 4Name _________________ Gr.docxIOBOARD Week 2 Lab BPage 2 of 4Name _________________ Gr.docx
IOBOARD Week 2 Lab BPage 2 of 4Name _________________ Gr.docx
mariuse18nolet
 
INVITATION TO Computer Science 1 1 Chapter 17 Making .docx
INVITATION TO  Computer Science 1 1 Chapter 17 Making .docxINVITATION TO  Computer Science 1 1 Chapter 17 Making .docx
INVITATION TO Computer Science 1 1 Chapter 17 Making .docx
mariuse18nolet
 
Investment Analysis & Portfolio Management AD 717 OLHomework E.docx
Investment Analysis & Portfolio Management AD 717 OLHomework E.docxInvestment Analysis & Portfolio Management AD 717 OLHomework E.docx
Investment Analysis & Portfolio Management AD 717 OLHomework E.docx
mariuse18nolet
 
Investment BAFI 1042 Kevin Dorr 3195598 GOODMAN .docx
Investment BAFI 1042  Kevin Dorr 3195598  GOODMAN .docxInvestment BAFI 1042  Kevin Dorr 3195598  GOODMAN .docx
Investment BAFI 1042 Kevin Dorr 3195598 GOODMAN .docx
mariuse18nolet
 

More from mariuse18nolet (20)

IRM 3305 Risk Management Theory and PracticeFall 2014Proje.docx
IRM 3305 Risk Management Theory and PracticeFall 2014Proje.docxIRM 3305 Risk Management Theory and PracticeFall 2014Proje.docx
IRM 3305 Risk Management Theory and PracticeFall 2014Proje.docx
 
Ironwood Company manufactures cast-iron barbeque cookware. During .docx
Ironwood Company manufactures cast-iron barbeque cookware. During .docxIronwood Company manufactures cast-iron barbeque cookware. During .docx
Ironwood Company manufactures cast-iron barbeque cookware. During .docx
 
IRM 3305 Risk Management Theory and PracticeGroup Project.docx
IRM 3305 Risk Management Theory and PracticeGroup Project.docxIRM 3305 Risk Management Theory and PracticeGroup Project.docx
IRM 3305 Risk Management Theory and PracticeGroup Project.docx
 
Iranian Women and GenderRelations in Los AngelesNAYEREH .docx
Iranian Women and GenderRelations in Los AngelesNAYEREH .docxIranian Women and GenderRelations in Los AngelesNAYEREH .docx
Iranian Women and GenderRelations in Los AngelesNAYEREH .docx
 
IRB HANDBOOK IRB A-Z Handbook E.docx
IRB HANDBOOK IRB A-Z Handbook  E.docxIRB HANDBOOK IRB A-Z Handbook  E.docx
IRB HANDBOOK IRB A-Z Handbook E.docx
 
IQuiz # II-Emerson QuizGeneral For Emerson, truth (or.docx
IQuiz # II-Emerson QuizGeneral For Emerson, truth (or.docxIQuiz # II-Emerson QuizGeneral For Emerson, truth (or.docx
IQuiz # II-Emerson QuizGeneral For Emerson, truth (or.docx
 
iPython 2For Beginners OnlyVersion 1.0Matthew .docx
iPython 2For Beginners OnlyVersion 1.0Matthew .docxiPython 2For Beginners OnlyVersion 1.0Matthew .docx
iPython 2For Beginners OnlyVersion 1.0Matthew .docx
 
Iranian Journal of Military Medicine Spring 2011, Volume 13, .docx
Iranian Journal of Military Medicine  Spring 2011, Volume 13, .docxIranian Journal of Military Medicine  Spring 2011, Volume 13, .docx
Iranian Journal of Military Medicine Spring 2011, Volume 13, .docx
 
IoT Referenceshttpswww.techrepublic.comarticlehow-to-secur.docx
IoT Referenceshttpswww.techrepublic.comarticlehow-to-secur.docxIoT Referenceshttpswww.techrepublic.comarticlehow-to-secur.docx
IoT Referenceshttpswww.techrepublic.comarticlehow-to-secur.docx
 
IP Subnet Design Project- ONLY QUALITY ASSIGNMENTS AND 0 PLAG.docx
IP Subnet Design Project- ONLY QUALITY ASSIGNMENTS AND 0 PLAG.docxIP Subnet Design Project- ONLY QUALITY ASSIGNMENTS AND 0 PLAG.docx
IP Subnet Design Project- ONLY QUALITY ASSIGNMENTS AND 0 PLAG.docx
 
IranAyatollahTheocracyTwelver ShiismVilayat-e Faghih (jur.docx
IranAyatollahTheocracyTwelver ShiismVilayat-e Faghih (jur.docxIranAyatollahTheocracyTwelver ShiismVilayat-e Faghih (jur.docx
IranAyatollahTheocracyTwelver ShiismVilayat-e Faghih (jur.docx
 
ipopulation monitoring in radiation emergencies a gui.docx
ipopulation monitoring in radiation emergencies a gui.docxipopulation monitoring in radiation emergencies a gui.docx
ipopulation monitoring in radiation emergencies a gui.docx
 
In Innovation as Usual How to Help Your People Bring Great Ideas .docx
In Innovation as Usual How to Help Your People Bring Great Ideas .docxIn Innovation as Usual How to Help Your People Bring Great Ideas .docx
In Innovation as Usual How to Help Your People Bring Great Ideas .docx
 
Investor’s Business Daily – Investors.comBloomberg Business – Blo.docx
Investor’s Business Daily –  Investors.comBloomberg Business – Blo.docxInvestor’s Business Daily –  Investors.comBloomberg Business – Blo.docx
Investor’s Business Daily – Investors.comBloomberg Business – Blo.docx
 
Invitation to Public Speaking, Fifth EditionChapter 8 Introdu.docx
Invitation to Public Speaking, Fifth EditionChapter 8 Introdu.docxInvitation to Public Speaking, Fifth EditionChapter 8 Introdu.docx
Invitation to Public Speaking, Fifth EditionChapter 8 Introdu.docx
 
Invitation to the Life SpanRead chapters 13 and 14.Objectives.docx
Invitation to the Life SpanRead chapters 13 and 14.Objectives.docxInvitation to the Life SpanRead chapters 13 and 14.Objectives.docx
Invitation to the Life SpanRead chapters 13 and 14.Objectives.docx
 
IOBOARD Week 2 Lab BPage 2 of 4Name _________________ Gr.docx
IOBOARD Week 2 Lab BPage 2 of 4Name _________________ Gr.docxIOBOARD Week 2 Lab BPage 2 of 4Name _________________ Gr.docx
IOBOARD Week 2 Lab BPage 2 of 4Name _________________ Gr.docx
 
INVITATION TO Computer Science 1 1 Chapter 17 Making .docx
INVITATION TO  Computer Science 1 1 Chapter 17 Making .docxINVITATION TO  Computer Science 1 1 Chapter 17 Making .docx
INVITATION TO Computer Science 1 1 Chapter 17 Making .docx
 
Investment Analysis & Portfolio Management AD 717 OLHomework E.docx
Investment Analysis & Portfolio Management AD 717 OLHomework E.docxInvestment Analysis & Portfolio Management AD 717 OLHomework E.docx
Investment Analysis & Portfolio Management AD 717 OLHomework E.docx
 
Investment BAFI 1042 Kevin Dorr 3195598 GOODMAN .docx
Investment BAFI 1042  Kevin Dorr 3195598  GOODMAN .docxInvestment BAFI 1042  Kevin Dorr 3195598  GOODMAN .docx
Investment BAFI 1042 Kevin Dorr 3195598 GOODMAN .docx
 

Recently uploaded

BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 

Recently uploaded (20)

BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 

Introduction to Unix - POS420Unix  Lab Exercise Week 3 BTo.docx

  • 1. Introduction to Unix - POS420 Unix Lab Exercise Week 3 B Topics : find grep sort uniq diff awk 1. find command 1. To find all files havingan extension of .h in the directory /usr/include $ find /usr/include -name *.h -print 2. To find all directories recursively (all sub directories also) $ find . -type d -print 3. To print all the files in the current directory and its subdirectories $ find . -print
  • 2. 4. To show files that have not been modified in the last day in /tmp directory $ find /tmp -type f -mtime +1 5. To remove the above files $ find /tmp -type f -mtime +1 -exec rm -f {} ; or $ find /tmp -type f -mtime +1 -ok rm -f {} ; 6. When you come out of vacation and If you forget where the file you were editing, find is very handy (like windows find). Let us search for the file "first.c" from root directory. $ find / -name first.c -print 2. grep command 1. Get all the lines with the string -Smith” from the phonebook: $ grep Smith phonebook 2. Get all the lines with the string “ Tom Smith” from the phonebook: $ grep “Tom Smith” phonebook 3. Get the number of times the string “ksh” occurs in the file /etc/passwd : $ grep -c ksh /etc/passwd 4. Get the all the lines having the string “Ken” in all the files
  • 3. in the directory : $ grep Ken * 5. Search whether your friend tom has logged in : $ who | grep tom 6. Get all the lines in the file intro not having the word “Unix” : $ grep -v Unix intro 7. Search lower case or upper case pattern "Unix". $ grep -i Unix intro 8. The -l option gives a list of files having the pattern "Unix". $ grep -l Unix intro 9. Search for the characters the or The: $ grep ' [tT]he intro 10. '^' character denotes the beginning of a line and '$' is the end of a line. Search for lines that start with the word 'The'. $ grep ^The Unix intro 11. Search for lines that end in .pic $ grep '.pic$' filename --The escape sequence is used for the dot as . is a wild character. 12. Search for containing five-character patterns that start with a capital letter and end with a digit $ grep '[A-Z]...[0-9]' filename 13. Th e-n option is used to display the line number that matched the pattern. $ grep -n Unix filename 3. sort command You might have created the file phonebook from Item No 1 above. 1. Sort the file phonebook: $ sort phonebook 2. Sort the file phonebook by second column : $ sort +1 phonebook 3. Sort the file by second column but distinct names: $ sort +1 phonebook | uniq 4. Sort the file and redirect the output to another file called “sortedusers” :
  • 4. $ sort phonebook > sortedusers 5. Display the file on the screen , sort and count lines $ cat phonebook | sort | wc -l 6. Find duplicate entries in /etc/passwd : $ sort /etc/passwd | uniq -d 4. uniq command Create a file "myfile" with the following lines This is first repeated line Is it the second repeated line ? This is first repeated line Is it the second repeated line ? Last not repeated line 1. Show all lines, with number of repeats counted $ uniq -c myfile 1 This is first repeated line 1 Is it the second repeated line ? 1 This is first repeated line 1 Is it the second repeated line ? 1 Last not repeated line Oops, it is now showing correct output, why ? We need to sort the file before we use uniq command. Two ways we can do, one by redirecting the output to another file and then do 'uniq' 1st way : $ sort myfile > sorted_myfile $ uniq -d sorted_myfile 2 Is it the second repeated line ? 1 Last not repeated line 2 This is first repeated line 2nd way : $ sort myfile | uniq -c 2 Is it the second repeated line ? 1 Last not repeated line 2 This is first repeated line
  • 5. 2. Show only duplicated lines $ sort myfile | uniq -d Is it the second repeated line ? This is first repeated line OR $ uniq -d sorted_myfile Is it the second repeated line ? This is first repeated line 3. Show only unique lines in infile $ sort myfile | uniq -u Last not repeated line OR $ uniq -u sorted_myfile Last not repeated line 5. diff command If you have not created the files file1, file2, please create with vi command and enter the following contents. $ vi file1 wendy ttyp0 Aug 3 18:24 (100.0.0.100) alexx ttyp1 Aug 31 14:34 (prokopev203.info) newuser ttyp2 Aug 23 23:20 (200.100.56.201) john ttyp3 Sep 1 08:05 (jatt.iitb.com) smithr ttyp4 Sep 1 09:18 (mnet.net.edu) root ttyp5 Aug 9 20:34 (gray.cyberspace.com) santose ttyp6 Sep 1 09:54 (www.optimus.com) sandy ttyp9 Sep 1 09:36 (140.10.00.00) jyothika
  • 6. ttypa Sep 1 09:50 (200.159.180.15) $ vi file2 Wendy ttyp0 Aug 3 18:24 (100.0.0.100) alexx ttyp1 Aug 31 14:34 (prokopev203.info) newuser ttyp2 Aug 23 23:20 (200.100.56.201) John ttyp3 Sep 1 08:05 (jatt.iitb.com) smithr ttyp4 Sep 1 09:18 (mnet.net.edu) root ttyp5 Aug 9 20:34 (gray.cyberspace.com) santose ttyp6 Sep 1 09:54 (www.optimus.com) Sandy ttyp9 Sep 1 09:36 (140.10.00.00) jyothika ttypa Sep 1 09:50 (200.159.180.15) The lines in the above files looks like same, but diff can find the differences. $ diff file1 file2 1c1 < wendy ttyp0 Aug 3 18:24 (100.0.0.100) --- > Wendy ttyp0 Aug 3 18:24 (100.0.0.100) 4c4 < john ttyp3 Sep 1 08:05 (jatt.iitb.com)
  • 7. --- > John ttyp3 Sep 1 08:05 (jatt.iitb.com) 8c8 < sandy ttyp9 Sep 1 09:36 (140.10.00.00) --- > Sandy ttyp9 Sep 1 09:36 (140.10.00.00) 6. awk command I introduce awk here as it is a powerful tool as well as easy to learn once we know what awk stands for. (This part of the lab is optional, but I suggest every one to practice awk commands). The awk is a programming language designed to search for, match patterns, and perform actions on files. awk programs are generally quite small, and are interpreted. This makes it a good language for prototyping. THE STRUCTURE OF AN AWK PROGRAM awk scans input lines one after the other, searching each line to see if it matches a set of patterns or conditions specified in the awk program. For each pattern, an action is specified. The action is performed when the pattern matches that of the input line. Thus, an awk program consists of a number of patterns and
  • 8. associated actions. Actions are enclosed using curly braces, and separated using semi-colons. pattern { action } pattern { action } Here are some examples on awk. 1. Create a file emp.data with the following data $ vi emp.dat Beth 4.00 0 Dan 3.75 0 Kathy 4.00 10 Mark 5.00 20 Mary 5.50 22 Susie 4.25 18 2. Print the name and pay for everyone who worked $ awk '$3 > 0 { print $1, $2 * $3 }' emp.dat 3. Print the name of those employees who did not work $ awk '$3 == 0 { print $1 }' emp.dat
  • 9. 4. Print each line with the pattern match $3 == 0 $ awk '$3 == 0' emp.dat 5. Print an action with no pattern $ awk '{ print $1 }' emp.dat 6. Error example, this will give error and redirect error to stdout also $ awk '$3 == 0 [ print $1 }' emp.dat 2>&1 7. Print every line of input, $ awk '{ print }' emp.dat 8. Same as above $ awk '{ print $0 }' emp.dat 9. Printing certain fields $ awk '{ print $1, $3 }' emp.dat 10. Print the number of fields $ awk '{ print NF, $1, $NF }' emp.dat 11. Computing and printing $ awk '{ print $1, $2 * $3 }' emp.dat 12 . Printing line numbers $ awk '{ print NR, $0 }' emp.dat 13. Putting text in the output $ awk '{ print "total pay for", $1, "is", $2 * $3 }' emp.dat 14. Using printf to print total pay for every employee $ awk '{ printf("total pay for %s, is $%.2fn", $1, $2 * $3) }' emp.dat Or
  • 10. $ awk '{ printf("%-8s $%6.2fn", $1, $2 * $3 ) }' emp.dat 15. Print all the data sorted in order of increasing pay $ awk '{ printf("%6.2f %sn", $2 * $3, $0 ) }' emp.dat 16. Print all the data sorted in order of increasing pay web fix $ awk ' { printf("%6.2ft%st%6.2ft%dn", $2 * $3, $1, $2, $3) } ‘ emp.dat | sort 17. Selection by comparison $ awk '$2 >= 5 ‘ emp.dat 18. Selection by computation $ awk ' $2 * $3 > 50 { printf ("$%.2f for %sn", $2 * $3, $1) } ‘ emp.dat 19. Selection by text content $ awk ' $1 == "Susie" ' emp.dat 20 .Selection by text content $ awk ' /Susie/ ' emp.dat 21. Print those lines where $2 is at least 4 or $3 is at least 20 $ awk ' $2 >= 4 || $3 >= 20 ' emp.dat 22 . This program prints and input line twice if it satisfies both conditions $ awk ' $2 >= 4; $3 >= 20 ' emp.dat or $ awk ' !($2 < 4 && $3 < 20) ' emp.dat 23. Print the total number of input lines $ awk '{ }END { print NR }' emp.dat 24. Print the tenth input line $ awk '{if (NR == 10) { print } }' emp.dat
  • 11. 25. Print the last field of every input line $ awk '{ print $NF }' emp.dat 27. Print the last field of the last input line $ awk '{ field = $NF } END { print field }' emp.dat 28. Print every input line in which the last field is more than 4 $ awk '{if (NF > 4) { print } }' emp.dat 29. Print every line in which the last field is more than 4 $ awk '{ if ($NF > 4) { print } }' emp.dat 30. Print the total number of fields in all input lines $ awk '{ nf = nf + NF } END { print nf }' emp.dat 31. Print the total number of lines that contain Beth $ awk '{ if (/Beth/) { nlines = nlines + 1 } } END { print nlines }' emp.dat 32 .Print the largest first field and the line that contains it (assumes some $1 is positive) $ awk '{ if ($1 > max) { max = $1; maxline = $0 } } END { print max, maxline }' emp.dat 33. Print every line that has at least one field $ awk '{ if (NF > 0) { print } }' emp.dat 34. Print every line longer than 80 characters $ awk '{ if (length($0) > 80) { print } }' emp.dat 35. Print number of fields in every line followed by the line itself $ awk '{ print NF, $0 }' emp.dat 36. Print the first two fields, in opposite order, of every line
  • 12. awk '{ print $2, $1 }' emp.dat 37. Exchange the first two fields of every line and then print the line $ awk '{ temp = $1; $1 = $2; $2 = temp; print }' emp.dat 38. Print every line with the first field replaced by the line number $ awk '{ $1 = NR; print }' emp.dat 39. Print every line after erasing the second field $ awk '{ $2 = ""; print }' emp.dat Syam Tangirala University of Phoenix Online Faculty [email protected] 732-397-4997(CEL)