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 to execute external script and write the ouput to separated files 2

Status
Not open for further replies.

ampra2

Technical User
Apr 18, 2011
4
AU
I need help on AWK scripting to do some actions as follows:
- Parsing File,
- Do some calculation, or Execute External Program,
- Write the Output into Separated Files

I have an input file (say, input.txt) consists of this data:
Code:
834608 0.50500 openssl-sp ampra2       r     04/18/2011 09:26:18 all.q@east-32.enterprisegrid.e     1
834609 0.55500 openssl-sp ampra2       qw    04/18/2011 14:43:03                                    1
834610 0.55500 openssl-sp ampra2       qw    04/18/2011 14:43:05                                    1

I need my AWK script to read some values of it.
Those values are:
- $1 (id)
- $5 (status)
- $6 (date)
- $7 (time), and
- $8 (location)

For each record of the input file which has "qw" on $5, this script will create a file named $1.data (for each record) to store a value calculated based on the current system time and $6 + $7 on the record.

Meanwhile, for each record which has "r" as status ($5), the script will execute an external program (sh script) using those values as parameters. This external program will give a string as return value. And then this AWK script will also create a file named $1.data (for each record) to store this return value.

On this case, the AWK script will create 3 output files:
- 834608.data with return-string-value from an external program as the content.
- 834609.data and 834610.data with calculated values as the content

Anybody can help me on this problem?

Many thanks in advance.
 
What have YOU tried so far and where in YOUR code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi
[ul]
[li]What have you tried so far ?[/li]
[li]What kind of AWK implementation can you use ?[/li]
[/ul]
The basic solution would be this :
Code:
awk '$5=="qw"{print$1,$5,$6,$7,$8>$1".data"}$5=="r"{system("echo "$1" "$5" "$6" "$7" "$8" > "$1".data")}' input.txt
But seems that your data has fixed width columns and some columns can be empty, so with [tt]gawk[/tt] you can add this to handle them :
Code:
awk '[highlight]BEGIN{FIELDWIDTHS="7 8 24 6 11 9 35 10"}[/highlight]$5=="qw"{print$1,$5,$6,$7,$8>$1".data"}$5=="r"{system("echo "$1" "$5" "$6" "$7" "$8" > "$1".data")}' input.txt
If you have no [tt]gawk[/tt] is abit lengthy, but not difficult, so I let it to you.


Feherke.
 
Hi PHV and feherke,
This is my current script.
I've made a bit change so it doesn't exactly the same thing with my description previously.
Code:
#!/bin/sh
awk '

function timedif(date,time) {
# get current system time

# calculate the difference (sec)

# convert the time difference format (hh:mm:ss)

# return the time difference

}

BEGIN {
        OFS="|";
}

{
        if ($5 == "r") {
                # run external script 
                "sendsnmp.sh $8 $1"
        } else if ($5 == "qw") {
                duration=timedif($6,$7)
                print $6, $7, $duration >> $1".qw"
}

}' input.txt
I haven't done anything for the time difference calculation, but I can write output.qw successfully if I remove some part related to the function. I'm sure if I can finish the function correctly, I can get output.qw for each record.

My problem is on the "r" one, I can't get output.run at all. Basically the script "sendsnmp.sh" will write its output to a file "output.run" where "output" is taken from the second argument (equal to $1 of input.txt). I tried executing sendsnmp.sh directly and it gives me "output.run" correctly. But when I execute this AWK script, it doesn't give me any "output.run" file. It doesn't give any error message, either.

Any idea what's happening with my script?
 
Replace this:
"sendsnmp.sh $8 $1"
with this:
system("sendsnmp.sh "$8" "$1)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PHV,

with
Code:
system("sendsnmp.sh "$8" "$1)
I get this error message:
Code:
sh: sendsnmp.sh: command not found
sh: sendsnmp.sh: command not found
sh: sendsnmp.sh: command not found

But it works when I change it to
Code:
system("./sendsnmp.sh "$8" "$1)

Thanks.

Now, I'm working on the time difference calculation.
If anybody have idea how to do this, it will help me so much.
 
Hi

You still not answered my question :
Feherke said:
What kind of AWK implementation can you use ?
But as you posted no objection against the use of [tt]gawk[/tt], I assume it is fine for you :
Code:
[b]function[/b] [COLOR=darkgoldenrod]timediff[/color][teal]([/teal]date[teal],[/teal]time   [teal],[/teal]diff[teal])[/teal]
[teal]{[/teal]
  diff[teal]=[/teal][COLOR=chocolate]systime[/color][teal]()-[/teal][COLOR=chocolate]mktime[/color][teal]([/teal][COLOR=chocolate]gensub[/color][teal](/([[:[/teal]digit[teal]:]]+)\/([[:[/teal]digit[teal]:]]+)\/([[:[/teal]digit[teal]:]]+)/,[/teal][green][i]"[/i][/green][lime][i]\\[/i][/lime][green][i]3 [/i][/green][lime][i]\\[/i][/lime][green][i]1 [/i][/green][lime][i]\\[/i][/lime][green][i]2"[/i][/green][teal],[/teal][green][i]""[/i][/green][teal],[/teal]date[teal])[/teal] [green][i]" "[/i][/green] [COLOR=chocolate]gensub[/color][teal](/:/,[/teal][green][i]" "[/i][/green][teal],[/teal][green][i]"g"[/i][/green][teal],[/teal]time[teal]))[/teal]
  [b]return[/b] [COLOR=chocolate]int[/color][teal]([/teal]diff[teal]/[/teal][purple]24[/purple][teal]/[/teal][purple]60[/purple][teal]/[/teal][purple]60[/purple][teal])[/teal] [green][i]" days "[/i][/green] [COLOR=chocolate]strftime[/color][teal]([/teal][green][i]"%T"[/i][/green][teal],[/teal]diff[teal],[/teal][purple]1[/purple][teal])[/teal]
[teal]}[/teal]


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top