Operating System - HP-UX
1753770 Members
5300 Online
108799 Solutions
New Discussion юеВ

parse a file with expressions (grep)

 
SOLVED
Go to solution
Dennis Handly
Acclaimed Contributor

Re: parse a file with expressions (grep)

>awk -v entry1="${entry1}" -v entry2="${entry2}" -v var="${var}" 'BEGIN { FS = "[ \t]*|[ \t]+" }
$1 ~ /^'entry1'$/ && $2 ~ /^'entry2'$/ && $3 ~ /^'var'$/ { print "FOUND" }' test.cnf

There is no need for your single quotes around entry1, etc. They aren't doing what you think.
Also, there may be no need to do pattern matching, you could do exact match:
awk -v entry1="${entry1}" -v entry2="${entry2}" -v var="${var}" '
$1 == entry1 && $2 == entry2 && $3 == var { print "FOUND" }' test.cnf

And if you really wanted to do pattern matching you would need to use something like:
$1 ~ /^'$entry1'$/
(And forget about using awk -v.)

Or do:
$1 ~ "^" entry1 "$"
Billa-User
Regular Advisor

Re: parse a file with expressions (grep)

hello,

thx to all for your input, last i have a special question, i add to "test.cnf" following entry:

* * var

and start test.sh (in attachment)

./test.sh entry1 entry2 var

this should find:
entry1 entry2 var
* * var

how can i use "*" in awk ?

regards,
Dennis Handly
Acclaimed Contributor

Re: parse a file with expressions (grep)

>this should find: * * var
>how can I use "*" in awk?

Where are your patterns? On the command line or in your test.cnf?

Do you want to have the command line or test.cnf have EREs? Or do you want an exact match with a special case of "*" in test.cnf?

>if ($1 == /^\*/ && entry2 == /^\*/) {

I have no idea what this will do. == should take either a literal string, number or variable. I guess "/^\*/" is just an expression and it seems to have the value 0 if it doesn't match $0.
Billa-User
Regular Advisor

Re: parse a file with expressions (grep)


@ Where are your patterns? On the command line or in your test.cnf?
in the attachment script (test.sh)

@ Do you want to have the command line or test.cnf have EREs? Or do you want an exact match with a special case of "*" in test.cnf?
i want it , if it easy to handle in test.cnf.

sorry, it explained it very bad .

what i mean :

i want to make an entry in config-file with wildcard's? like :

* * var

and then i want to search with a statement all entry's field 1 and 2 and with exact value in field 3 (example : var)

also i think it complicated to search entries with example
entry* entry* var

but this i don't need for my case.

in the meantime in think about following,
i create a placeholder or wildcard for "*" like "allmatches" and it is
easier to handle in statement's . example:

allmatches allmatches var
Dennis Handly
Acclaimed Contributor
Solution

Re: parse a file with expressions (grep)

>I want to make an entry in config-file with wildcards? like: * * var
>I want to search with a statement all entries field 1 and 2 and with exact value in field 3 (example: var)

>I think it complicated to search entries with example: entry* entry* var
>but this I don't need for my case.

Right, this is harder but if you use EREs, you can do that in awk.

>I create a placeholder or wildcard for "*" like "allmatches" and it is easier to handle in statements

No, "*" is easier:
# Pattern in test.cnf
# Use == for match
awk -v entry1="${entry1}" -v entry2="${entry2}" -v var="${var}" '
($1 == "*" || $1 == entry1) &&
($2 == "*" || $2 == entry2) &&
$3 == var { print "FOUND5:", $0 }' test.cnf
Billa-User
Regular Advisor

Re: parse a file with expressions (grep)

perfect,thank you very,very much!!