Operating System - HP-UX
1753672 Members
5993 Online
108799 Solutions
New Discussion юеВ

Re: Shell script help with: eval grep

 
SOLVED
Go to solution
Krister Enger
Advisor

Shell script help with: eval grep

Hello,

I've several (100s) files looking like these:

FILE A.txt
==========
557_B_590-08_TEXT_111_3333333_1.bin
56_K_519-07_DIFFERENT_TEXT_1111_8888888_1.bin
727_├Е├Ц_8519-07_Text_11111_77777777_2.bin
269_B_73-08_Another_text_12345_2999999_1.bin
5_B_590-08_More_Text_22311_1999999_1.bin
157_519-07_A_lot_of_more_text_11322_1311321_1.bin

The field separator (which also could be part of the text field in the middle) is '_'.
Each line looks like this:
_____.bin
where
f1: <1-3 digits>
f2: either <3-4 digits>-<2-4 digits>
or <1-3 letters>_<3-4 digits>-<2-4 digits>
letters can be [a-zA-Z] incl swedish chars
f3: text of any length (maximum 30 chars)
f4: <3-7 digits>
f5: <5-9 digits>
f6: <1-3 digits>

All fields are compulsory.

A user to the script can supply values for each of the fields f1,f2,f3,f4. But only f2 is
mandatory to execute a search through the files:

So I need a grep to get all the lines the user is searching for.

FILE foo.sh
#!/usr/bin/ksh

# Default search values
F1=".*"
F2=".*"
F3=".*"
F4=".*"
F5=".*"
F6=".*"

# Test case
F2="519-07"

eval grep \
-i -e '^${F1}_.*${F2}_${F3}_${F4}' A.txt

Running the script foo.sh results in:
./foo.sh
56_K_519-07_DIFFERENT_TEXT_1111_8888888_1.bin
727_├Е├Ц_8519-07_Text_11111_77777777_2.bin
157_519-07_A_lot_of_more_text_11322_1311321_1.bin

which is almost correct but not quite.
The second line in the answer should not be matched.
How do I accomplish this?

This first script works for most cases but not all the time and I would like to change the default values from ".*" to eg "[0-9]\{1,3\}" but I get
grep: Invalid subexpression backreference number
when I try to run it, and preceeding with extra backslashes doesn't work either.
2 REPLIES 2
Dennis Handly
Acclaimed Contributor
Solution

Re: Shell script help with: eval grep

>The field separator (which also could be part of the text field in the middle) is '_'.

This is never a good idea. This needs to be simple and unambiguous.
f1 and f4..f6 are ok. It's 2 and 3 that cause problems.

Why are you using eval??
You probably want to remove the eval and replace that '' by ""

>preceding with extra backslashes doesn't work either.

How many did you try? It would be helpful if you could write a script that prints out its arguments so you can see how may "\" it is getting.
Krister Enger
Advisor

Re: Shell script help with: eval grep

>>The field separator (which also could be
>>part of the text field in the middle) is
>>'_'.

>This is never a good idea. This needs to be
>simple and unambiguous.
>f1 and f4..f6 are ok. It's 2 and 3 that cause
>problems.

I know, I forgot to mention that the user doesn't know if f2 has letters or not, they only know about the digits...

I have no influence of how the lines are put togethet, I just get the lines, and I know what the fields can consist of.

>Why are you using eval??

Eh, I had a different solution in the very beginning with an eval statement, I guess it's
historical, and I see that I get the same result without it, thx

>You probably want to remove the eval and >replace that '' by ""

This did the thing!!!
10p for you