dynamic match thru awk


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users dynamic match thru awk
# 8  
Old 08-17-2007
I really doubt if we could use sed here, bcoz there is no specific pattern here.
# 9  
Old 08-17-2007
hi lorcan,

any suggestion thru AWK ????
# 10  
Old 08-17-2007
Quote:
Originally Posted by manas_ranjan
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 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.
# 11  
Old 08-21-2007
Quote:
Originally Posted by manas_ranjan
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.
this is dynamic,try this

cat inputfile |awk -F'|' -v srn=$1
'{count=0; while (count < NF )
{count ++; if (index($count,srn)){print $count}}}'
# 12  
Old 08-21-2007
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 }
# 13  
Old 08-21-2007
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

# 14  
Old 08-21-2007
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
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