AWK help - if no match 'stop' and print "no match"

Mashed Potato

Senior member
Feb 3, 2005
213
0
0
This may be a simple one for the bigger brains of the forums, but I'm a scripting novice so it is driving me nuts.

file_a contains:

1
2
3
4
5
6
7

file_b contains:

7 hens
22 gorillas
31 kangaroos


working scriptlet:

i=$(awk /7/ file_a)

awk /$i/ file_b

output would be:

7 hens

It does not work if the first awk does not match:

i=$(awk /9/ file_a)

awk /$i/ file_b

output:

7 hens
22 gorillas
31 kangaroos

Because $i equals nothing, all lines are printed. So what I am trying to do is "if $i" equals nothing, stop running and print "no match".

This is being run on a stripped version of net BSD, so I really only have awk and sed - no grep.

Can anyone help?

Thank you in advance.
 

Charles Kozierok

Elite Member
May 14, 2012
6,762
1
0
I use awk a lot, but more as a conventional programming language. What are you trying to do, only ouput the lines in file_b for which there is a number in file_a?
 

Mashed Potato

Senior member
Feb 3, 2005
213
0
0
Essentially yes.

For matches in file_a, print lines in file_b which contain match. If no matches in file_a, stop and print "no match"....or even no output is fine. I was trying to make it as simple as possible for anyone who wished to help.

This is actually being run on a live system and the 'files' in my example are really the output of commands.:

vol=$(snmpwalk.sh <mib name> | awk /block/ | sed -e 's/^.*Status.//' -e 's/ =.*//'); <proprietary command> | awk /$vol/

Where the output of snmpwalk.sh <mib name> is something like:

XXXXXX.XXXXXXX.XXXStatus.530754450.1 = INTEGER: online-control (4) lost-block
XXXXXX.XXXXXXX.XXXStatus.530754450.2 = INTEGER: online-control (4)
XXXXXX.XXXXXXX.XXXStatus.530754450.3 = INTEGER: online-control (4)
XXXXXX.XXXXXXX.XXXStatus.530754450.4 = INTEGER: online-control (4)

and the output of <proprietary command> is:

"test" X-XXXXXX-XXXXXXXXXX-XXXXXXXXXXXXXXXX 342pgs 5.01GB RW Base 530754450.1

"apples" X-XXXXXX-XXXXXXXXXX-XXXXXXXXXXXXXXXX 342pgs 5.01GB RW Base 530754450.2

"bananas" X-XXXXXX-XXXXXXXXXX-XXXXXXXXXXXXXXXX 342pgs 5.01GB RW Base 530754450.3


The sed strips everything but "530754450.1" and then awks it in the output of <proprietary command>. BUT... if there is no line with 'block' in it from the 'snmpwalk.sh <mib name>' command, all lines from the <proprietary command> are displayed.

How can I stop all lines from being printed if $vol is empty?

I hope this had made things a little more clearer than mud.

Thanks for the speedy response.
 

Charles Kozierok

Elite Member
May 14, 2012
6,762
1
0
I sort of see what you're doing. Unfortunately I don't have any experience in using Awk for quickie command line stuff. I would just write an actual Awk program that did the processing, so you could use if/then and other control structures to get what you wanted.

But that would only work well if the data were in actual files, not part of command line pipes.

Why aren't you just using grep? Seems like a better tool for simple matching. In fact I bet a simple change from "awk /$vol/" to "grep $vol" would solve the problem entirely.
 

Mashed Potato

Senior member
Feb 3, 2005
213
0
0
Indeed, grep would save the day...however...

output from shell:

#grep
grep: not found
#

unfortunately grep is not on this OS

I think I could finagle it into a 'one liner' if I could just figure out if/how awk can verify that there is a match or not.

if $vol is not null (using awk)
then print "match"
"run commands"
else print "no match"


the search continues...

Thanks for the input.
 

Charles Kozierok

Elite Member
May 14, 2012
6,762
1
0
You have awk but not grep? That's weird. What is the OS? Have you tried egrep?

It's hard to help because of course I'm not familiar with what you're doing. If these things were actually in files I'd do something like this:

Code:
{
   while (getline file_a_line <file_a) {
      line_a[file_a_line] = 1
   }
   close (file_a)

   while (getline <file_b) {
      if (line_a[$1]) {
          print $0
      }
   }
}

Fairly simple, but requires using temporary files in your application. Maybe this will give you some ideas though.

A kludge might be to put something at the end of the file that has "block" in it and always matches.. and then ignore or strip it.
 

Aluvus

Platinum Member
Apr 27, 2006
2,913
1
0
I sort of see what you're doing. Unfortunately I don't have any experience in using Awk for quickie command line stuff. I would just write an actual Awk program that did the processing, so you could use if/then and other control structures to get what you wanted.

But that would only work well if the data were in actual files, not part of command line pipes.

I will admit to knowing very little about awk, but surely awk scripts can read from STDIN? A brief Google search suggests you perhaps need to supply '-' as an explicit argument.

You could also replace the entire sed/awk chain with a bit of Perl or Python, if either is available.

I too am curious what OS has awk and sed, but not grep.
 

Mashed Potato

Senior member
Feb 3, 2005
213
0
0
This is being run on a stripped down version of NetBSD. This OS is on a SAN device. I have sed, awk, and shell. Bash was removed a little while back.

I'm getting closer, but I can't seem to work it into my situation. The decimal messes with the 'greater than' and 'less than'.

Potato@Potato-PC ~
$ if [[ $vol -gt 0 ]]; then echo "not playing"; else ls; fi
-bash: [[: 12343145.223
: syntax error: invalid arithmetic operator (error token is ".223
")
 

Charles Kozierok

Elite Member
May 14, 2012
6,762
1
0
Don't know much BASH but what I just read suggests "-gt" only works on integers. Try stripping off the fractional part.
 

Mashed Potato

Senior member
Feb 3, 2005
213
0
0
figured it out with a loop

for i in $(snmpwalk.sh <mib> s | awk /block/ | sed -e 's/^.*Status.//' -e 's/ =.*//'); do <proprietary command> | awk /$i/;done

thanks for the replies
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |