Unix question. How do I perform the following commands? TASK 1: Display all line
ID: 3725795 • Letter: U
Question
Unix question.
How do I perform the following commands?
TASK 1:
Display all lines in famous.dat that end in 0 or 1, and have a 1 in the 3rd from the last column. For example lines ending in 120 and 131 would be displayed.
TASK 2:
Create a one-line command, using he famous.dat file, to add the word " STREET" to the address, for all who live on 2nd or 3rd.
For example: '2nd' becomes "2nd STREET" and '3rd' becomes "3rd STREET".
Display only the first 9 lines.
Hint: use 2 sed statements with pipes.
Task 3
Display all lines in famous.dat that have only 4, 5, 6, 7 and 9 in the zip code. HINT: this is the same as NOT 0,1,2,3 or 8.
Task 4
Using only the grep command, search all files with names that end in either .html or .htm under the $HOME/public_html directory and display any lines that contain body tags (containing '<body>'). No other output should be displayed even if there is no public_html directory.
Hint: use two command arguments and 2>
Explanation / Answer
1. grep '1.[01]$' famous.dat
In this command regular exp 1.[01]$ is used as follows: $ represents end of the line and [01] characters inside [] represents any of these characters 0/1 can take the last place in the line. a '.' before this represents that at second last position anything can be placed and a '1' before this represents that third last position should always be taken by 1.
2. sed -i 's/2nd/2nd STREET/g' famous.dat| sed -i 's/3rd/3rd STREET/g' famous.dat | head -9 famous.dat
First sed command replaces all the occurences of 2nd with 2nd STREET and then second sed command replaces all the occurences of 3rd with 3rd STREET. head command is used to display first 9 lines of the file.
3.
Assuming zipcode is in the last column and is of 5 characters wide. So we want to display lines which have only 4,5,6,7,9 in the last 6 characters.
grep '[45679][45679][45679][45679][45679]$' famous.dat
4. grep -nri --include *.html --include *.html "<body>" $HOME/public_html
This command will search under directory $HOME/public_html recursively, all the files with extensions .htm or .html and will display any lines that contain <body> tag.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.