Solved

Self training exercise

Posted on 2011-03-25
3
455 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
This video discusses moving either the default database or any database to a new volume.

705 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

21 Experts available now in Live!

Get 1:1 Help Now