Use cat, paste, cut commands and stdin, stdout, or stderr redirection as needed
ID: 3677017 • Letter: U
Question
Use cat, paste, cut commands and stdin, stdout, or stderr redirection as needed to answer the following questions.
1. Create a file called fullNames.txt using vi and have the following content in the file -- Please pay attention to put space character between the first and last name for each person below. (hint for the upcoming questions: the default delimeter for the paste and cut commands is TAB character not space character).
john smith
alice wonderland
sam blue
2. Write a unix command to print first name of each person in the fullNames.txt file to the screen.
3. Write a unix command to create a file called firstNames.txt from the fullNames.txt file. This file should have the first name of each person per line.
4. Write a unix command to print last name of each person in the fullNames.txt file to the screen
5. Write a unix command to create a file called lastNames.txt from the fullNames.txt file. This file should have the last name of each person per line.
6. Use a unix command to print the combined firstNames.txt and lastNames.txt files to the screen in a way that each line has the first and las name of one person delimited using : symbol. For instance, the first line to appear on the screen should be
john:smith
7. Modify your unix command in step 6 so that the lines are written to another file called newFullNames.txt rather than on the screen.
8. Write a unix command to append the line
mike:brown
to the end of your newFullNames.txt file
Explanation / Answer
Answered questions 1 to 5
1. Create a file called fullNames.txt using vi
Ans: vi fullNames.tx
then enter the data, then click esc button. Now save using wq command
2)
Here f1 is the first field after splitting string using delimiter
cut -d ' ' -f1 fullNames.txt
3)
cut -d ' ' -f1 fullNames.txt > firstNames.txt
4)
Here f2 is the second field (i.e last name) after splitting string using delimiter
cut -d ' ' -f2 fullNames.txt
5)
cut -d ' ' -f2 fullNames.txt > lastNames.txt
--output----
m
bash-4.3$ cut -d ' ' -f1 fullNames.txt > firstNames.txt
bash-4.3$ cat firstNames.txt
john
alice
sam
bash-4.3$ ^C
bash-4.3$ cut -d ' ' -f2 fullNames.txt
smith
wonderland
blue
bash-4.3$ cut -d ' ' -f2 fullNames.txt > lastNames.txt
bash-4.3$ cat lastNames.txt
smith
wonderland
blue
bash-4.3$ ^C
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.