Matching text using grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching text using grep
# 8  
Old 07-03-2012
Sorry for the lack info/clarity...

Basically the subnet file will contain a list of subnets where the first to values (111.222 in this case) will not change so entries could be as follows:

111.222.xxx.x
or
111.222.xx.x
or
111.222.xxx.xx

etc etc.

So for example if the subnet was 111.222.15.0 (and making the assumption that there is also a .115 subnet present) a grep will return both:

111.222.15.0 and 111.222.115.0

What I'm struggling to do is pick out the .15.0 subnet only


NOTE - 111.222.333 is just being used as examples Smilie
# 9  
Old 07-03-2012
Quote:
Originally Posted by CiCa
Sorry for the lack info/clarity...

Basically the subnet file will contain a list of subnets where the first to values (111.222 in this case) will not change so entries could be as follows:

111.222.xxx.x
or
111.222.xx.x
or
111.222.xxx.xx

etc etc.

So for example if the subnet was 111.222.15.0 (and making the assumption that there is also a .115 subnet present) a grep will return both:

111.222.15.0 and 111.222.115.0

What I'm struggling to do is pick out the .15.0 subnet only


NOTE - 111.222.333 is just being used as examples Smilie
what is your operating system and grep version ?
# 10  
Old 07-03-2012
Solaris 10 and grep is SUNWggrp 11.10.0.

Also I'm trying to write the above code in ksh
# 11  
Old 07-03-2012
Quote:
Originally Posted by CiCa
Solaris 10 and grep is SUNWggrp 11.10.0.

Also I'm trying to write the above code in ksh
i try some but result seem like correct..
maybe let check the this in ksh and bash in your script

Code:
..........
read sub

echo "`ps -ef|grep $PPID|grep -v grep`"
echo "SHEBANG : `/usr/xpg4/bin/sed -n '1s/^#!/&/p' $0`"
echo "grep : `which grep`"
echo "sub value : $sub"

...........

what is the outputs ?
# 12  
Old 07-03-2012
Try:
Code:
grep '\.15\.0$'

The dot has a special meaning in regex and needs to be escaped...
# 13  
Old 07-03-2012
Output:

Please enter the subnet to be changed: 111.222.333.0
root 3765 3755 0 Jun 29 pts/2 0:02 -bash
root 20968 3765 0 13:31:46 pts/2 0:00 /bin/ksh ./generate-batchlist
SHEBANG : #!/bin/ksh
grep : /usr/bin/grep
sub value : 111.222.333.0
0

---------- Post updated at 01:37 PM ---------- Previous update was at 01:35 PM ----------

Scruitinizer - that would be fine, although the problem is, the subnet address is read in via the $sub variable and won't always be .15
# 14  
Old 07-03-2012
Quote:
Originally Posted by CiCa
[..]
Scruitinizer - that would be fine, although the problem is, the subnet address is read in via the $sub variable and won't always be .15
Then try:
Code:
grep "${sub//./\\.}$"

in bash / ksh93
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matching and where, using grep

May somebody can give me a hint. I want to find using the command "grep" a certain word or term in a foo.txt file. By using the following command grep -i word file1 > newfile4 it puts it into a new foo.txt-file, the n times it matches. Fine, it matches n times, but how could I specify where... (2 Replies)
Discussion started by: 1in10
2 Replies

2. Shell Programming and Scripting

Grep with ... matching more than 3 characters

I am trying to understand what the grep command in ubuntu is trying to do here. The contents of my test file is given below harsha@harsha-H67MA-USB3-B3:~/Documents$ cat data abcd efghi jklmno pqr stuv wxyz When I grep for 3 dots (...) without the parenthesis as follows I would expect the... (4 Replies)
Discussion started by: sreeharshasn
4 Replies

3. UNIX for Dummies Questions & Answers

Regex matching with grep -l

I am trying to find patterns in files using grep -l -e. I specifically am searching for abc. I want any file that has abc in it, but not just the letters abc. I am searching for a pattern a followed by b followed by c. I have tried egrep -l and also I have tried the following: grep -el... (2 Replies)
Discussion started by: newbie2010
2 Replies

4. Shell Programming and Scripting

Grep negative matching

is it possible to reverse the output of grep -o (9 Replies)
Discussion started by: squrcles
9 Replies

5. Shell Programming and Scripting

Grep -v (inverse matching)

I am totally unexperienced in writing scripts of any kind. I am working on Mac OS X and would like to run a shell script to find files in a directory that do not conform to a specific naming convention and print to a text file in the same directory. For example, I have a folder called... (9 Replies)
Discussion started by: j_alicea
9 Replies

6. Shell Programming and Scripting

Grep text matching problem with script which checks if web page contains text.

I wrote a Bash script which checks to see if a text string exists on a web page and then sends me an email if it does (or does not e.g. "Out of stock"). I run it from my crontab, it's quite handy from time to time and I've been using it for a few years now. The script uses wget to download an... (6 Replies)
Discussion started by: gencon
6 Replies

7. Shell Programming and Scripting

Grep the non-matching lines

Hi, I need to make a script to extract the number that are not in a file. Example: I have file-A that has 100000 (70000-799999) numbers. And a file-B with number that already are in the system. Now I need to know/get the numbers that are not in system. I was thinking something like this:... (5 Replies)
Discussion started by: AK47
5 Replies

8. UNIX for Dummies Questions & Answers

Issues while pattern matching using grep

Hi, I have a file f1 wi the following data f1.txt ======== Report ID Report Name ----------------------------------------------------------------- Post Requests : 2 Post successes : 2 ============================================= I need to search for the... (2 Replies)
Discussion started by: RP09
2 Replies

9. Shell Programming and Scripting

Matching white space through Grep

Hello All, I am trying to match white space in patterns through - Grep I tried ] & ] but none of them worked. Then I tried Perl extension '\s' and it worked. So just wanted to know if ] & ] are still supported or have they become deprecated. However they have been mentioned in the... (3 Replies)
Discussion started by: paragkalra
3 Replies

10. UNIX for Advanced & Expert Users

Grep Line with Matching Fields

Below is the scenario. Help is appreciated. File1: ( 500,000 lines ) : Three fields comma delimited : Not sorted 1234FAA,435612,88975 1224FAB,12345,212356 File2: ( 4,000,000 lines ) : Six fields comma delimited (Last 3 field should match the 3 fields of File1) : Not Sorted : ... (13 Replies)
Discussion started by: hemangjani
13 Replies
Login or Register to Ask a Question