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

Awk Help

Status
Not open for further replies.

beaster

Technical User
Aug 20, 2001
225
US
I have a text file that has text in it like that below. I need a small script that reads or cat's this file, alarm.txt (filename). I need it to give me an output file with only certain info from the file.

***FILE ALARM.TXT***
A1/EXT "BH1MSC C000Q00" 007 011026 0020
COMMERCIAL POWER FAIL



EXTERNAL ALARM
GENERATOR CONTROL SWITCH IN MANUAL.
EXTERNAL CABINET DOOR ALARM.



A3/APT "BH2MSC D000L00" 100 011118 0006
IAN-2 D-CHANNEL FAILURE

DETY
RALT24


If the above alarm.txt file contained the words COMMERCIAL POWER FAIL or IAN-2 D-CHANNEL FAILURE (From which are set up in another file called alarm_strip which it fgrep's from, I want a seperate output file for each. The output file would need to be a "one-liner" which I have listed an example below and the file name would have to go up in sequence for each file. Example would be alarm_ftp1 and alarm_ftp2.

Inside the first file alarm_ftp1 textfile would read:

BH1MSC COMMERCIAL POWER FAIL

Inside the second file alarm_ftp2 textfile would read:

BH2MSC IAN-2 D-CHANNEL FAILURE

So inside the final file, it would have had to fgrep from an input file, searched for the keywords and if it found it, output it in a particular format while reading which node it was in (BH1MSC or BH2MSC, see above alarm.txt file example) in front of the text and output them to seperate text files going up in sequence.


I hope I have not bothered anyone by asking for too much.

Beaster



 
Try this
Code:
BEGIN {
  while ((getline < &quot;alarm_strip&quot;) > 0) hld1[$0] = 1
}
{
  if (hld1[$0]) {
    ix++
    print hld2 &quot; &quot; $0 > &quot;alarm_ftp&quot; ix
  }
  else {
    hld2 = substr($2,2,length($2))
  }
}
Hope this helps.
CaKiwi
 
I am feeling pretty stupid right now. I am sorry that I am having a brain dump right now. Here is what I ran below:

#!/bin/sh

#CaKiwi Script

'BEGIN { fgrep -v -f alarm_data.txt alarm.txt
while ((getline < &quot;alarm_strip&quot;) > 0) hld1[$0] =
1
}
{
if (hld1[$0]) {
ix++
print hld2 &quot; &quot; $0 > &quot;alarm_ftp&quot; ix
}
else {
hld2 = substr($2,2,length($2))
}
}'




I get the following output:


alarm_kiwi.sh: BEGIN { fgrep -v -f alarm_data.txt alarm.txt^J while ((getline <
&quot;alarm_strip&quot;) > 0) hld1[$0] = ^J1^J}^J{^J if (hld1[$0]) {^J ix++^J prin
t hld2 &quot; &quot; $0 > &quot;alarm_ftp&quot; ix^J }^J else {^J hld2 = substr($2,2,length($2)
)^J }^J}: not found
 
this is NOT a shell script - save the suggested solution in foo.awk and run it from shell like so:

awk -f foo.awk alarm.txt

Hm.... why have you added the &quot;fgrep&quot; in the BEGIN clause????

vlad
 
Can I run it in a shell script, so I can have my &quot;at&quot; job execute it?

Won't I need fgrep to tell it where to read from? I am sorry, but I am not that good at this stuff.

//beaster
 
Hi Beaster,
Vlad is correct. My awk script will read alarm_strip to find the alarm text to process and create file alarm_ftp1, alarm_ftp2,... Put the awk comand in your script as suggested by Vlad.
Hope this helps. CaKiwi
 
Vlad states to save the suggestion in foo.awk? Where is this? I removed evything I added because I did not understand to begin with. I have it now exactly like to wrote it.

//beaster
 
I suggest that you save the awk code I posted in a file called alarm.awk (rather than foo.awk suggested by Vlad) then put a line

awk -f alarm.awk alarm.txt

in your shell script.



CaKiwi
 
I have had problems with awk in the past, and have successfully been able to use nawk in my scripts.

The file alarm.nawk looks like this now:
BEGIN {
while ((getline < &quot;alarm_data&quot;) > 0) hld1[$0] = 1
}
{
if (hld1[$0]) {
ix++
print hld2 &quot; &quot; $0 > &quot;alarm_ftp&quot; ix
}
else {
hld2 = substr($2,2,length($2))
}
}

The response I get back when running from prompt is:

beaster> nawk -f alarm.nawk alarm
nawk: syntax error at source line 7
context is
print hld2 &quot; &quot; $0 > &quot;alarm_ftp&quot; >>> ix <<<
nawk: illegal statement at source line 7
 

yeah, I've had this problem with awk in the passed. I've ended up building the file name in aseperate statement [as oppose of building on the fly].

outputFile=&quot;alarm_ftp&quot; ix++
print hld2 &quot; &quot; $0 > outputFile

vlad
 

actually in _your_ case it should be:

outputFile=&quot;alarm_ftp&quot; ++ix

vlad
 
Hi Beaster,
You must be on a Sun. They provide nawk with the newer awk features and have not updated awk. It also seems to be a little finicky about some things. Vlad's suggestions look good. CaKiwi
 
Yea I am using an Ultra 10, but my script will be running on an Enterprise 4500. I am at home now, so I will try Vlad's suggestion in the morning, and post here if it does not work.

One question,
If I have more alarms than two in the file it is searching will I run into a problem? Sometimes it may be 10 or maybe 100.

//beaster

You guys have been a great help!
 
Any number of alarms in the file should be ok (at least until awk runs out of memory ;-) ) CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top