Awk/sed command to extract the string between 2 patterns but having some particular value


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Awk/sed command to extract the string between 2 patterns but having some particular value
# 1  
Old 09-23-2019
Awk/sed command to extract the string between 2 patterns but having some particular value

Hi -

i have one file with content as below.

Code:
***** BEGIN 123 *****
BASH is awesome
***** END *****
***** BEGIN 365 *****
KSH is awesome
***** END *****
***** BEGIN 157 *****
KSH is awesome
***** END *****
***** BEGIN 7123 *****
C is awesome
***** END *****

I am trying to find all the lines in between BEGIN and END but having KSH is awesome

I was using awk '/BEGIN/,/END/' MyFileName
but it is giving me all the content. How do i extract only below 2, kind of having clause between the 2 strings.

***** BEGIN 365 *****
KSH is awesome
***** END *****
***** BEGIN 157 *****
KSH is awesome
***** END *****

Pls suggest a way around
# 2  
Old 09-23-2019
Untested:
Code:
awk '{store=(store ORS $0)} /BEGIN/ {store=$0} /END/ && store ~ /KSH is awesome/ {print store}' MyFileName

One store variable -> can be done with sed and its hold space.
Code:
sed -n 'H; /BEGIN/h; /END/!d; x; /KSH is awesome/p' MyFileName

These 2 Users Gave Thanks to MadeInGermany For This Post:
# 3  
Old 09-23-2019
Hi,

For something like this, just use.

Code:
awk '/KSH is awesome/ { print $0 }' MyFilename

Didn't read the post properly, however I see it's been answered.

Regards

Gull04

Last edited by gull04; 09-23-2019 at 07:36 AM.. Reason: Didn't read the post properly;
# 4  
Old 09-23-2019
this will only show the lines with KSH is awesome .. it will not showing the lines between BEGIN/END

--- Post updated at 08:14 AM ---

MadeInGermany
your command mention below is working fine (though difficult to remember for future uses Smilie ]

Code:
awk '{store=(store ORS $0)} /BEGIN/ {store=$0} /END/ && store ~ /KSH is awesome/ {print store}' MyFileName

thanks a lot, appreciate your help on this.

Moderator's Comments:
Mod Comment Please wrap your samples in CODE TAGS as per forum rules.

Last edited by RavinderSingh13; 09-23-2019 at 08:32 AM..
# 5  
Old 09-23-2019
Hi,

Yes I was aware of the mistake, however by the time that I went in to rectify it -Madein Germany had posted two solutions, which is why I updated the post the way I did.

Regards

Gull04
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed, awk or another bash command to modify string with the content of another file

Hello everybody, I would like modify some strings using sed or another command line with the content file. For example: - {fqdn: "server-01" , ip: "server-01"} - {fqdn: "server-02" , ip: "server-02"} - {fqdn: "server-03" , ip: "server-03"} - {fqdn: "server-04" , ip: "server-04"} My... (4 Replies)
Discussion started by: dco
4 Replies

2. Shell Programming and Scripting

awk extract strings matching multiple patterns

Hi, I wasn't quite sure how to title this one! Here goes: I have some already partially parsed log files, which I now need to extract info from. Because of the way they are originally and the fact they have been partially processed already, I can't make any assumptions on the number of... (8 Replies)
Discussion started by: chrissycc
8 Replies

3. Shell Programming and Scripting

String search between patterns using sed

Hi, I am trying to find a way to get sed/awk/grep to help me find a string in a log file that exists between two datestamps and then print the preceding datestamp up to the next datestamp. Here is an example of my logfile: +++ 2013/03/28 17:01:37.085 SIGNALING HIGH ACTIVE Failure Response... (5 Replies)
Discussion started by: raytx
5 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. Shell Programming and Scripting

Patterns with egrep/sed/awk?

I have an array with characters, what I want is if there are other characters in the array which I am looking for than take action that is print BAD ARRAY. So far my code just finds characters but instead I want that it should look for other characters. echo "A B C D F" | egrep -o "D | F" O/P... (5 Replies)
Discussion started by: dixits
5 Replies

6. Shell Programming and Scripting

Sed/awk print between different patterns the first occurrence

Thanks for the help yesterday. I have a little modification today, I am trying the following: i have a log file of a webbap which logs in the following pattern: 2011-08-14 21:10:04,535 blablabla ERROR Exception1 blablabla bla bla bla bla 2011-08-14... (2 Replies)
Discussion started by: ppolianidis
2 Replies

7. Shell Programming and Scripting

Use grep sed or awk to extract string from log file and put into CSV

I'd like to copy strings from a log file and put them into a CSV. The strings could be on different line numbers, depending on size of log. Example Log File: File = foo.bat Date = 11/11/11 User = Foo Bar Size = 1024 ... CSV should look like: "foo.bat","11/11/11","Foo Bar","1024" (7 Replies)
Discussion started by: chipperuga
7 Replies

8. Shell Programming and Scripting

deleting lines between patterns using sed or awk

hi, Here is excerpt from my xml file <!-- The custom module to do the authentication for LDAP --> </login-module> <login-module code="com.nlayers.seneca.security.LdapLogin" flag="sufficient"> <module-option... (1 Reply)
Discussion started by: sunrexstar
1 Replies

9. Shell Programming and Scripting

awk or sed command to print specific string between word and blank space

My source is on each line 98.194.245.255 - - "GET /disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=&drc=&mo=&sid=&lang=EN&loc=JPN HTTP/1.1" 302 - "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR... (5 Replies)
Discussion started by: elamurugu
5 Replies

10. Shell Programming and Scripting

To extract the string between two patterns

Sample input: Loading File System Networking in nature Closing the System now i need to extract the patterns between the words File and Closing: i.e. sample output: System Networking in Nature Thanks in advance !!!!!!!!!!!!!!!!! (6 Replies)
Discussion started by: aajan
6 Replies
Login or Register to Ask a Question