Solved

Self training exercise

Posted on 2011-03-25
3
483 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Utilizing an array to gracefully append to a list of EmailAddresses
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

710 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