Solved

Self training exercise

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
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…

623 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