Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Multi pattern selection

Shell Programming and Scripting


Tags
solved, solved awk

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-12-2017
sam_bd sam_bd is offline
Registered User
 
Join Date: May 2011
Last Activity: 25 September 2017, 9:16 AM EDT
Posts: 72
Thanks: 20
Thanked 1 Time in 1 Post
Multi pattern selection

I'm confused with what to use (awk or grep) in this case as i need to select 2 corresponding patterns. "SName" & "ESys" in a appln config file which looks like this;
HTML Code:
[appln name1]
SName=abc123
ESys=xyz456
Host=xxx
Port=yyy
I used awk and didn't get any output for multi-pattern search.
HTML Code:
$ awk -F"=" '/SName/ && /ESys/ {print}' file_name
Requesting help on this. (OS; Redhat 2.6.18)
Sponsored Links
    #2  
Old Unix and Linux 07-12-2017
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 26 September 2017, 2:02 PM EDT
Location: Chennai
Posts: 2,609
Thanks: 573
Thanked 1,238 Times in 1,116 Posts
Hello sam_bd,

Could you please try following and let me know if this helps you.

Code:
awk '/SName/ || /ESys/'   Input_file

If you want to have the values after =, then following may help you in same.

Code:
awk -F"=" '/SName/ || /ESys/{print $2}'  Input_file

Thanks,
R. Singh
Sponsored Links
    #3  
Old Unix and Linux 07-12-2017
sam_bd sam_bd is offline
Registered User
 
Join Date: May 2011
Last Activity: 25 September 2017, 9:16 AM EDT
Posts: 72
Thanks: 20
Thanked 1 Time in 1 Post
Tried with
Code:
awk '/SName/ || /ESys/'   Input_file

but not getting output as expected. i need output in following manner:

Code:
SName=abc123  ESys=xyz456
SName=cdf234  ESys=gfh768
SName=rfg346  ESys=kls983

so it needs to be side-by-side. corresponding values as in main input file(which as one below another)
    #4  
Old Unix and Linux 07-12-2017
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 26 September 2017, 2:02 PM EDT
Location: Chennai
Posts: 2,609
Thanks: 573
Thanked 1,238 Times in 1,116 Posts
Hello sam_bd,

As your sample Input_file is NOT clear, still on few assumption on question. Could you please try following and let me know if this helps you.

Code:
awk -F"=" '/SName/{VAL=$0;next} /ESys/{print VAL,$0}'   Input_file

Thanks,
R. Singh
The Following User Says Thank You to RavinderSingh13 For This Useful Post:
sam_bd (07-12-2017)
Sponsored Links
    #5  
Old Unix and Linux 07-12-2017
sam_bd sam_bd is offline
Registered User
 
Join Date: May 2011
Last Activity: 25 September 2017, 9:16 AM EDT
Posts: 72
Thanks: 20
Thanked 1 Time in 1 Post
Thanks R.Singh.
Code:
awk -F"=" '/SName/{VAL=$0;next} /ESys/{print VAL,$0}'   Input_file

This code looks to be working on my first try. Input file is just what i've mentioned in my first question post. In the input file the above mentioned pattern repeats with different SName and ESys values. I'm trying to understand this code; VAL=$0. will this hold SName values?
Sponsored Links
    #6  
Old Unix and Linux 07-12-2017
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 26 September 2017, 2:02 PM EDT
Location: Chennai
Posts: 2,609
Thanks: 573
Thanked 1,238 Times in 1,116 Posts
Hello sam_bd,

Could you please go through the following explanation and let me know if this helps you.

Code:
awk -F"="  '                           ###Set field seprator as "=" here.
  /SName/{               ###Looking for string SName in any line, if that is present i any line then perform following.
   VAL=$0;        ###create variable VAL and assign its value to current line.
   next           ###next is awk built-in keyword which will SKIP all next statements now.
                       }
        /ESys/  {               ###Searching for string "ESys" here in any line, if that is present then perform following.
   print VAL,$0   ###Print the value of variable VAL and current line too.
         }
           '  Input_file               ###Mentioning the Input_file here, which is getting processes by awk.

Thanks,
R. Singh
The Following User Says Thank You to RavinderSingh13 For This Useful Post:
sam_bd (07-12-2017)
Sponsored Links
    #7  
Old Unix and Linux 07-12-2017
sam_bd sam_bd is offline
Registered User
 
Join Date: May 2011
Last Activity: 25 September 2017, 9:16 AM EDT
Posts: 72
Thanks: 20
Thanked 1 Time in 1 Post
Yes. This break-up explanation of the code you gave really helping me. I'm introduced to new form of awk esp. next command, placing current search value in a variable & later printing with the second search pattern. Altogether this is new to me. Thank you very much.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Multi-line filtering based on multi-line pattern in a file Finja Shell Programming and Scripting 2 05-16-2012 10:42 AM
Print between multi line pattern sol_nov Shell Programming and Scripting 3 02-27-2012 05:41 PM
Sed or awk : pattern selection based on special characters usha rao Shell Programming and Scripting 6 09-13-2011 12:30 PM
how can awk match multi pattern in a string sleepy_11 UNIX for Advanced & Expert Users 5 03-16-2008 11:40 PM



All times are GMT -4. The time now is 02:05 PM.