SlideShare a Scribd company logo
1 of 12
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.variablesKing Hom
 
Lecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administrationLecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administrationMohammed 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 applicationsSunil Kumar Gunasekaran
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
Bozorgmeh os lab
Bozorgmeh os labBozorgmeh os lab
Bozorgmeh os labFS Karimi
 
32 shell-programming
32 shell-programming32 shell-programming
32 shell-programmingkayalkarnan
 
Cheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regexCheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regexKasper de Waard
 
Hex file and regex cheat sheet
Hex file and regex cheat sheetHex file and regex cheat sheet
Hex file and regex cheat sheetMartin Cabrera
 
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 adminZoumanaDiomande1
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdfsrxerox
 
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
 
Cheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regexCheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regex
 
Hex file and regex cheat sheet
Hex file and regex cheat sheetHex file and regex cheat sheet
Hex file and regex cheat sheet
 
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.docxmariuse18nolet
 
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 .docxmariuse18nolet
 
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.docxmariuse18nolet
 
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 .docxmariuse18nolet
 
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.docxmariuse18nolet
 
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.docxmariuse18nolet
 
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 .docxmariuse18nolet
 
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, .docxmariuse18nolet
 
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.docxmariuse18nolet
 
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.docxmariuse18nolet
 
IranAyatollahTheocracyTwelver ShiismVilayat-e Faghih (jur.docx
IranAyatollahTheocracyTwelver ShiismVilayat-e Faghih (jur.docxIranAyatollahTheocracyTwelver ShiismVilayat-e Faghih (jur.docx
IranAyatollahTheocracyTwelver ShiismVilayat-e Faghih (jur.docxmariuse18nolet
 
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.docxmariuse18nolet
 
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 .docxmariuse18nolet
 
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.docxmariuse18nolet
 
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.docxmariuse18nolet
 
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.docxmariuse18nolet
 
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.docxmariuse18nolet
 
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 .docxmariuse18nolet
 
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.docxmariuse18nolet
 
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 .docxmariuse18nolet
 

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

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 

Recently uploaded (20)

TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 

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)