dynamic match thru awk

hey ,

my i/p text looks like this,
FILE_TYPE=01|FILE_DESC=Periodic|FILE_SCHDL_TYPE=Daily|FILE_SCHDL=|FILE_SCHDL_TIME=9:00am|RESULTS=B
FILE_TYPE=02|FILE_DESC=NCTO|FILE_SCHDL_TYPE=Daily|FILE_SCHDL=|FILE_SCHDL_TIME=9:00am|RESULTS=M

NOTE Look carefully for the position FILE_TYPE,FILE_DESC etecetra etcetra

this position may varry, e.g. FILE_TYPE is here in 1st postion might be to-morrow in the 3rd or last postion,

So my query is to give the input search name, and it should display the field value, FILE_TYPE and value after = .

awk -F"|" '/inputvalue/ { print $3 }' filename

what is the corresponding field when you say input search name ?

Give a try,

#! /usr/bin/perl

$inpFile=$ARGV[0];
$searchStr=$ARGV[1];
open (INP,"<$inpFile") || die "Unable to open INPFILE::$inpFile\n";
while (<INP>) {
        $_ =~ s/\x0a|\x0d//g;
        @inpTag = split(/\|/,$_);
        foreach $value ( @inpTag ) {
                ($key,$val)  = (split ( /=/,$value ));
                if ( $key =~ /\b$searchStr\b/ ) {
                        print "$key= $val\n";
                }
        }
}
close (INP);

hey matrixmadhan, Field is not static, it might be changed across the file.
so according to the i/p search value, i want to fetch the field name and value .

hey lorcan , thanx a lot..can i have this in unix...not thru perl.

cat your_file | tr "|" "\n" | grep -x "searchField="

or

tr "|" "\n" < your_file | grep -x "searchField="

code is this ;

cat inputfile |awk -F'|' -v srchname=$1 ' $1 ~ srchname {print $1};
$2 ~ srchname {print $2};$3 ~ srchname {print $3};$4 ~ srchname {print $4};
$5 ~ srchname {print $5};$6 ~ srchname {print $6}'

the first $1 is command line parameter comes from outside the script. so you can use your script this way from command line or call from an another program ;

myscript FILE_TYPE or myscript FILE_DESC etc.

hey fazliturk,

thanx a lot...but if the field is going to change i.e. might be increased from 6 to 7 then , in real time it is not feasible to change the query to add one line $7 ~ srchname {print $7} !!!!!!

So i want irrespective of all the changes to i/p file, that we can think of, it should give us the o/p of the proper search value....but anyway thanx a lot.

lorcan, i tried your one it works fine , with some modification according to my requirement ....But still can we have some more modularized one...using AWK or SED.

I really doubt if we could use sed here, bcoz there is no specific pattern here.

hi lorcan,

any suggestion thru AWK ????

i tried this code it works fine . You don't have to use 6 or 7 fields . you can can write more than from the possibilities. for e.g print $8,..print 12 etc.

this is dynamic,try this

cat inputfile |awk -F'|' -v srn=$1
'{count=0; while (count < NF )
{count ++; if (index($count,srn)){print $count}}}'

hey fazliturk.

Thanx a lot, it's working.....
can i put the o/p into arrays ,
e.g. if my srch pattern "FILE_TYPE"
then the values of FILE_TYPE should store in array of file_type .

let think of my current sample,

1 i have given the search pattern as "FILE_TYPE"
then the query should store the FILE_TYPE values into array "FILE_TYPE"[index] { index is 1,2,3......etc }

2 i have given my next search pattern as "FILE_DESC"
then the query should store the FILE_DESC values into array "FILE_DESC"[index] { index is 1,2,3......etc }

Try...

$ cat file1
FILE_TYPE=01|FILE_DESC=Periodic|FILE_SCHDL_TYPE=Daily|FILE_SCHDL=|FILE_SCHDL_TIME=9:00am|RESULTS=B
FILE_TYPE=02|FILE_DESC=NCTO|FILE_SCHDL_TYPE=Daily|FILE_SCHDL=|FILE_SCHDL_TIME=9:00am|RESULTS=M

$ eval $(awk -F \| -v find=FILE_DESC '{
>   for(f=1;f<=NF;f++)
>     if(match($f,find))
>       printf "%s[%d]=\"%s\"\n", substr($f,1,RLENGTH), ++c, substr($f,RLENGTH+2)
>  }' file1)

$ echo ${FILE_DESC[1]}
Periodic

$ echo ${FILE_DESC[2]}
NCTO

thanx Ygor,

It's working........can i take the count of match of search pattern,
so that in outside i can call those variable thru loop,

e.g. i have given search pattern as FILE_DESC and i got 2 matches of the same search pattern,

so
while [ $var < $total ]
do
echo ${FILE_DESC[$var]}
var=`expr $var + 1`
done

hey dude,

if i am going to put all this into one function, and trying to call that function
with the variable as search pattern, then it is showing me END OF FILE error,

any help !!!!!

Issue resolved...i missed out a single quote , so for that reason it was showing END OF FILE Error.

All,

I have a script which looks as follows,

fileConfigTable_length=( "XX"
"FILE_TYPE"
"CS_ID"
"FILE_DESC"
"FILE_SCHDL_TYPE"
"FILE_SCHDL"
"FILE_SCHDL_TIME"
"RESULT"
"UNIT_TYPE"
"UNIT_NAME")
function fetch_Records
{
var=$1
eval $(awk -F \| -v find=$var '{
for(f=1;f<=NF;f++)
if(match($f,find))
printf "%s[%d]=\"%s\"\n", substr($f,1,RLENGTH), ++c, substr($f,RLENGTH+2)
}' noname.txt )
}

total_Array=${#fileConfigTable_length[*]}
i=1
while [ $i -lt $total_Array ]
do
fetch_Records ${fileConfigTable_length[$i]}
i=`expr $i + 1`
done

and noname.txt is

FILE_TYPE=01|FILE_DESC=Periodic|FILE_SCHDL_TYPE=Daily|FILE_SCHDL=|FILE_SCHDL_TIME=9:00am|RESULTS=B|UNIT_TYPE=PEP|UNIT_NAME=BUNL001PEP
FILE_TYPE=02|FILE_DESC=NCTO|FILE_SCHDL_TYPE=Daily|FILE_SCHDL=|FILE_SCHDL_TIME=9:00am|RESULTS=M|UNIT_TYPE=SAN|UNIT_NAME=BUNL001SAN

The problem starts when the search pattern is FILE_SCHDL,
as it is showing the values as follows

FILE_SCHDL[1]="TYPE=Daily" FILE_SCHDL[2]="" FILE_SCHDL[3]="TIME=9:00am" FILE_SCHDL[4]="TYPE=Daily" FILE_SCHDL[5]="" FILE_SCHDL[6]="TIME=9:00am"

[ these are combination of values for FILE_SCHDL_TYPE,FILE_SCHDL_TIME and FILE_SCHDL ]

can any body suggest how to make the exact match in my above script ..
so that it will give me exact match ???

The scripts works fine for everything other than search pattern FILE_SCHDL

FILE_SCHDL[1]="TYPE=Daily" FILE_SCHDL[2]="" FILE_SCHDL[3]="TIME=9:00am" FILE_SCHDL[4]="TYPE=Daily" FILE_SCHDL[5]="" FILE_SCHDL[6]="TIME=9:00am"

if i put my serach pattern as FILE_SCHDL, it gives all the combination of
values FILE_SCHDL_TYPE,FILE_SCHDL and FILE_SCHDL_TIME from i/p file.

can you help me out modifying the query for exact match with array ????

is it too tough for GURUS.......?????:smiley:

Please read the forum rules.

(4) Do not 'bump up' questions if they are not answered promptly. No duplicate or cross-posting and do not report a post or send a private message where your goal is to get an answer more quickly.

The two threads have been merged.

To be honest, I can't see the point in posting a question twice, sending a private message, and 'bumping-up' your post after an hour. I'm not even in the same time-zone.