Answer the following questions about the cut and tr commands (UNIX): a) If you h
ID: 3605727 • Letter: A
Question
Answer the following questions about the cut and tr commands (UNIX):
a) If you have a file called ‘data.csv’ with several lines of data, each line containing comma-separated values, give the AWK command to print off the 8th 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).
b) Give the cut command for accomplishing the same thing that you did with AWK in part (a).
c) If you have a file called ‘records’ with many lines of data, each line having values separated by a ‘:’ character, how would you obtain the 5th value of each line using the cut command?
d) Assume the same things from part (c), 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.
e) Assume you want to read as input the file ‘names.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 to a temporary file named ‘tempfile’.
Explanation / Answer
a) If you have a file called ‘data.csv’ with several lines of data, each line containing comma-separated values, give the AWK command to print off the 8th 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).
awk -F "," '{print $8}' data.csv #will print the 8th column as we have given , as delimiter assuming number to be column.
b) Give the cut command for accomplishing the same thing that you did with AWK in part (a).
c) If you have a file called ‘records’ with many lines of data, each line having values separated by a ‘:’ character, how would you obtain the 5th value of each line using the cut command?
cut -d: -f5 data.csv #replacing : with ", " similar to last example.
d) Assume the same things from part (c), 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
cut -d, -f5 data.csv | cut -c3 #We will get data of the fifth field and then piping it anf getting the 3rd character in it.
e) Assume you want to read as input the file ‘names.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?
cat names.txt | tr [:lower:] [:upper:] | tr "#" "-" > tempfile
According to the forum rules we are only suppose to answer 4 sub questions not 5, but i have made an exception for the same.
Thanks for asking the question, Please let me know if you have any queries will be glad to help