The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Match words moutaz1983 Shell Programming and Scripting 8 01-07-2008 03:26 AM
Dynamic SQl in KSH kousikan Shell Programming and Scripting 2 03-18-2007 09:12 AM
how to dynamic DNS bondoq Linux 1 11-14-2006 07:34 AM
Dynamic DNS [MA]Flying_Meat OS X (Apple) 0 12-06-2005 06:07 PM
dynamic pid? yls177 UNIX for Dummies Questions & Answers 2 12-17-2002 05:26 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 08-17-2007
manas_ranjan's Avatar
Registered User
 

Join Date: Jul 2007
Location: PUNE
Posts: 157
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 = .
Reply With Quote
Forum Sponsor
  #2  
Old 08-17-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Code:
awk -F"|" '/inputvalue/ { print $3 }' filename
what is the corresponding field when you say input search name ?
Reply With Quote
  #3  
Old 08-17-2007
Registered User
 

Join Date: May 2007
Posts: 211
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);
Reply With Quote
  #4  
Old 08-17-2007
manas_ranjan's Avatar
Registered User
 

Join Date: Jul 2007
Location: PUNE
Posts: 157
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.
Reply With Quote
  #5  
Old 08-17-2007
Registered User
 

Join Date: May 2007
Posts: 211
Code:
cat your_file | tr "|" "\n" | grep -x "searchField="
or

Code:
tr "|" "\n" < your_file | grep -x "searchField="
Reply With Quote
  #6  
Old 08-17-2007
Registered User
 

Join Date: Aug 2007
Posts: 45
Quote:
Originally Posted by manas_ranjan View Post
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 06:48 AM.
Reply With Quote
  #7  
Old 08-17-2007
manas_ranjan's Avatar
Registered User
 

Join Date: Jul 2007
Location: PUNE
Posts: 157
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:05 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0