Solved

Self training exercise

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

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

Suggested Solutions

Title # Comments Views Activity
EXCHANGE 2007, SCRIPTS 67 82
listing unique file prefixes in a directory Linux 1 65
Powershell Script need assistance to make some changes 4 92
AWK: Pythagoras script 6 23
This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
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…
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…

830 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