Compare output of UNIX command and match data to text file

I am working on an outage script and I run a command from the command line which tells me the amount of generator failures in my market. The output of this command only gives me three digits to identify the site by. I have a master list of all sites in a separate file, call it list.txt. If my command appends to file outagereport.txt, I want to use SED or something else to look at the output of that file and match it to the sites in the list.txt file and then modify the original file.

So, lets say that I run the following command:

  TICLI "op:alarm,all" | {  printf "Austin Generators Running Alarms:\n" && grep GEN | grep RUN || printf "Austin Generator Alarms: Zero \n"; }>> AustinOutageReport.txt
    

and I get the following output:

Austin Generators Running Alarms:
     CELL 40 GENERATOR RUNNING

and my list.txt file has the following entry in it:
Sitename-market-0040-awesomesite

How can I get UNIX to look at list.txt and replace the text of cell for generator running with the text of list.txt that says Sitename-market-0040-awesomesite?

My thought was to use a sed substitute command for this but if I do that I will have a script with over 1800 sed substitutes.

how about using awk like this:

TICLI "op:alarm,all" | awk '
   FNR==NR {V[$3+0]=$0; next}
   /GEN/ && /RUN/ && ($3 in V) {
     if(!found) {
       print "Austin Generators Running Alarms:"
       found++
     }
     $2=V[$3]
     print
   }
   END { if(!found) print "Austin Generators Running Alarms: Zero" }' FS=- list.txt FS="\\\s+" -
1 Like

I tried the suggestion and I am still working with it to get the output I need. I don't think I understand enough what it's doing.

Might be better to step back, lay out the entire picture and create a detailed, accurate specification?

Yes, it would be good to step back at this point and do more research.

I solved this problem through using sed.

I created a sed script which contains about 3,400 sed commands like the one below:

sed -i.bak 's/143  ISOL     INDT/SA 4119-4-4-0143-ST_ALBANS/' masterreport.txt    

This searches my file of master report and modifies the text of the original report with the site names in my sed script. It takes less time than I thought. thanks for all the help.

Invoking awk once to make up to 3400 changes to a file is 99.94% likely to run MUCH faster (by a few orders of magnitude) than invoking sed 3400 times to perform the same task. If you would give us clear, explicit descriptions of what the files being processed really look like (instead of showing us the output you get from running a script with unspecified input file formats), we would be happy to try to help you solve your problem more efficiently.

The key to writing efficient, reliable software is to know what the input looks like, what output needs to be produced, and what constraints can be assumed concerning the inputs and outputs.

Don, while he might be saying he's running sed 3400 times, if instead they are 3400 sed commands in single invocation, it will run faster than your awk (just saying).

You might be right. I looked at:

and interpreted it as "a shell script containing 3,400 sed commands ...". If jbrass meant what you're describing, I would have expected it to be something more like: "a sed script which contains about 3,400 substitute commands...".