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

want to use awk for this 1

Status
Not open for further replies.

frankli

Technical User
Apr 6, 2005
44
CA
Hello all,

I have two files, vfile, venv,

cat vfile
file1 v_name1 v_name2 v_name3 na v_name5 na v_name7
file2 v_name1 na v_name3 na v_name5 v_name6 na
file3 v_name1 v_name2 na na na na na

cat venv
v_name1=v1
v_name2=v2
v_name3=v3
v_name4=v4
v_name5=v5
v_name6=v6
v_name7=v7

want to use awk to go through each record in vfile

for (i=2; i<=NF; ++i)
if ( $i !~ /^na$/ )

want to obtain the result of (e.g. for 1st record)
result = grep $v_name1 file1 | wc -l
it's actually 'grep v1 file1 | wc -l'

if (result = 2)
pass
else
fail

please see if you could provide a start point. I don't have deep understand of awk, so messed up a little bit.
Thanks!
 
A starting point:
awk '
NR==FNR{split($0,a,"=");v[a[1]]=a[2];next}
{
for(i=2;i<=NF;++i)
if($i!="na"){
cmd="grep "v[$i]" "$1" | wc -l"
cmd | getline result;close(cmd)
print cmd, (result==2?"pass":"FAIL")
}
}
' venv vfile

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PHV, this start point pretty much resolve the whole thing, I need to study on it.

Best Regards!
 
By using the files you guys provided, I got the following output from AIX. I can see the "FAIL" message is there, but is the other stuff what you wanted?


Thanks
David

grep: 0652-033 Cannot open file1.
grep v1 file1 | wc -l FAIL
grep: 0652-033 Cannot open file1.
grep v2 file1 | wc -l FAIL
grep: 0652-033 Cannot open file1.
grep v3 file1 | wc -l FAIL
grep: 0652-033 Cannot open file1.
grep v5 file1 | wc -l FAIL
grep: 0652-033 Cannot open file1.
grep v7 file1 | wc -l FAIL
grep: 0652-033 Cannot open file2.
grep v1 file2 | wc -l FAIL
grep: 0652-033 Cannot open file2.
grep v3 file2 | wc -l FAIL
grep: 0652-033 Cannot open file2.
grep v5 file2 | wc -l FAIL
grep: 0652-033 Cannot open file2.
grep v6 file2 | wc -l FAIL
grep: 0652-033 Cannot open file3.
grep v1 file3 | wc -l FAIL
grep: 0652-033 Cannot open file3.
grep v2 file3 | wc -l FAIL
 
kobewins, have you created files named file1, file2, file3 ...?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
hello PHV and all,

by using NR==FNR{split($0,a,"=");v[a[1]]=a[2];next}
can I use the v[] array outside of awk but in the same script? I m thinking of using awk to obtain variable from 2 files but need to do the substitution outside awk. Thanks.

Regards!
 
actually don't have to do the substitution outside of awk, just wondering

cat vfile
file1 v_name1 v_name2 v_name3 na v_name5 na v_name7
file2 v_name1 na v_name3 na v_name5 v_name6 na
file3 v_name1 v_name2 na na na na na

cat venv
v_name1=v1
v_name2=v2
v_name3=v3
v_name4=v4
v_name5=v5
v_name6=v6
v_name7=v7

cat venv.new
v_name1=v1new
v_name2=v2new
v_name3=v3new
v_name4=v4new
v_name5=v5new
v_name6=v6new
v_name7=v7new

for file1, v_name1 substitute v1 for v1new; v_name2 substitute v2 for v2new ...

Regards!
 
ooh, gsub might be the solution, but is there a smart way to deal with 3 files something like use NR==FNR to process lines from 2 files. Thanks.

Regards!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top