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

String Search in Data Block

Status
Not open for further replies.

imFrank

Technical User
Mar 30, 2005
28
US
THIS IS THE CODE I AM USING. I AM RUNNING AWK ON SGI IRIX.


{if ( $1~/@NAME/ && $2~/ASalarm_HBT_SS/ ) {a=$2} {t1=1}}

{if ( /@ENABLE_LIMITS:/ ) {b=$0} {t2=1}}

{if (t1==1 && t2==1) printf "%s %s", FILENAME " " a, b "\n"}

{a=""} {b=""} {t1=0} {t2=0}

{if (/@FMT/) {printf "\n" "NEXT PARAM" "\n\n" }}


THESE ARE THE RESULTS I AM GETTING
(The "NEXT PARAM" statement only helps me see I am getting output
from every single data file record. I am only using it for debugging.
It is not necessary for the final code)

ws20 21% awk -f parsrch3.awk data.file
small.txt
small.txt
small.txt
small.txt
small.txt
small.txt
small.txt
small.txt @ENABLE_LIMITS: NO
small.txt
small.txt
small.txt
small.txt
small.txt
small.txt
small.txt
small.txt

NEXT PARAM

small.txt
small.txt
small.txt
small.txt
small.txt
small.txt

NEXT PARAM

small.txt
small.txt ASalarm_HBT_SS
small.txt
small.txt
small.txt @ENABLE_LIMITS: NO
small.txt
small.txt
small.txt
small.txt
small.txt
small.txt
small.txt
small.txt

NEXT PARAM

small.txt
small.txt
small.txt
small.txt
small.txt @ENABLE_LIMITS: NO
small.txt
small.txt
small.txt
small.txt
small.txt
small.txt
small.txt
small.txt

NEXT PARAM

ws20 22%


I WOULD LIKE TO GET THE RESULTS IN TWO FLAVORS:

(DESIRED OUTPUT 1)
THESE ARE THE RESULTS I WANT


ws20 21% awk -f parsrch3.awk data.file
small.txt ASalarm_HBT_SS
small.txt @ENABLE_LIMITS: NO


(DESIRED OUTPUT 2)
AND IN SOME CASES THESE ARE THE RESULTS I WOULD LIKE


ws20 21% awk -f parsrch3.awk data.file
small.txt ASalarm_HBT_SS @ENABLE_LIMITS: NO


THIS IS THE DATA


ws20 31% cat data.file
@VERSION_NUM: 1.11

@JITTER_VAL: 55 @MATH_TRIG: NONE

@NAME: ASalarm_HB @STREAM: 1
@DESC: PAM VEH alarm BH @UNITS: BL
@DATAQ_CC
@ENABLE_LIMITS: NO
@PROC_LOC: FRONT END @FEND_NO: 1 @TIME_FMT: BINARY
@RAW_COMP: NONE
@RAW_CONV: IEEE FP
@EU_CAL: NULL
@EU_COMP: NONE
@YEL_LO: -7.5e+05 @YEL_HI: 7.5e+05 @RED_LO: -7.5e+05 @RED_HI: 7.5e+05
@DYN_LIMS: INACTIVE
@FMT: FLOAT @RES: 3

@NAME: ASalarm_HBT_CS @STREAM: 2
@DESC: PAM VEH alarm BH ENABLE CMD ST @UNITS: CMD ST
@DATAQ_CC
@EU_CAL: NULL
@FMT: FLOAT @RES: 3

@NAME: ASalarm_HBT_SS @STREAM: 1
@DESC: PAM VEH alarm BH SCRIPT STATUS @UNITS: DSRT
@DATAQ_CC
@ENABLE_LIMITS: NO
@PROC_LOC: FRONT END @FEND_NO: 1 @TIME_FMT: BINARY
@RAW_COMP: NONE
@RAW_CONV: IEEE FP
@EU_CAL: NULL
@EU_COMP: NONE
@YEL_LO: -7.5e+05 @YEL_HI: 7.5e+05 @RED_LO: -7.5e+05 @RED_HI: 7.5e+05
@DYN_LIMS: INACTIVE
@FMT: FLOAT @RES: 3

@NAME: ASalarm_STALL @STREAM: 1
@DESC: PAM VEH alarm BH FAILURE STATUS @UNITS: BL
@DATAQ_CC
@ENABLE_LIMITS: NO
@PROC_LOC: FRONT END @FEND_NO: 1 @TIME_FMT: BINARY
@RAW_COMP: NONE
@RAW_CONV: IEEE FP
@EU_CAL: NULL
@EU_COMP: NONE
@YEL_LO: -7.5e+05 @YEL_HI: 7.5e+05 @RED_LO: -7.5e+05 @RED_HI: 7.5e+05
@DYN_LIMS: INACTIVE
@FMT: FLOAT @RES: 3
 
Hi

Caps off, please.

Do not put each command between it's own braces, just separate them with semicolon :
Code:
{ if ($1~/@NAME/ && $2~/ASalarm_HBT_SS/) { a=$2; t1=1 } }

As you wrote, the [tt]if[/tt] has effect only on the first command block, the [tt]{a=$2}[/tt], the second block, the [tt]{t1=1}[/tt] will be always executed. And this applies to all your script.

Feherke.
 
Hi

I know that there are incompatibilities, so is possible to not work on your machine. With [tt]gawk[/tt] on Linux works :
Code:
$1~/@NAME/ && $2~/ASalarm_HBT_SS/ { a=$2 }

/@ENABLE_LIMITS:/ { b=$0 }

a!="" && b!="" { print FILENAME,a,b; a=b="" }

/@FMT/ { print "\nNEXT PARAM\n" }

Feherke.
 
feherke,

Will try your suggestions today and let you know the results.

Thanks,
imFrank
 
feherke,

With the code so far, I'm getting more output than I want. Within the blocks of data, I only want to print out the data when @NAME is equal to ASALARM_HBT_SS. And when @NAME is equal to ASALARM_HBT_SS, I want to also print the value following @ENABLE_LIMITS.

In the sample data I provided, @ENABLE_LIMITS is at the beginning of the each sample record. Unfortuantely @ENABLE_LIMITS is not always at the beginning of a record. Thus I was trying to find /@ENABLE_LIMITS/ in the block by specifying a string match. I need to know how to search for @ENABLE_LIMITS in the block and pull out its value.

Here's what I have so far:

BEGIN { RS = "" }
{

if ($1~/@NAME/ && $2~/ASALARM_HBT_SS/) ; a=$2

/@ENABLE_LIMITS:/ ;{b=$5}

a!="" && b!="" ; printf "%s %s", FILENAME "\nNAME: " a, "\nENABLE: "b "\n" ; a=b=""


/@FMT/ ;printf "\n NEXT PARAM \n\n"

}


ws20 26% awk -f parsrch3TTA_R3.awk data.file
data.file
NAME: 5.13
ENABLE:

NEXT PARAM

data.file
NAME: 10
ENABLE:

NEXT PARAM

data.file
NAME: ASALARM_HB
ENABLE: @DESC:

NEXT PARAM

data.file
NAME: ASALARM_HBT_CS
ENABLE: @DESC:

NEXT PARAM

data.file
NAME: ASALARM_HBT_SS
ENABLE: @DESC:

NEXT PARAM

data.file
NAME: ASALARM_STALL
ENABLE: @DESC:

NEXT PARAM

ws20 27%



I know the "/@ENABLE_LIMITS:/ ;{b=$5}" is obviously wrong, but it's a lot better than when I had {b=$0}.

Frank
 
Hi

I'm sorry to say, but you really have to read some [tt]awk[/tt] documentation, starting with it's man page. What you done with braces and semicolons is a huge nothing. For example :
[tt]
/@ENABLE_LIMITS:/ ;{b=$5}
[green]\_____________/[/green] [blue]|[/blue] [purple]\__/[/purple]
[/tt]
If input line match the pattern ( marked with green ), then execute the following command ( marked with blue ). After the semicolon a new block follows ( marked with purple ), this one without any pattern, so will be always executed.

Please look at my second post ( 13 Aug 05 4:40 ), I hope the use of braces and semicolons is clear.

Feherke.
 
feherke,

Sorry for the trouble. I've copied and pasted the code you provided (13 Aug) and it works as is. Thank you for taking time for the detailed explaination of code syntax.

imFrank
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top