red week 3 of my pos 420 part B of my assignment done. SEE ATTACHMENT AND ALL ORIGANAL WORK WITH THE BEST QUALITY

mosthated01
labexerciseweek3b.doc

 

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 having an 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 $%.2f\n", $1, $2 * $3) }' emp.dat

Or

 

$ awk '{ printf("%-8s $%6.2f\n", $1, $2 * $3 ) }' emp.dat

 

15. Print all the data sorted in order of increasing pay

$ awk '{ printf("%6.2f %s\n", $2 * $3, $0 ) }' emp.dat

 

16. Print all the data sorted in order of increasing pay web fix

$ awk ' { printf("%6.2f\t%s\t%6.2f\t%d\n", $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 %s\n", $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 stangira@email.phoenix.edu 732-397-4997(CEL)