Need help in Regular expression in bash shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in Regular expression in bash shell
# 8  
Old 07-26-2010
@Zaxxon,

I want to match exactly XX.XX.XX.XX only 4 sets.

@ygemici,

The one which you gave is also giving other lines.

Code:
ex2.sh

echo "Hai........"
abdfkljk 110.111.111.11 kjdkjjkjdk
11.11.1.1111
3.23.AS.AA
24.DD.DD.DD
ab.cd.df.rf
/usr/bin
jhhh 12.12.AV.12	
abcdjhds /usrwww/bin/hhhhk kdjkdjkdj
/urf/sss/kk kkk

Code:
grep -rn '[0-9a-fA-F][0-9a-fA-F].\{4\}'  ex2.sh

1:echo "Hai........"
2:abdfkljk 110.111.111.11 kjdkjjkjdk
3:11.11.1.1111
4:3.23.AS.AA
5:24.DD.DD.DD
6:ab.cd.df.rf
8:jhhh 12.12.AV.12
10:abcdjhds /usrwww/bin/hhhhk kdjkdjkdj

The result should be only

5:24.DD.DD.DD
8:jhhh 12.12.AV.12
# 9  
Old 07-26-2010
Code:
# grep -rn '[0-9a-f][0-9a-f]\.[0-9a-f][0-9a-f]\.[0-9a-f][0-9a-f]\.' macfile
2:here is a mac address 00.30.05.be.c5.bc ok?
5:another 00.30.05.be.c5.b1 ok?



---------- Post updated at 04:57 PM ---------- Previous update was at 04:53 PM ----------

Code:
 
# grep -rn '[0-9A-F][0-9A-F]\.[0-9A-F][0-9A-F]\.[0-9A-F][0-9A-F]\.' ex2.sh
5:24.DD.DD.DD

8:jhhh 12.12.AV.12

V is not a hexadecimal character Smilie

Last edited by ygemici; 07-26-2010 at 11:03 AM..
# 10  
Old 07-26-2010
Sorry ..........



If suppose ex2.sh file content is



Code:
 

echo "Hai........"
abdfkljk 110.111.111.11 kjdkjjkjdk
11.11.1.1111
3.23.AS.AA
24.DD.DD.DDD
24.DD.DD
ab.cd.df.rf
/usr/bin
jhhh 12.12.AF.12



Output is :

Code:
5:24.DD.DD.DDD
9:jhhh 12.12.AF.12


Whereas output should be

Code:
9:jhhh 12.12.AF.12

# 11  
Old 07-26-2010
Code:
grep -ni '[^0-9A-F][0-9A-F]\{1,2\}\.[0-9A-F]\{1,2\}\.[0-9A-F]\{1,2\}\.[0-9A-F]\{1,2\}' infile
9:jhhh 12.12.AF.12

# 12  
Old 07-26-2010
Code:
 
# grep -rn '[0-9A-F][0-9A-F]\.[0-9A-F][0-9A-F]\.[0-9A-F][0-9A-F]\.' ex2.sh
5:24.DD.DD.DD
8:jhhh 12.12.AF.12



---------- Post updated at 05:23 PM ---------- Previous update was at 05:19 PM ----------

Code:
# grep -rn '[0-9a-fA-F][0-9a-fA-F]\.[0-9a-fA-F][0-9a-fA-F]\.[0-9a-fA-F][0-9a-fA-F]\.[0-9a-fA-F][0-9a-fA-F]$' ex2.sh
9:jhhh 12.12.AF.12

if mac address not end of

Code:
# grep -rn '\b[0-9a-fA-F][0-9a-fA-F]\.[0-9a-fA-F][0-9a-fA-F]\.[0-9a-fA-F][0-9a-fA-F]\.[0-9a-fA-F][0-9a-fA-F]\b' ex2.sh
9:jhhh 12.12.AF.12

This User Gave Thanks to ygemici For This Post:
# 13  
Old 07-26-2010
Thanks a lot ygemici... Smilie


The below code is working for me ....

Code:
grep -rn '\b[0-9a-fA-F][0-9a-fA-F]\.[0-9a-fA-F][0-9a-fA-F]\.[0-9a-fA-F][0-9a-fA-F]\.[0-9a-fA-F][0-9a-fA-F]\b' ex2.sh

But am not understanding why this is not working Smilie

Code:
grep -rn '[0-9a-fA-F][0-9a-fA-F]\.[0-9a-fA-F][0-9a-fA-F]\.[0-9a-fA-F][0-9a-fA-F]\.[0-9a-fA-F][0-9a-fA-F]$' ex2.sh

# 14  
Old 07-26-2010
Quote:
Originally Posted by flamingo_l
Thanks a lot ygemici... Smilie


The below code is working for me ....

Code:
grep -rn '\b[0-9a-fA-F][0-9a-fA-F]\.[0-9a-fA-F][0-9a-fA-F]\.[0-9a-fA-F][0-9a-fA-F]\.[0-9a-fA-F][0-9a-fA-F]\b' ex2.sh

But am not understanding why this is not working Smilie

Code:
grep -rn '[0-9a-fA-F][0-9a-fA-F]\.[0-9a-fA-F][0-9a-fA-F]\.[0-9a-fA-F][0-9a-fA-F]\.[0-9a-fA-F][0-9a-fA-F]$' ex2.sh

Because $ means end of the line

if the line that is

"..... jhhh 12.12.AF.12" --> means "..... jhhh 12.12.AF.12$ "

if the line is

" ..... jhhh 12.12.AF.12 bla blaa... --> means "..... jhhh 12.12.AF.12 bla blaa...$"

and our pattern does not matchs Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

SHELL: UNIX : Ls regular expression not working when used with variables

If i do below command in unix prompt which static values (ie 27..97), it is working fine and gives desired output >ls -d $WORKDIR/batch/somefilename_{27..97}.* 2>/dev/null somefilename_27.sometxt somefilename_28.sometxt somefilename_29.sometxt .. somefilename_97.sometxt But if i want... (2 Replies)
Discussion started by: haiderali
2 Replies

2. Shell Programming and Scripting

incorporating a regular expression statement in a shell script (.sh)

I do have a shell file where I call many unix commands . I would like to add a regular expression step in that shell file, where a text file, say Test.txt has to be openned and all the :'s should be replaced. Basically apply the follwoing regular expression: :%s/://g to that particular text... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

3. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

4. Shell Programming and Scripting

regular expression with shell script to extract data out of a text file

hi i am trying to extract some specific data out of a text file using regular expressions with shell script that is using a multiline grep .. and the tool i am using is pcregrep so that i can get compatibility with perl's regular expressions for a sample data like this, i am trying to grab... (6 Replies)
Discussion started by: vemkiran
6 Replies

5. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

6. Shell Programming and Scripting

search a regular expression and match in two (or more files) using bash

Dear all, I have a specific problem that I don't quite understand how to solve. I have two files, both of the same format: XXXXXX_FIND1 bla bla bla bla bla bla bla bla bla bla bla bla ======== (return) XXXXXX_FIND2 bla bla bla bla bla bla (10 Replies)
Discussion started by: TheTransporter
10 Replies

7. Shell Programming and Scripting

problem with Regular expression as input in shell script

Hi, I have script which will take a string as input and search in a file. But when I want to search a pattern which has special characters script is ignoring it. For example: I want to search a pattern "\.tumblr\.com". shell script is removing \ (backslah) and trying to search... (7 Replies)
Discussion started by: Anjan1
7 Replies

8. Shell Programming and Scripting

Regular expression matching in BASH (equivalent of =~ in Perl)

In Perl I can write a condition that evaluates a match expression like this: if ($foo =~ /^bar/) { do blah blah blah } How do I write this in shell? What I need to know is what operator do I use? The '=~' doesn't seem to fit. I've tried different operators, I browsed the man page for... (3 Replies)
Discussion started by: indiana_tas
3 Replies

9. Shell Programming and Scripting

how to use regular expression in Bash Shell Scripting

Hi, Actually i have written one test.sh (shell program) in bash. Here i have a variables $a which stored the value package1. Now I want to write a regular expression inside the if command that "if $a variable contains letter p in the begining of the value package1 then it is coming true.... (5 Replies)
Discussion started by: sunitachoudhury
5 Replies

10. Shell Programming and Scripting

using regular expression an shell script!!

I want to check if the first argument of my shell script starts with a specifiec string? Any Idea?? Thank u (3 Replies)
Discussion started by: andy2000
3 Replies
Login or Register to Ask a Question