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

a) If you have a file called ‘saved_data.txt’ with many lines of data, each line

ID: 3816797 • Letter: A

Question

a) If you have a file called ‘saved_data.txt’ with many lines of data, each line having values separated by a ‘$’ character, how would you obtain the 7th value/field of each line using the cut command?

b) Assume the same things from part (a), except now you want to obtain the 3rd character of the 5th value on each line. How would you do this using the cut command? HINT: You may use piping here.

c) If you have a file called ‘records.csv’ with several lines of data, each line containing comma-separated values, give the AWK command to print off the 5th number on each line (you have to tell AWK to use a comma as the separator using the -F flag, so look up this flag to see how to use it properly).

d) Give the cut command for accomplishing the same thing that you did with AWK in part (c).

e) Assume you want to read as input the file ‘passwords.txt’, change every lowercase letter to an uppercase letter and every ‘$’ to a ‘#’, then save the result back to that same file (overwriting it). How would you do this with the tr command? HINT: Use piping and file redirection.

Explanation / Answer

a:   cut -f 7 -d '$' saved_data.txt

cut command is used to get particular section of data from the input given to it. input can be a file or a string or output from another command when passed to it. (using pipes)

-f option specifies the field of information required. Here we need the 7th field,, so -f 7

-d is used to specify the delimiter. If this option is not used, default delimiter TAB will be considered. Here, our required delimiter is $, so -d '$'. Note: delimiter caan be a single character in quotes

b: cut -f 5 -d '$' saved_data.txt | cut -c 3

I assume that the first part of the above command is clear. We are taking the 5th field in the saved_data.txt file which has a delimiter $ between each field.

Now we have piped the output from fisrt command to the second one. The second command receives 5th field from each line as input. We need the third character of 5th field.

-c <N> gives the Nth character in each line counted from 1.

So, why did we have to pipe this? the options -b (which returns Nth byte), -c and -f are mutually exclusive. Only one of these three can be used in a single cut command.

c: awk -F ',' '{print $5}' records.csv

awk command is basically an implementation of GNU AWK programming language. This is a regular expression parser. It is something similar to grep, something more like an advanced version of it.

Here, out requirement is to get the 5th field of data on each line using awk command.

Like in cut, even in awk we have on option to separate fields if the delimiter is not the standard TAB. The option is

-F. -F '<delimiter>' specifies which delimiter should be used to separate fields. Now we have specified our delimiter using -F ','

We need to print the 5th field using this command. Each field is stored in $n where n is the number of the field. We need the fifth field, so it is $5. We print it to the output using print so, {print $5}. We put this in {}, because flower brackets tells the command that this is the action that needs to be done.

d: cut -f 5 -d ',' records.csv

It is same as the command in Question a, except for a different delimiter.

e: tr [a-z$] [A-Z#] < passwords.txt > temp.txt | mv temp.txt passwords.txt

tr command replaces the first set of characters with the second set of characters. tr accepts input only from standard input i.e., console.

Here we want to convert all lowercase characters to uppercase characters and $ to #. So, in the first set we specify the characters needed to be converted [a-z$] and in the second set, we specify characters to which the first set needs to be converted to [A-Z#].

Since tr accepts only input from standard input, we use < input redirection operator to send input to tr from our file. We use > output redirection operator to send output from tr to a file temp.txt because we can't read from and write to the same file at the same time in one command.

We pipe this this with mv command to move the temp.txt file to our required file.

Hope everything is clear. Cheers!