Compare output of UNIX command and match data to text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare output of UNIX command and match data to text file
# 1  
Old 06-30-2015
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:

Code:
  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:
Code:
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.
Moderator's Comments:
Mod Comment Please do NOT use FONT tags inside CODE tags.

Last edited by Don Cragun; 06-30-2015 at 12:42 AM.. Reason: Remove extraneous FONT tags.
# 2  
Old 06-30-2015
how about using awk like this:

Code:
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+" -

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 07-01-2015
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.
# 4  
Old 07-01-2015
Might be better to step back, lay out the entire picture and create a detailed, accurate specification?
# 5  
Old 07-01-2015
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:

Code:
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.
# 6  
Old 07-01-2015
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.
# 7  
Old 07-01-2015
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).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. UNIX for Dummies Questions & Answers

Compare data - Match first column and compare second

Hi guys, looking for some help with a way to compare data in two files but with some conditions. example, File 1 consists of site1,10.1.1.1 site2,20.2.2.2 site3,30.3.3.3 File 2 contains site1,l0.1.1.1 site2,50.1.1.1 site3,30.3.3.3 site4,40.1.1.1 I want to be able to match the... (1 Reply)
Discussion started by: mutley2202
1 Replies

3. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

4. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

5. Shell Programming and Scripting

awk help: Match data fields from 2 files & output results from both into 1 file

I need to take 2 input files and create 1 output based on matches from each file. I am looking to match field #1 in both files (Userid) and create an output file that will be a combination of fields from both file1 and file2 if there are any differences in the fields 2,3,4,5,or 6. Below is an... (5 Replies)
Discussion started by: ambroze
5 Replies

6. Shell Programming and Scripting

Compare 2 files and match column data and align data from 3 column

Hello experts, Please help me in achieving this in an easier way possible. I have 2 csv files with following data: File1 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:36:09,JOB_5340 08/23/2012 12:36:14,JOB_5340 08/23/2012 12:36:22,JOB_5350 08/23/2012... (5 Replies)
Discussion started by: asnandhakumar
5 Replies

7. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

8. Shell Programming and Scripting

Request to check: compare two files , match same entries, write data before it

Hi all, I have 2 files:Column1 of first file has to be matched with column 3 of second file first file contain DATA like this in 2 columns one with gene name second with whether CAD,HT,RA T2Dor any one column 1 column2 ARFGEF2 CAD DDEF2 CAD PSCD3 CAD PSCD4 CAD CAMK1... (5 Replies)
Discussion started by: manigrover
5 Replies

9. Shell Programming and Scripting

Redirect output to a different text file depending source of data

I have a list of DNS servers I need to look up information on. Each of these servers has a master and a slave database. Essentially what I need to do is create two text files for each server. One with the Master view and one with the Slave view. There's 20 servers, in the end I should have 40 text... (4 Replies)
Discussion started by: spartan22
4 Replies

10. Shell Programming and Scripting

Compare two text file and output the same to third file

Need help. I have a source file that listed sets of numbers/words and what i'm trying to do is, by each line of the source file i want to look for same numbers/words in second file and if it match then write it to third file. Third file should have whole line from source file plus whole line... (7 Replies)
Discussion started by: suresh7730
7 Replies
Login or Register to Ask a Question