Visit Our UNIX and Linux User Community

Awk to match a pattern and perform a search after the first pattern

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk to match a pattern and perform a search after the first pattern
# 1  
Old 09-06-2012
Awk to match a pattern and perform a search after the first pattern

Hello Guyz

I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline


In the above text i need to search for EFG first and then keeping EFG as the starting point, i need to search for LMN. I should not get the LMN before EFG.

Any help would be greatly appreciated!!

# 2  
Old 09-06-2012
awk '/EFG/{p=1}/LMN/&&p{print "LMN at line "NR}' file

This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 09-06-2012
Thanks Elixir. That works like a charm. can you please explain how it works.
# 4  
Old 09-06-2012
awk '/EFG/{p=1}         # set variable p to 1 when the pattern EFG is found
/LMN/&&p{               # when pattern LMN is found AND p is set (when EFG has been found)
print "LMN at line "NR} # include line number to make sure that the correct record is selected' file

This User Gave Thanks to elixir_sinari For This Post:
# 5  
Old 09-06-2012
Thanks again elixir. Even if the pattern EFG is not found, can i still search for LMN. In that case my output should produce 2 lines
# 6  
Old 09-06-2012
That would be as simple as:
awk '/LMN/' file

# 7  
Old 09-07-2012
Yeah. i agree. But i wanted to search for EFG, if EFG is found the output should print LMN. if EFG is not found, then my output should print LMN twice. Can i combine that in a single awk script

Previous Thread | Next Thread
Test Your Knowledge in Computers #579
Difficulty: Easy
A string is a disordered sequence of characters.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

awk pattern match by looping through search patterns

Hi I am using Solaris 5.10 & ksh Wanted to loop through a pattern file by reading it and passing it to the awk to match that value present in column 1 of rawdata.txt , if so print column 1 & 2 in to Avlblpatterns.txt. Using the following code but it seems some mistakes and it is running for... (2 Replies)
Discussion started by: ananan
2 Replies

3. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

4. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

5. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

6. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

7. Shell Programming and Scripting

AWK match $1 $2 pattern in file 1 to $1 $2 pattern in file2

Hi, I have 2 files that I have modified to basically match each other, however I want to determine what (if any) line in file 1 does not exist in file 2. I need to match column $1 and $2 as a single string in file1 to $1 and $2 in file2 as these two columns create a match. I'm stuck in an AWK... (9 Replies)
Discussion started by: right_coaster
9 Replies

8. Shell Programming and Scripting

awk pattern match and search in single statement

Hi All, I am trying to alter all lines between EXEC SQL and END-EXEC that have an INCLUDE in them. The following code search="INCLUDE " cp -f ${WORK}/$ ${WORK}/$file.wrk2 for item in `echo $search `; do > ${WORK}/$file.wrk1 awk -vITEM="$item" '{ if ( $0... (3 Replies)
Discussion started by: Bruble
3 Replies

9. Shell Programming and Scripting

Use to awk to match pattern, and print the pattern

Hi, I know how to use awk to search some expressions like five consecutive numbers, , this is easy. However, how do I make awk print the pattern that is been matched? For example: input: usa,canada99292,japan222,france59664,egypt223 output:99292,59664 (6 Replies)
Discussion started by: grossgermany
6 Replies

10. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

Featured Tech Videos