AWK: How to extract text lines between two strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK: How to extract text lines between two strings
# 1  
Old 12-02-2009
AWK: How to extract text lines between two strings

Hi.

I have a text test1.txt file like:
Code:
Receipt
Line1
Line2
Line3
End
 
Receipt
Line4
Line5
Line6
Canceled
 
Receipt
Line7
Line8
Line9
End
 
Line10
Line11
 
Receipt
Line12
Line13
Line14
Canceled

As result I need only lines between "Receipt" and "Canceled".

E.g.:

Code:
Receipt
Line4
Line5
Line6
Canceled
 
Receipt
Line12
Line13
Line14
Canceled

I have the following script:
Code:
awk '/Receipt/, $NF ~ /Canceled/ ' test1.txt

But this will extract other lines starting with "Receipt"

I found some other solution wotking with groups, but this is not helping me much:

Code:
awk '\
/Receipt/,/Canceled/ {
if (newgroup==0)
{print substr($0,index($0,"Receipt"))
newgroup=1}
else
{if (match($0,"Canceled"))
{print substr($0,1,index($0,"Canceled")-1)
newgroup=0}
else
print
}
}' test1.txt

Any ideas appreciated. Smilie

Last edited by rbatte1; 07-06-2016 at 08:51 AM.. Reason: Code tags
# 2  
Old 12-02-2009
why bother with long code use the comma operator as below:-


Code:
nawk '/Receipt/, /Canceled/' file.txt

you can use nawk,gawk,/usr/xpg4/bin/awk

SmilieSmilieSmilieSmilieSmilie
# 3  
Old 12-02-2009
If the groups are separated by blank lines, then:
Code:
awk 'BEGIN{FS="\n";RS="";ORS="\n\n"}/Receipt.*Canceled/'

# 4  
Old 12-02-2009
Code:
awk '/Receipt/{s=x}{s=s$0"\n"}/Canceled/{print s}'

# 5  
Old 12-03-2009
Bug

Quote:
Originally Posted by Scrutinizer
Code:
awk '/Receipt/{s=x}{s=s$0"\n"}/Canceled/{print s}'

Thanks a lot!
That's exactly what I need. Smilie
# 6  
Old 01-19-2010
AWK: How to extract text lines between two strings only if the pattern patches

Thanks again Scrutinizer for previous solution, but how to modify it
to get only lines between "Receipt" and "Canceled"
if any line in between contains pattern "Line13"?

Using previous example I would like to have only:

Code:
Receipt
Line12
Line13
Line14
Canceled


Last edited by rbatte1; 07-06-2016 at 08:52 AM.. Reason: Code tags
# 7  
Old 01-19-2010
With a slightly change of the code of Scrutinizer:

Code:
awk '/Receipt/{s=x}{s=s$0"\n"}/Line13/{p=1}/Canceled/ && p{print s;exit}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract text between two strings

Hi, I have a text like these: ECHO "BEGGINING THE SHELL....." MV FILE1 > FILE2 UNIQ_ID=${1} PARTITION_1=`${PL}/Q${CON}.KSH "SELECT ....." PARTITION_2=`${PL}/Q${CON}.KSH "SELECT ........" ${PL}/Q${CON}.KSH "CREATE ...." IF .... ....... I would like to extract only text that only... (4 Replies)
Discussion started by: mierdatuti
4 Replies

2. Shell Programming and Scripting

awk? extract quoted "" strings from multiple lines.

I am trying to extract multiple strings from snmp-mib files like below. ----- $ cat IF-MIB.mib <snip> linkDown NOTIFICATION-TYPE OBJECTS { ifIndex, ifAdminStatus, ifOperStatus } STATUS current DESCRIPTION "A linkDown trap signifies that the SNMP entity, acting in... (5 Replies)
Discussion started by: genzo
5 Replies

3. Shell Programming and Scripting

Extract text between two strings

Hi I have something like this: EXAMPLE 1 CREATE UNIQUE INDEX "STRING_1"."STRING_2" ON "BOSNI_CAB_EVENTO" ("CD_EVENTO" , "CD_EJECUCION" ) PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 5242880 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "DB1000_INDICES_512K"... (4 Replies)
Discussion started by: chrispaz81
4 Replies

4. Shell Programming and Scripting

How to Extract text between two strings?

Hi, I want to extract some text between two strings in a line i am using following command i.e; awk '/-string1/,/-string2/' filename contents of file is--- line1 line2 aaa -bbb -ccc -string1 c,d,e -string2 line4 but it is showing complete line which is having searched strings. aaa... (19 Replies)
Discussion started by: emresearch
19 Replies

5. Shell Programming and Scripting

Extract strings from multiple lines into one file -

input file Desired csv output gc_type, date/time, milli secs af, Mar 17 13:09:04 2011, 144.596 af, Mar 20 00:37:37 2011, 144.242 af, ar 20 21:30:59 2011, 108.518 Hi All, Any help in acheiving the above would be appreciated. I would like to parse through lines within one file and... (5 Replies)
Discussion started by: satish.vampire
5 Replies

6. UNIX for Advanced & Expert Users

bash/grep/awk/sed: How to extract every appearance of text between two specific strings

I have a text wich looks like this: clid=2 cid=6 client_database_id=35 client_nickname=Peter client_type=0|clid=3 cid=22 client_database_id=57 client_nickname=Paul client_type=0|clid=5 cid=22 client_database_id=7 client_nickname=Mary client_type=0|clid=6 cid=22 client_database_id=6... (3 Replies)
Discussion started by: Pioneer1976
3 Replies

7. UNIX for Dummies Questions & Answers

Using awk/sed to extract text between Strings

Dear Unix Gurus, I've got a data file with a few hundred lines (see truncated sample)... BEGIN_SCAN1 TASK_NAME=LA48 PDD Profiles PROGRAM=ArrayScan 1.00 21.220E+00 2.00 21.280E+00 END_DATA END_SCAN1 BEGIN_SCAN2 TASK_NAME=LA48 PDD Profiles 194.00 2.1870E+00 ... (5 Replies)
Discussion started by: tintin72
5 Replies

8. UNIX for Dummies Questions & Answers

extract text between two strings exclusive

Hi, I'm trying to retrieve text between two strings excluding the two strings. So for example I have the following input: type=friend username=phone1 secret=password host=dynamic dtmfmode=rfc2833 mailbox=9664 context=sip insecure=very canreinvite=no nat=yes realm=192.168.1.122... (3 Replies)
Discussion started by: the1armedcoder
3 Replies

9. Shell Programming and Scripting

using awk to extract text between two constant strings

Hi, I have a file from which i need to extract data between two constant strings. The data looks like this : Line 1 SUN> read db @cmpd unit 60 Line 2 Parameter: CMPD -> "C00071" Line 3 Line 4 SUN> generate Line 5 tabint>ERROR: (Variable data) The data i need to extract is... (11 Replies)
Discussion started by: mjoshi
11 Replies

10. Shell Programming and Scripting

using AWK how to extract text between two same strings

I have a file like: myfile.txt it is easy to learn awk and begin awk scripting and awk has got many features awk is a powerful text processing tool Now i want to get the text between first awk and immediate awk not the third awk . How to get it ? its urgent pls help me and file is unevenly... (2 Replies)
Discussion started by: santosh1234
2 Replies
Login or Register to Ask a Question