Self training exercise

Hi, I have a self-traing exercise. Help will be appreciated.
1. a. Use grep to find information about the HTTP protocol in the file /etc/services.
b. Usually this file contains some comments, starting with the ‘#’ symbol. Use grep with the -v option to ignore lines starting with ‘#’ and look at the rest of the file in less.
c. Add another use of grep -v to your pipeline to remove blank lines (which match the pattern ˆ$).
d. Use sed (also in the same pipeline) to remove the information after the ‘/’ symbol on each line, leaving just the names of the protocols and their port numbers.

Open in new window

My solution:
grep http /etc/services

Open in new window

grep -v '#' | less

Open in new window

c and d. No idea so far.

Thanks for expert's advice.
Who is Participating?
svgmucConnect With a Mentor Commented:
b: Don't forget the file name!

grep -v '#' /etc/services | less


grep -v '#' /etc/services | grep -v '^$' | less


grep -v '#' /etc/services | grep -v '^$' | sed -e 's/\/.*$//' | less
you are allowed to have a comment one the same line as an entry in the file, for example:
    tcpmux          1/tcp                           # TCP port service multiplexer

so to get rid of the comment part, you could have:
  grep -v '^[ ]*#'
to exclude any lines where '' is the first non-space character on the line.

an alternative would be to use sed to remove comments:
  sed 's/[ ]*#.*$//' /etc/services | grep -v '^$'
the 'sed' removes the # and any following characters, and the grep removes any empty lines.
To condense it...

egrep -v "^#|^$" /etc/services | sed s#/.*## | less

Open in new window

All Courses

From novice to tech pro — start learning today.