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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hi, If I have a command in awk s

Status
Not open for further replies.

SpongeBob1

Programmer
Mar 1, 2002
49
0
0
US
hi,

If I have a command in awk such as;

awk '/error/ { print $7}' errorlog.log

This will give me a list of all the $7 variables in the line containing 'error', now what if I wish to use the $7 variable to test the file again ( errorlog.log ), using this $7 as the input value i.e.

awk '/$7/ { print $0 }' errorlog.log

I am hoping to get back all lines where $7 occurs - I need to do this operation in my script. Does anyone have any ideas on how to achieve this? I'm sure it is simple, but I am new to awk...
 
something like that:

pats=$(nawk '/error/ {printf $7 "|"} END {print}' errorlog.log | sed -e 's/|$//g')

#echo "pats->[${pats}]"

nawk -v pats=${pats} '$0 ~ pats' errorlog.log
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
#!/bin/sh
pat=$1
one=`awk -v p=$pat '/p/ {print $7}' errorlog.log`
test ! -z &quot;$one&quot; && awk -v p=$one '/p/ {print $0}' error.log
echo &quot;Return = $?&quot;


This is a quick hack, a pipe would also work.
 
marsd,
that doesn't work. Here's why:

> #!/bin/sh
> pat=$1
> one=`awk -v p=$pat '/p/ {print $7}' errorlog.log`
the &quot;/p/&quot; construct takes the pattern LITERALLY, meaning a string &quot;p&quot;. If you have a dynamic pattern/RE, you need to use the '$0 ~ p' construct.

Also, if the resulting variable 'one' contains MULTIPLE string [a pattern 'p' matched MULTIPLE lines producing multiple '$7'], how are you going to use that pattern later on?

You really need to construct an RE with ORed matching patterns from the first awk run. In your implementation 'one' will contain patterns [$7] ONE per line - you cannot use that pattern in your second step.

> test ! -z &quot;$one&quot; && awk -v p=$one '/p/ {print $0}' error.log

The same note applies here re: the DYNAMIC RE you're trying to match agains.

> echo &quot;Return = $?&quot;

I tried this one a test file and 'no joy'. vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Oh...
Thanks vlad.. I just assumed the re expansion would work.
Not one of my better suggestions.
Please disregard. This never happened, move along, nothing to see here ;)
 
If you would like to organize your lines according to &quot;$7&quot; entries (so that if there are &quot;error&quot; lines with $7=&quot;error1&quot; and $7=&quot;error2&quot;, you will get all lines for &quot;error1&quot; before all lines for &quot;error2&quot;), then try something like this:
Code:
awk '/error/ {print $7}' errorlog.log | sort -u | while read entry
do
   echo &quot;*** Lines for $entry ***&quot;
   awk '/'&quot;$entry&quot;'/' errorlog.log
done

Note that if any of your $7 values contains an awk metacharacter (such as . or *) this will not work right. But if all the $7 values are alphanumeric, you'll be fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top