Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. What would be the effect of each of the following commands if entered at the

ID: 3599039 • Letter: 1

Question

1. What would be the effect of each of the following commands if entered at the Bash prompt?

a) ps –e | grep bash

b) who | grep ^ab1234cd

c) ls –l   | sort –k5

d) date | cut –c 12-19

e) grep ‘^[0-9]’ myFile.txt

The file Donors.txt holds the name, telephone number, street address, birthday, and contribution, separated by ‘:’, for a list of donors to a certain charity. You can download a sample file from the D2L Content page under Data Files.

Give grep commands to do the following.

a)Print all lines containing the string Lane.

b)Print all lines of donors from MN.

c)Print all lines of donors NOT from MN.

d)Print all lines of donors born in October.

e)Print just the birthdays of all donors born in October.

f)Print just the names of all donors with a 408 area code.

Explanation / Answer

Hi,

a) ps –e | grep bash --> ps command is used to view any process which is running in unix. The option -e indicates to view all the processes that are currently running in the system. The command is followed by a grep command which tells the computer to filter the results and search for only those lines which has string "bash" in it.

b) who | grep ^ab1234cd -- > The who command prints information about all users who are currently logged in. The command is followed by a grep command which tells the computer to filter the results and search for only those lines which begins with string ab1234cd.

c) ls –l | sort –k5 --> This first command lists all the files in the current directory in the long format. This means it will disply the list alongwith the owner name, file permissions, creation date and time. The command is followed by a sort command which sorts the list of file names based on fifth column which is "file creation date".

d) date | cut –c 12-19 --> The date command is used to print out, or change the value of, the system's time and date information. The cut command is used to cut the string that is returned from the previous command. The -c option is used to cut specific portion of string. Here, date | cut -c12-19 is used to cut the characters 12 to 19 from the date command output. This will return the timestamp of the file creation date.

e) grep ‘^[0-9]’ myFile.txt --> This command prints out all the lines that begins with a number from file myFile.txt.

Grep commands-

Ans a) grep 'Lane' Donors.txt
Ans b) grep 'MN' Donors.txt
Ans c) grep -v 'MN' Donors.txt
Ans d) grep '8/[0-9][0-9]*' Donors.txt
Ans e) grep '8/[0-9][0-9]*' | sed 's/8/([0-9]*)/([0-9]*)//g' Donors.txt
Ans f) grep '-408-' Donors.txt