dynamic match thru awk


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users dynamic match thru awk
# 15  
Old 08-22-2007
Any Help

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 !!!!!
# 16  
Old 08-22-2007
Issue resolved...i missed out a single quote , so for that reason it was showing END OF FILE Error.
# 17  
Old 08-22-2007
Problem while acurate pattern matching.....

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|U NIT_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 ???
# 18  
Old 08-22-2007
Quote:
Originally Posted by Ygor
Try...
Code:
$ 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

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 ????
# 19  
Old 08-22-2007
is it too tough for GURUS.......?????Smilie
# 20  
Old 08-22-2007
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.
# 21  
Old 08-22-2007
So anyway, you need to match the variable name and the following equals sign. Changes highlighted...
Code:
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-1), ++c, substr($f,RLENGTH+1)
   }' noname.txt)

For details on the awk match function, see section: Built-in Functions for String Manipulation of the awk manual.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Executing if dynamic conditions in awk

Hi All, I got struck at the below point where i am unable to get the desired output after forming the dynamic conditions.Below is the design. 1. We are getting inputs from the shell arguments and storing in a variable like below. CONDITIONS="1=CT,2=US_10,3=CT_US_10" 2. After this i am... (14 Replies)
Discussion started by: cskumar
14 Replies

2. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

awk to match field between two files and use conditions on match

I am trying to look for $2 of file1 (skipping the header) in $2 of file2 (skipping the header) and if they match and the value in $10 is > 30 and $11 is > 49, then print the line from file1 to a output file. If no match is foung the line is not printed. Both the input and output are tab-delimited.... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

Request: How to Parse dynamic SQL query to pad extra columns to match the fixed number of columns

Hello All, I have a requirement in which i will be given a sql query as input in a file with dynamic number of columns. For example some times i will get 5 columns, some times 8 columns etc up to 20 columns. So my requirement is to generate a output query which will have 20 columns all the... (7 Replies)
Discussion started by: vikas_trl
7 Replies

6. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

7. Shell Programming and Scripting

how to make pattern search dynamic in awk

Hi, I have a data in a file like below - andy 22 abc 30000 wallstreet paul 30 xyz 40000 martstreet john 35 abc 50000 martstreet I want to search number of employees working in a particular company. Below query executes perfectly - awk '/abc/{ COUNT ++; }END { print "number of... (3 Replies)
Discussion started by: shell123
3 Replies

8. Shell Programming and Scripting

Perl regular expression - To match a Dynamic URL

Hello All, I have a requirement to match a dynamic url and extract each of the directory and page and store it -Only PERL style Regular EXP as it will be used in informatica - REG_EXTRACT function Example Input URLs: 1)... (2 Replies)
Discussion started by: jambesh
2 Replies

9. Shell Programming and Scripting

setting variable value to dynamic sed match - escaping hell

Hello All, I'm trying to write a script that will perform a dynamic match (of a dynamic variable) and set a variable to have the resulting (match) value. The idea is that the environment variable to check ($1) and the regular expression to use ($2) are given as parameters. For example,... (5 Replies)
Discussion started by: aedgar
5 Replies

10. Shell Programming and Scripting

Dynamic filename in awk

Hi The following code seems to work, but why am i getting an error message? cscyabl@comet:(develop)> awk 'BEGIN {FS="|"}{print $2 >> $1}' test.sum awk: A print or getline function must have a file name. The input line number is 8. The file is test.sum. The source line number is 1. ... (2 Replies)
Discussion started by: Indalecio
2 Replies
Login or Register to Ask a Question