![]() |
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 |
grep has an option for exact matches -w.
|
Quote:
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 :( |
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/servicesHope this helps. |
Quote:
plz some expert man help me and check your solution before post |
@mmhs:
Works nicely on my side: Code:
$ grep " 4/" /etc/services - 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:
|
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 |
@jschiwal: Doesn't getent only give a result if the service is actually active and only tcp?
Code:
$ getent services 4 |
I was in the middle of coding this when I saw:
Quote:
So, this code is half-finished, and I intend to leave it that way. Code:
#!/bin/bash |
| All times are GMT -5. The time now is 01:26 PM. |