Operating System - HP-UX
1752780 Members
6026 Online
108789 Solutions
New Discussion юеВ

Re: Need some scripting assistance....

 
SOLVED
Go to solution
Rob Johnson_3
Regular Advisor

Need some scripting assistance....

Situation:
I have a text file with 949 serial numbers in this format:
johnsonr@nmscrme01 > cat file
28991175
28991216
29591918
...
...
...
johnsonr@nmscrme01 >

What I need to do is search this file against several XML files to see if there are any matches and if so, what file is the match in?


Here's a script that basically says:
for each serial number in file, do a grep on several NC*.xml files in a certain directory.
However, I would like to make it where the output is redirected to another file say in the /tmp directory. Can someone help me out?
---Here's the script---
#!/bin/sh
for SN in `cat /opt/home/johnsonr/scripts/file`
do
grep $SN /opt/CSCOpx/htdocs/reports/NC*.xml
done

Here's output from the script which is fine.

johnsonr@nmscrme01 > ./snsearch.sh
/opt/CSCOpx/htdocs/reports/NC-DONPortCount.xml:29769187
/opt/CSCOpx/htdocs/reports/NC-DONPortCount.xml:29769188
/opt/CSCOpx/htdocs/reports/NC-DONPortCount.xml:30959474
johnsonr@nmscrme01 >


I was thinking something along these lines but need assistance...

#!/bin/sh
for SN in `cat /opt/home/johnsonr/scripts/file`
do
grep $SN /opt/CSCOpx/htdocs/reports/NC*.xml

if [ $? -gt 0 ] -------this is the section i need the help in
then -------this is the section i need the help in
echo $results >> /tmp/SNFile -------this is the section i need the help in
fi -------this is the section i need the help in
done

Thanks in advance to all you scripters out there!
4 REPLIES 4
Rob Johnson_3
Regular Advisor

Re: Need some scripting assistance....

Looks like this is all I have to do...


#!/bin/sh
for SN in `cat /opt/home/johnsonr/scripts/file`
do
grep $SN /opt/CSCOpx/htdocs/reports/NC*.xml >> /tmp/SNFile

done
James R. Ferguson
Acclaimed Contributor
Solution

Re: Need some scripting assistance....

Hi Rob:

You can do something like this:

#!/usr/bin/sh
set -u
typeset TOKENS=/opt/home/johnsonr/scripts/file
typeset OUTPUT=/tmp/SNFile
rm "${OUTPUT}"
for SN in `cat ${TOKENS}`
do
grep ${SN} /opt/CSCOpx/htdocs/reports/NC*.xml /dev/null >> ${OUTPUT}
done

...By adding '/dev/null' to the file arguments to 'grep' the name of any file that constains a match is reported along with the match.

In keeping with the rule that "silence is golden", 'grep' will not output anything if there isn't at least one match.

Regards!

...JRF...
John Kittel
Trusted Contributor

Re: Need some scripting assistance....

if all you want is for the exact output of your existing script to go to a file, you don't need to modify the script. Just redirect it's output.

johnsonr@nmscrme01 > ./snsearch.sh >/tmp/SNFile
Dennis Handly
Acclaimed Contributor

Re: Need some scripting assistance....

Note there is no reason to use a for-loop unless you need to test on the serial number :

$ grep -f /opt/home/johnsonr/scripts/file \
/opt/CSCOpx/htdocs/reports/NC*.xml /dev/null

If you only want the filename of the match, but not number you can add -l.

If you don't want to find your serial numbers in the middle of strings, you would use -w.