using awk to extract text between two constant strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using awk to extract text between two constant strings
# 8  
Old 02-05-2008
Thanks to all who replied, however only Radoulov's script worked.
Radoulov, can you please help me understand your script ?
# 9  
Old 02-05-2008
Quote:
Originally Posted by mjoshi
Radoulov, can you please help me understand your script ?
Yes.

Code:
'/SUN> read db @cmpd unit 60/ {c = 3; getline; s = $0 }

If the current record matches the pattern "SUN> read db @cmpd unit 60",
set the parameter c to 3, read the next record and save it in the parameter s.

Code:
!c-- && /tabint>ERROR:/ { print s }

If the value of the parameter c is not true (is zero) and the current record matches the pattern "tabint>ERROR:",print s.

If the expression !c-- is not clear,
consider this:

Code:
% cat file
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)

% awk '/SUN> read db @cmpd unit 60/ { 
print "c is:", c, "$0 is:", $0; c = 3; getline; s = $0 }
{ print "c is:", c, "$0 is:", $0; c-- }' file
c is:  $0 is: Line 1 SUN> read db @cmpd unit 60
c is: 3 $0 is: Line 2 Parameter: CMPD -> "C00071"
c is: 2 $0 is: Line 3
c is: 1 $0 is: Line 4 SUN> generate
c is: 0 $0 is: Line 5 tabint>ERROR: (Variable data)



In this case:

Code:
!c-- && /tabint>ERROR:/

The first expression (left side of the logical AND) is evaluated for every record (so c is decremented).
# 10  
Old 02-05-2008
Quote:
Originally Posted by mjoshi
Thanks to all who replied, however only Radoulov's script worked.
shamrock's solution works well on my box and my solution also works fine.
Maybe a difference awk version.

Regards
# 11  
Old 03-30-2009
I have a variant of the same question. I have been searching across number of threads, but it has not been covered, apparently.

Any input and explanation of the same would be helpful.
Sample Input
-------------
Record 1
Field 1: Data for field 1
Field 2: Data for field 2
Field 3: Data for field 3
Field 4: Data for field 4

Record 2
Field 1: Data for field 1
Field 2: Data for field 2
Field 4: Data for field 4

Record 3
Field 1: Data for field 1
Field 3: Data for field 3
Field 4: Data for field 4

---------------------------
Expected Output (The new inserts are in CAPS)
---------------------------
Record 1
Field 1: Data for field 1
Field 2: Data for field 2
Field 3: Data for field 3
Field 4: Data for field 4

Record 2
Field 1: Data for field 1
Field 2: Data for field 2
FIELD 3: <NULL>
Field 4: Data for field 4

Record 3
Field 1: Data for field 1
FIELD 2: <NULL>
Field 3: Data for field 3
Field 4: Data for field 4


Logic:
If (^Field) && ( (next occurence of ^Field) <> condition)
insert line as above
endif
endif
# 12  
Old 03-31-2009
Is the maximum number of fields per record (4 in your example) fixed?
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

Extracting text between two constant strings

Hi All, I have a file whose common patter is like this: .I 1 .U 87049087 .S Some text here too .M This is a text .T Some another text here .P Name of the book .W Some lines of more text. This text needs to be extracted. .A more text goes here too .I 2 (2 Replies)
Discussion started by: shoaibjameel123
2 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 text between two specified "constant" texts using awk

Hi All, From the title you may know that this question has been asked several times and I have done lot of Googling on this. I have a Wikipedia dump file in XML format. All the contents are in one XML file i.e. all different topics have been put in one XML file. Now I need to separate them and... (1 Reply)
Discussion started by: shoaibjameel123
1 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. Shell Programming and Scripting

choose random text between constant string.. using awk?

Hallo I have maybe a little bit advanced request.... I need to choose one random part betwen %.... so i have this.. % text1 text1 text1 text1 text1 text1 text1 text1 text1 % text2 text2 text2 text2 text2 % text3 text3 text3 tetx3 % this choose text between % awk ' /%/... (8 Replies)
Discussion started by: sandwich
8 Replies

8. Shell Programming and Scripting

AWK: How to extract text lines between two strings

Hi. I have a text test1.txt file like:Receipt Line1 Line2 Line3 End Receipt Line4 Line5 Line6 Canceled Receipt Line7 Line8 Line9 End (9 Replies)
Discussion started by: TQ3
9 Replies

9. 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

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