Solved

Self training exercise

Posted on 2011-03-25
3
456 Views
Last Modified: 2012-06-27
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:
a.
grep http /etc/services

Open in new window

b.
grep -v '#' | less

Open in new window

c and d. No idea so far.

Thanks for expert's advice.
0
Comment
Question by:zhshqzyc
3 Comments
 
LVL 14

Accepted Solution

by:
svgmuc earned 125 total points
ID: 35215345
b: Don't forget the file name!

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

c:

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

d:


grep -v '#' /etc/services | grep -v '^$' | sed -e 's/\/.*$//' | less
0
 
LVL 19

Expert Comment

by:simon3270
ID: 35215571
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.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 35219962
To condense it...

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

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now