LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 02-25-2011, 01:05 AM   #1
mmhs
Member
 
Registered: Oct 2010
Posts: 99

Rep: Reputation: 1
match number


hey guys i gave simple question in bash programming

i want to write shell script that give a port number and find it in /etc/services but i want to find exact match number

for example user enter 4 and script find exact 4 in /etc/services not contain 444 or 45 or 23124 and ...

(just say how can i find match number ) with grep or sed or awk .....

and how can i find exact match string in a file

for example user enter Por and script find just Por string not Port or Port1 or Por-23 or ...


thx
 
Old 02-25-2011, 01:27 AM   #2
r_s
Member
 
Registered: May 2009
Distribution: slackware, fedora, ubuntu
Posts: 94

Rep: Reputation: 21
grep has an option for exact matches -w.
 
Old 02-25-2011, 02:22 AM   #3
mmhs
Member
 
Registered: Oct 2010
Posts: 99

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by r_s View Post
grep has an option for exact matches -w.
)

grep -w only find word

for example if you have abd and abd-12 and abd.12 if you use grep -w "abd" file it find all of abd abd-12 and abd.12

plz some expert people in bash script help me
 
Old 02-25-2011, 02:55 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

@mmhs: You need to find something that makes the search string unique for the specific infile you are using.

The following will return a specific port from /etc/services:
Code:
grep " 4/" /etc/services
As you can see the wanted string (4) is enclosed by a space (might be a tab, my /etc/services file uses spaces) and a slash, which makes it unique for this specific infile.

Hope this helps.
 
Old 02-25-2011, 03:00 AM   #5
mmhs
Member
 
Registered: Oct 2010
Posts: 99

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by druuna View Post
Hi,

@mmhs: You need to find something that makes the search string unique for the specific infile you are using.

The following will return a specific port from /etc/services:
Code:
grep " 4/" /etc/services
As you can see the wanted string (4) is enclosed by a space (might be a tab, my /etc/services file uses spaces) and a slash, which makes it unique for this specific infile.

Hope this helps.
) check it man u mistake it's wrong 100% it will return nothing

plz some expert man help me and check your solution before post
 
Old 02-25-2011, 03:08 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
@mmhs:

Works nicely on my side:

Code:
$ grep " 4/" /etc/services 
#                 4/tcp    # Unassigned
#                 4/udp    # Unassigned

$ grep " 80/" /etc/services 
http             80/tcp    # World Wide Web HTTP
http             80/udp    # World Wide Web HTTP
- Did you try changing the space to a tab (as mentioned in my previous post)?
- Are you actually grepping something in /etc/services?
- Did you try to integrate this in an already existing script?

Please provide more detailed info.

Quote:
plz some expert man help me and check your solution before post
Don't be condescending!
 
Old 02-25-2011, 03:10 AM   #7
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
There is already a program that does this. `getent'. It looks up entries for various databases such as /etc/services, /etc/hosts.

>getent services 4
echo 4/ddp

You can use it to for example test if domain names are being resolved properly:
>getent hosts elite
192.168.1.106 elite.jesnet elite desktop
>getent hosts www.google.com
getent hosts elite
192.168.1.106 elite.jesnet elite desktop
> getent hosts www.google.com
74.125.229.17 www.l.google.com www.google.com
74.125.229.19 www.l.google.com www.google.com
74.125.229.16 www.l.google.com www.google.com
74.125.229.18 www.l.google.com www.google.com
74.125.229.20 www.l.google.com www.google.com

Last edited by jschiwal; 02-25-2011 at 03:12 AM.
 
1 members found this post helpful.
Old 02-25-2011, 03:18 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
@jschiwal: Doesn't getent only give a result if the service is actually active and only tcp?

Code:
$ getent services 4
$ getent services 80
http                  80/tcp

$ grep " 4/" /etc/services 
#                 4/tcp    # Unassigned
#                 4/udp    # Unassigned

$ grep " 80/" /etc/services 
http             80/tcp    # World Wide Web HTTP
http             80/udp    # World Wide Web HTTP
 
Old 02-25-2011, 03:55 AM   #9
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
I was in the middle of coding this when I saw:
Quote:
Originally Posted by mmhs
check it man u mistake it's wrong 100% it will return nothing

plz some expert man help me and check your solution before post
I'm not a big fan of complaining about possible solutions when you have not provided anything to start with; even though you say it's a "simple question in bash programming"

So, this code is half-finished, and I intend to leave it that way.

Code:
#!/bin/bash

rawPort=""
while [ -z "${rawPort}" ] ; do
  echo -n "Enter port: "
  read rawPort
done

let validatePort=rawPort+0

if [ "${rawPort}x" = "${validatePort}x" ] ; then
  numericPort=1
else
  numericPort=0
fi

if [ ${numericPort} -eq 1 ] ; then
  lineNumbers=$( sed 's@/@ @g' /etc/services | \
                 awk "{if(\$2==\"${validatePort}\") print NR}" )
  for lineNumber in ${lineNumbers} ; do
     sed -n "${lineNumber}p" /etc/services
  done
else
  echo "unsupported"
fi
Like I said: half finished. Does it work 100% of the time? I don't know. Am I going to test it any further? Nope.
 
  


Reply

Tags
bash, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular expression to match unspecified number of characters until a '>' cygnal Linux - General 7 07-15-2010 01:21 PM
why sector number not match block number? bitzsk Linux - Kernel 1 06-09-2009 06:32 AM
LXer: Revamped Perl Script To More Evenly Distribute Number Pool Match Odds LXer Syndicated Linux News 0 09-17-2008 02:30 PM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 09:55 AM
Regular expression to match "^" then a number? PsychosisNode Linux - Newbie 1 01-14-2007 10:26 AM


All times are GMT -5. The time now is 11:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration