Print all lines between two keyword if a specific pattern exist
I have input file as below I need to check for a pattern and if it is there in file then I need to print all the lines below BEGIN and END keyword. Could you please help me how to get this in AIX using sed or awk.
Input file:
If I am looking for Amit then Output should be
If I am looking for Rahul then Output should be
If I am looking for XYZ then Output should be nothing.
From below command I am able to get all the data between two keyword but not able to grep the data.
From below command I am able to get all the data between two keyword but not able to grep the data.
This command prints everything between a line containing "BEGIN" and one containing "END", regardless of what is in between.
Your problem is, if i have understood correctly, is to only print these lines if some condition (a line containing a third word, like "Amit") is met. Here is how you solve these problems with sed:
The first thing you need is: you have to store the text in question somewhere until you decide if you print it or not. For this there is the "hold space". This is a text buffer you can manipulate separately from the "pattern space". It will maintain its content across the processing of lines. See the man page of "sed", the commands "g", "h", "x", "G" and "H". The principle being when you encounter a line with "BEGIN" you start a new cycle: put the encountered line in the hold space. From here on you append every line to there and accumulate the text this way until you encounter a line with "END" in it. When you finally encounter a line with "END" in it you clean out the hold space and start over again.
The second task is to decide if the text should be printed or not: When you encounter a line with END this ends the cycle: move all text accumulated in the hold space back to the pattern space, search the text there for your search string "Amit" and either print the whole text or discard it.
Or, the same in one line:
I hope this helps.
bakunin
/PS: only now i saw that Anbu23 has already posted a sed solution which works the same way. My script and his second solution are quite similar but his way of cleaning the hold space is better than mine, so i suggest you use his.
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,288
Thanks Given: 430
Thanked 480 Times in 395 Posts
Hi.
There are grep-like utilities that are designed to handle these kinds of situations. Here is one called cgrep:
producing:
and
and
The source for cgrep can be found at the site mentioned in the comments in the demonstration script above.
[QUOTE=anbu23;302952135]
Could you explain the part
of your command line please, it's confuse for me.
I've understood that for each block /BEGIN/,/END/ you do this { str = str ? str "\n" $0 : $0 }, i know the result but it's not clear for me
Thx.
Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" .
I have attached sample input file and the desired input should be as:
INPUT FORMAT:
SELECT
ABCD,
DEFGH,
DFGHJ,
JKLMN,
AXCVB,... (5 Replies)
I have multi line input(var1) and reference(var2) variables.
How to capture lines not present in var2 but present in var1?
How to capture lines present var2 but not in var1?
# configuration from server
var1="""
Custom JAX-RS
Custom Shared
Web 2.0
"""
# required configuration... (6 Replies)
The intended result should be :
PDF converters
'empty line'
gpdftext and pdftotext?xml version="1.0"?>
xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters
gpdftext and pdftotext</note-content>... (9 Replies)
Hello Experts , require help . See below output:
File inputs
------------------------------------------
Server Host = mike
id rl images allocated last updated density
vimages expiration last read <------- STATUS ------->... (4 Replies)
I'm still beginner and maybe someone can help me.
I have this input:
the great warrior a, b, c
and what i want to know is, with awk, how can i detect the string with 'warrior' string on it and print the a, b, and c seperately, become like this :
Warrior Type
a
b
c
Im still very... (3 Replies)
I regularly extract lines of text from files based on the presence of a particular keyword; I place the extracted lines into another text file. This takes about 2 hours to complete using the "sort" command then Kate's find & highlight facility.
I've been reading the forum & googling and can find... (4 Replies)