Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grep Problem 1

Status
Not open for further replies.

kvh71

Programmer
Dec 17, 2001
6
GB
I am trying to check a line of data using grep. The data I have in the file should be in the format

0123456/1,xxx,xxx etc

or

012345678/1,xxx,xxx etc

The first field must either be 7digits/1digit or 9digits/1digit, but cannot be 8digits/1digit.

So far i have '[0-9]\{7\}/[0-9]' but can someone give me a clue how to check for both formats in one line. I tried '[0-9]\{7|9\}/[0-9]' but it didn't seem to work.

Any ideas ??

 
Try something like this:
Code:
grep -E '([0-9]\{7\}|[0-9]\{9\})/[0-9]'

Hope This Help
PH.
 
I tried this but I keep getting

grep: illegal option -- E

I'm on a server using Oracle 9i, would this make any difference to the options available to me?
 
Try without the -E option: it could be implicit with your version of grep.
If this does not work, you could also try 'egrep' instead of 'grep -E'.

--------------------
"When all else has failed, read the manuals."

Denis
 
It's still not working correctly.
The code and files I am using are

egrep -v '^([0-9]\{7\}|[0-9]\{9\})/[0-9],[0-9A-Z]\{1,12\},[^ ]\{1,6\}$' test_saw.txt >> errors_saw.txt

test_saw.txt :-

0123456/1,AB,21
0321654/2,BC,22
02345678/3,CD,23
0987654/4,DE, 24
0654321/5,Ef,25

errors_saw.txt

0123456/1,AB,21
0321654/2,BC,22
02345678/3,CD,23
0987654/4,DE, 24
0654321/5,Ef,25

 
It works for me if I suppress the \ before { and }:

Code:
Code:
$
Code:
egrep -v '^([0-9]{7}|[0-9]{9})/[0-9],[0-9A-Z]{1,12},[^ ]{1,6}$' test_saw.txt
Code:
02345678/3,CD,23
0987654/4,DE, 24
0654321/5,Ef,25
Code:

--------------------
"When all else has failed, read the manuals."

Denis
 
Thanks for you patience, but if I leave out the \ it produces no results at all. Could this be an Oracle issue?
 
I don't think so. It must be a grep issue: you apply a grep command to a simple text file.
It seems your grep does not behave like mine
(
Code:
$
Code:
grep --version
Code:
grep (GNU grep) 2.4.2
).

We could try another tool. Do you have perl ?
If so try:
Code:
$
Code:
perl -n -e 'print unless m!^([0-9]{7}|[0-9]{9})/[0-9],[0-9A-Z]{1,12},[^ ]{1,6}$!' tmp/test_saw.txt
Code:
02345678/3,CD,23
0987654/4,DE, 24
0654321/5,Ef,25

And just to be sure, the three lines result I get is what you are expecting, is'nt it ?

--------------------
"When all else has failed, read the manuals."

Denis
 
Tried the Perl and it worked.

Thank you for your time
 
How about egrep '^.......(..)?/'. It's not as strict about numerics being in there, but if you wanted to be you could replace each . with [0-9].

Annihilannic.
 
This also works, cheers.

It's always good to have options.
 
"perl" is MUCH better at "pattern matching" than "grep", or even "egrep". And, often perl is beter because it can deal with "other" requirements of the problem-to-be solved, also.

End-of-memo: Best to you..
from ernieah.
 
your ratio of the problem complexity to the 'foot print' of the tools to solve it may vary as well.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top