- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Shell script help with: eval grep
Operating System - HP-UX
1753809
Members
7894
Online
108805
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-04-2008 03:12 AM
тАО03-04-2008 03:12 AM
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.
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:
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.
Solved! Go to Solution.
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-04-2008 04:24 AM
тАО03-04-2008 04:24 AM
Solution
>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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-04-2008 04:53 AM
тАО03-04-2008 04:53 AM
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
>>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
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP