Multi pattern selection


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multi pattern selection
# 1  
Old 07-12-2017
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)
# 2  
Old 07-12-2017
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
# 3  
Old 07-12-2017
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 07-12-2017
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 07-12-2017
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?
# 6  
Old 07-12-2017
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 07-12-2017
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Scripting wpa_supplicant for multi-profile selection

Hi all. So, first of all my apologies for the noob question. I've searched around for a way to make use of bash arrays to perform what I need, but I'm struggling to understand which method would be better. So, I'm looking to get a bit more comfortable with scripting, hoping to use... (1 Reply)
Discussion started by: Techbart
1 Replies

2. Shell Programming and Scripting

How to find all the multi line pattern and redirecting it to a file?

I've a file like this {multi line ....... ....... pattern} { some other stuff ......... } {multi line ....... ....... pattern} { some other stuff ......... } and so on (2 Replies)
Discussion started by: aamir_raihan
2 Replies

3. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

4. Shell Programming and Scripting

sed multiple multi line blocks of text containing pattern

Hi, I have a log file which has sessionids in it, each block in the log starts with a date entry, a block may be a single line or multiple lines. I need to sed (or awk) out the lines/blocks with that start with a date and include the session id. The files are large at several Gb. My... (3 Replies)
Discussion started by: andyatit
3 Replies

5. Shell Programming and Scripting

How to substract selective values in multi row, multi column file (using awk or sed?)

Hi, I have a problem where I need to make this input: nameRow1a,text1a,text2a,floatValue1a,FloatValue2a,...,floatValue140a nameRow1b,text1b,text2b,floatValue1b,FloatValue2b,...,floatValue140b look like this output: nameRow1a,text1b,text2a,(floatValue1a - floatValue1b),(floatValue2a -... (4 Replies)
Discussion started by: nricardo
4 Replies

6. Shell Programming and Scripting

Multi-line filtering based on multi-line pattern in a file

I have a file with data records separated by multiple equals signs, as below. ========== RECORD 1 ========== RECORD 2 DATA LINE ========== RECORD 3 ========== RECORD 4 DATA LINE ========== RECORD 5 DATA LINE ========== I need to filter out all data from this file where the... (2 Replies)
Discussion started by: Finja
2 Replies

7. Shell Programming and Scripting

Print between multi line pattern

Hi, I have a file with text like this .SET WIDTH 10000 .SET MAXERROR 1 insert into new_db SELECT * FROM some_db ; +---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+- .SET WIDTH 10000... (3 Replies)
Discussion started by: sol_nov
3 Replies

8. Shell Programming and Scripting

Sed or awk : pattern selection based on special characters

Hello All, I am here again scratching my head on pattern selection with special characters. I have a large file having around 200 entries and i have to select a single line based on a pattern. I am able to do that: Code: cat mytest.txt | awk -F: '/myregex/ { print $2}' ... (6 Replies)
Discussion started by: usha rao
6 Replies

9. UNIX for Advanced & Expert Users

how can awk match multi pattern in a string

Hi all, I need to category the processes in my system with awk. And for now, there are several command with similar name, so i have to match more than one pattern to pick it out. for instance: binrundb the string1, 2 & 3 may contain word, number, blank or "/". The "bin" should be ahead "rundb"... (5 Replies)
Discussion started by: sleepy_11
5 Replies
Login or Register to Ask a Question