Search a String between start and end of a block in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search a String between start and end of a block in a file
# 1  
Old 02-22-2018
Linux Search a String between start and end of a block in a file

Hi,

I have a scenario where I want to display the output based on the pattern search between the start and end of a block in a file, we can have multiple start and end blocks in a file.
Example give below, we need to search between the start block abc and end block def in a file, after that display the block which has the word "123" in a line. I have tried this command:
Code:
awk '/^abc$/{flag=1}/^def$/{print;flag=0}flag' values.txt

, but how can we code to identify the block which has 123 word and print that entire block(refer expected out below). Please guide me in resolving this issue. Thanks all in Advance for helping!

Input file: values.txt
Code:
abc
xyz123
456
def
ghi
abc
456
def
abc
123
def

Expected Output:
Code:
abc
xyz123
456
def
abc
123
def


Last edited by Chubler_XL; 02-22-2018 at 08:23 PM.. Reason: Please use [code] and [/code] tags around data and programs
# 2  
Old 02-22-2018
store the string in a variable and test it contains 123 before printing it:

Code:
awk '
/^abc$/{flag=1;block=""}
flag{if(length(block)) block = block "\n" $0
     else block=$0 }
/^def$/ && block ~ "123" { print block }' values.txt


Last edited by Chubler_XL; 02-22-2018 at 08:34 PM.. Reason: Fixed code logic - incorrectly built block variable
# 3  
Old 02-22-2018
Code:
awk '$0~a,$0~b{l=l$0 RS;if($0~b){if(l~c)printf l;l=_}}' a="^abc$" b="^def$" c="123" values.txt

# 4  
Old 02-22-2018
Linux

Quote:
Originally Posted by Chubler_XL
store the string in a variable and test it contains 123 before printing it:

Code:
awk '
/^abc$/{flag=1;block=""}
flag{if(length(block)) block = block "\n" $0
     else block=$0 }
/^def$/ && block ~ "123" { print block }' values.txt

Quote:
Thanks for the code snippetSmilie, And can we search multiple strings like either "123" or "456" if it didn't find 123 then it has to check for 456. How can we include "OR" condition?Smilie
---------- Post updated at 07:37 PM ---------- Previous update was at 07:15 PM ----------

Quote:
Originally Posted by rdrtx1
Code:
awk '$0~a,$0~b{l=l$0 RS;if($0~b){if(l~c)printf l;l=_}}' a="^abc$" b="^def$" c="123" values.txt

Quote:
I got an error "ran out of here" while running for the original file
# 5  
Old 02-23-2018
I think the flag should control a bit more. For example a further abc within a block should be ignored. Dito a further def outside the block.
The logical OR is ||
Code:
awk '{
  if (flag==0) {
    if (/^abc$/) {
      flag=1; block=$0
    }
  } else {
    block=(block RS $0)
    if (/^def$/) {
      flag=0; if (block ~ "123" || block ~ "456") print block
    }
  }
}' values.txt


Last edited by MadeInGermany; 02-23-2018 at 05:09 PM.. Reason: RS not FS
# 6  
Old 02-23-2018
You can use sed for this task.
Code:
start='abc'
end='def'
search1='123'
search2='456'
sed '/^'"${start}"'/!d;:A;N;/\n'"${end}"'/!bA;/'"${search1}"'/bB;/'"${search2}"'/!d;:B' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do delete certain lines alone which are matching with start and end string values in file?

Hi, In my previous post ( How to print lines from a files with specific start and end patterns and pick only the last lines? ), i have got a help to get the last select statement from a file, now i need to remove/exclude the output from main file: Input File format: SELECT ABCD, DEFGH,... (2 Replies)
Discussion started by: nani2019
2 Replies

2. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

3. UNIX for Beginners Questions & Answers

Search a string inside a pattern matched block of a file

How to grep for searching a string within a begin and end pattern of a file. Sent from my Redmi 3S using Tapatalk (8 Replies)
Discussion started by: Baishali
8 Replies

4. Shell Programming and Scripting

How can I search with start and end criteria?

Hello I'm using cygwin and wouldlike extract information from an xml file according specific values, but don't know how. Let's say in a file content looks like this: <tab> SURNAME=Mustermann NAME=Max CUSTOMER SINCE= 18.01.2000 ADDRESS=Birmingham ... (2 Replies)
Discussion started by: witchblade
2 Replies

5. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

6. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

7. Shell Programming and Scripting

how to specify start and stop of a search string

I am trying to extract a string from a line of text. Currently I am using grep -o 'startofstring(.........' The string is not always the same size. The string I'm trying to extract starts with 'test(' ends with ')'. ex "blah,blah,blah,test(stringoftext),blah blah" How do I... (4 Replies)
Discussion started by: jeepguy
4 Replies

8. Shell Programming and Scripting

read a string from its end to its start and stop when you find a specific character

How can I do this? Actually I have a file which contains a path e.g. /home/john/Music/hello.mp3 and I want to take only the filename (hello.mp3) So, I need to read the file from its end to its start till the character "/" Is this possible? Thanks, I am sure you'll not disappoint me here! Oh,... (9 Replies)
Discussion started by: hakermania
9 Replies

9. Shell Programming and Scripting

Appending string, variable to file at the start and string at end

Hi , I have below file with 13 columns. I need 2-13 columns seperated by comma and I want to append each row with a string "INSERT INTO xxx" in the begining as 1st column and then a variable "$node" and then $2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13 and at the end another string " ; COMMIT;" ... (4 Replies)
Discussion started by: Vaddadi
4 Replies

10. Shell Programming and Scripting

String as both start and end anchors in awk

Not sure if the title of this thread makes sense, but hopefully my explanation will. I'm using awk to print some stats from an apache accesslog. I would like to specify the regexp condition where only the two root pages of "index.html" and "/" are counted in my results. What I can't figure out... (3 Replies)
Discussion started by: picassolsus
3 Replies
Login or Register to Ask a Question