The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
using awk to extract text between two constant strings mjoshi Shell Programming and Scripting 11 03-31-2009 09:37 AM
c program to extract text between two delimiters from some text file kukretiabhi13 High Level Programming 7 12-03-2008 06:29 PM
extract text b/w two delimiters dowsed4u8 UNIX for Advanced & Expert Users 6 01-18-2008 05:48 PM
How to extract text from xml file chrisf Shell Programming and Scripting 3 09-01-2007 02:25 PM
How to extract data from a text file negixx Shell Programming and Scripting 1 07-19-2005 09:30 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-09-2005
Davizzle Davizzle is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 1
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 (permalink)  
Old 03-10-2005
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
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 (permalink)  
Old 03-10-2005
muthukumar muthukumar is offline
Registered User
  
 

Join Date: Feb 2005
Location: Coimbatore, Tamilnadu, India
Posts: 119
Try with awk as,
awk '{ if ($0 ~ "WORKING LOG") { getline;do {print $0;getline;}while ($0 ~ "---"); }}END { print $0 }'

hth.
  #4 (permalink)  
Old 03-10-2005
tanku tanku is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 53
----------

Last edited by tanku; 03-13-2005 at 07:40 PM..
  #5 (permalink)  
Old 08-14-2008
claudioc claudioc is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 1
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 (permalink)  
Old 08-15-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,079
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];
}
Closed Thread

Bookmarks

Tags
perl, perl shift, shift, shift perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0