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;

[appln name1]
SName=abc123
ESys=xyz456
Host=xxx
Port=yyy

I used awk and didn't get any output for multi-pattern search.

$ awk -F"=" '/SName/ && /ESys/ {print}' file_name

Requesting help on this. (OS; Redhat 2.6.18)

Hello sam_bd,

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

awk '/SName/ || /ESys/'   Input_file

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

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

Thanks,
R. Singh

Tried with

awk '/SName/ || /ESys/'   Input_file

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

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)

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.

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

Thanks,
R. Singh

1 Like

Thanks R.Singh.

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?

Hello sam_bd,

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

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

1 Like

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.

Hi.

Using standard commands, and the very small sample data set; the operative commands here are:

grep -E 'SName|ESys' $FILE |
paste - - 

In this script and output:

#!/usr/bin/env bash

# @(#) s1       Demonstrate matching, joining.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C grep paste pass-fail

FILE=${1-data1}
E=expected-output.txt

pl " Input data file $FILE:"
cat $FILE

pl " Expected output:"
cat $E

pl " Results:"
grep -E 'SName|ESys' $FILE |
paste - - | tee f1

pl " Verify results if possible:"
C=$HOME/bin/pass-fail
[ -f $C ] && $C || ( pe; pe " Results cannot be verified." ) >&2

exit 0

producing:

$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.8 (jessie) 
bash GNU bash 4.3.30
grep (GNU grep) 2.20
paste (GNU coreutils) 8.23
pass-fail (local) 1.9

-----
 Input data file data1:
SName=abc123
ESys=xyz456
Host=xxx
Port=yyy

-----
 Expected output:
SName=abc123  ESys=xyz456

-----
 Results:
SName=abc123    ESys=xyz456

-----
 Verify results if possible:

-----
 Comparison of 1 created lines with 1 lines of desired results:
f1 expected-output.txt differ: char 13, line 1
 Failed -- files f1 and expected-output.txt not identical -- detailed comparison follows.
 Succeeded by ignoring whitespace differences.

Best wishes ... cheers, drl