All in Unix 1. How do you select from a file (i) lines 5 to 10, (ii) second-to-l
ID: 3814711 • Letter: A
Question
All in Unix
1. How do you select from a file (i) lines 5 to 10, (ii) second-to-last line?
2. How do you extract the names of the users from /etc/passwd after ignoring the
first 10 entries?
3. How do you display a list of all processes without the ps header line where
processes with the same name are grouped together?
4. List from /etc/passwd the UID and the user having the highest UID
5. What is the difference between a wild card and a regular expression?
6. How do these expressions differ? (i) [0-9]* and [0-9][0-9]*,
(ii) ^[^^] and ^^^.
7. How do you delete all leading and trailing spaces in all lines?
Explanation / Answer
1) sed -n 5,10p file location/name - prints lines between given range
tail -n+2 filename - prints lines from 2nd to end of file
2) tail -n+11 /etc/passwd | cut -d: -f1 - prints names of users from 11th line to end of given file
4) cat /etc/passwd | awk -F: '{print $3,$1}' | sort -n | tail -n 1 - prints the user having highest UID including user name
5) Difference between wildcard and regular expressions:
wildcard - it is a charecter used in regular expressions to increase the flexibility and search efficiency.
examples: *,.,+,?,[]
regular expressions - these are a pattern matching system that uses wildcards and strings constructed according to pre-defined syntax rules to find desired strings in text.
examples:
ls *.html - lists all files end with extension html
6) awk '{$1=$1};1' < file name - prints the file with trimmed leading and trailing whitespaces.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.