Using awk to extract text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk to extract text
# 1  
Old 03-09-2005
Using awk to extract text

I am currently on a project where I must extract all the data between two words. I am currently running a perl script and trying to use awk to extract specific lines of text between two words: ./myscript.pl | awk ' "

An example of the output of the script:

===================
WORKING LOG
===================
<-----------------------
------------------------
------------------------
------text here---------
------------------------
------------------------
----------------------->
===================

I'm trying to use the awk command in the UNIX shell to extract everything between WORKING LOG and the last row of "=" signs. Is there a way to specficy with regexs or in awk syntax to take all the text between two specified points?
# 2  
Old 03-10-2005
Using awk to print lines between "WORKING LOG" and the second line of equals signs...
Code:
  /^WORKING LOG/ {flg = 2}
  flg
  flg && /^========/ {flg--}

Same code translated to perl...
Code:
while (<>) {
    if (/^WORKING LOG/) {
        $flg = 2;
    }
    print $_ if $flg;
    if ($flg && /^========/) {
        $flg--;
    }
}

# 3  
Old 03-10-2005
Try with awk as,
awk '{ if ($0 ~ "WORKING LOG") { getline;do {print $0;getline;}while ($0 ~ "---"); }}END { print $0 }'

hth.
# 4  
Old 03-10-2005
----------

Last edited by tanku; 03-13-2005 at 07:40 PM..
# 5  
Old 08-14-2008
Expanded and corrected solution

muthukumar's solution almost worked for me and got me on the right track.

I expanded it to multiple lines (I prefer easy-to-read over compact) and got this to work:

{
if ($0 ~ "text-start")
{
getline
while (! ($0 ~ "text-end"))
{
if ($0 ~ "text-find")
{
print FNR, ":", $0
}
getline
}
}
}


if you put that script in a file called awk-script, then you would invoke it to search a file called text2search as follows:

awk -f awk-script text2search

it will print out the line #(s) of where it found the search text in between the two tags/marker text.
# 6  
Old 08-15-2008
perl:

Code:
$begin=shift;
$end=shift;
open(FH,"<a");
while(<FH>){
	if(m/$begin/){
		$s=$.;
	}
	if(m/$end/){
		$e=$.;
	}
	$arr[$.]=$_;
}
close(FH);
for($i=$s;$i<=$e;$i++){
	print $arr[$i];
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to extract digit in line of text and create link

I am trying to extract the number in bold (leading zero removed) after Medexome_xx_numbertoextractin file and create an output using that extracted number. In the output the on thing that will change is the number the other test is static and will be the same each time. Thank you :). file ... (8 Replies)
Discussion started by: cmccabe
8 Replies

2. Shell Programming and Scripting

Extract text from html using perl or awk

I am trying to extract text after keywords fron an html file. The keywords are reportLink":, "barcodedSamples": {", "barcodedSamples": {". Both the perl and awk run but the output is just the entire index.html not the desired output. Also for the reportLink": only the text after the second / until... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

awk used to extract data between text

Hello all, I have a file (filename.txt) with some data (in two columns X and Y) which looks like this: ########## 'Header1' 'Sub-header1' X Y xxxx.xx yyyy.yyy xxxx.xx yyyy.yyy .... ... 'Sub-header2' X Y xxxx.xx ... (7 Replies)
Discussion started by: jaldo0805
7 Replies

4. Shell Programming and Scripting

Extract word from text (sed,awk, etc...)

Hello, I need some help extracting the number after the RBA e.g 15911688 from the below block of text (e.g: grep RBA |sed .......). The code should be valid for blocks if text generated at different times as well and not for the below text only. ... (2 Replies)
Discussion started by: drbiloukos
2 Replies

5. Shell Programming and Scripting

how to extract a paticular string from the text file with awk.

hello forum members I have txt file which consists the following information. Server: abababa.xyz.ap.mxmx.com Address: 111.143.211.202 Name: rmxd.ipc.ap.mxmx.com Address: 144.111.99.9 from the abovefile i have to extract only string "rmxd.ipc.ap.mxmx.com" through awk command.... (1 Reply)
Discussion started by: rajkumar_g
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

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

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

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