dynamic match thru awk


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users dynamic match thru awk
# 1  
Old 08-17-2007
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 = .
# 2  
Old 08-17-2007
Code:
awk -F"|" '/inputvalue/ { print $3 }' filename

what is the corresponding field when you say input search name ?
# 3  
Old 08-17-2007
Give a try,

Code:
#! /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);

# 4  
Old 08-17-2007
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.
# 5  
Old 08-17-2007
Code:
cat your_file | tr "|" "\n" | grep -x "searchField="

or

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

# 6  
Old 08-17-2007
Quote:
Originally Posted by manas_ranjan
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 = .

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.

Last edited by fazliturk; 08-17-2007 at 10:48 AM..
# 7  
Old 08-17-2007
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.
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