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
select a record from one file matching from second file using awk synmag Shell Programming and Scripting 7 06-12-2008 03:37 AM
select a portion of a file into a CSV anju Shell Programming and Scripting 8 02-28-2008 01:50 AM
select last field from a file kykyboss Shell Programming and Scripting 3 11-14-2006 10:15 AM
select distinct row from a file merry susana UNIX for Dummies Questions & Answers 7 05-03-2005 07:54 AM
extract block in file sskb UNIX for Dummies Questions & Answers 5 10-25-2001 11:29 AM

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 10-11-2006
misenkiser misenkiser is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 36
Select last block from a file

Hi,

I have to always select last portion of a block identified by 2 fixed keywords
which repeats in a file for eg

START
....
...
END


START
....
...
END

START
....
...
END

THE LAST PORTION WITHIN (START AND END) SHOULD ALWAYS BE SELECTED
  #2 (permalink)  
Old 10-11-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,538
python alternative:
INput:
START
1st
END


START second block
second
END

START third block
last
END


Code:
store =[]
data = open("input").readlines() #each file into array
for i in data[::-1]: #starting from last entry, work backwards
        i = i.strip() #strip leading/trailing spaces
 	if i.startswith("END"):continue
 	elif i.startswith("START"):break
 	else: store.append(i) 
print ''.join(store[::-1]) #reverse result back..
output:
Code:
last

Last edited by ghostdog74; 10-11-2006 at 10:06 AM..
  #3 (permalink)  
Old 10-11-2006
shreedhar shreedhar is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 2
#! /usr/bin/perl

$file='last_block'; #file name
open (FH, $file);

while ($line=<FH>) {
undef(@array);
chomp($line);
if($line =~ /^START/) {
while ($temp = <FH>) {
chomp($temp);
if ($temp =~ /^END/) {
last;
}
push(@array, $temp);
}
@copy=@array;
}
}
print "@copy\n";

input file (last_block):
START
i am in first block
Now tell me i am...
END


START
i am in second block
now tell me i am
...
END

START
I am last block
did u asked me
END

output:
I am last block did u asked me
  #4 (permalink)  
Old 10-11-2006
yashavant.a yashavant.a is offline
Registered User
  
 

Join Date: Oct 2006
Location: Bangalore
Posts: 16
#!/bin/bash
file=input
line_no=1

total=`cat $file | wc -l`
state=0

while [ $line_no -le $total ]
do
line=`tail -$line_no $file | head -1`

if [ "$line" = "END" ]
then
state=1
elif [ "$line" = "START" ]
then
break
elif [ $state -eq 1 ]
then
output=$line"\n"$output
fi

line_no=`expr $line_no + 1`
done

echo -e $output
  #5 (permalink)  
Old 10-11-2006
mahendramahendr mahendramahendr is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2005
Location: London
Posts: 222
$ more test.pl
#!/usr/bin/perl -w

$file="test.txt";

open(FH,$file) || die "Unable to open $file : $!\n";

my $line;
my $strt_str=0;
my @arr=();

while ( $line = <FH> )
{
chomp $line;

if( $line =~ /^START/ ) {
@arr = ();
$strt_str = 1;
}

push(@arr,$line) if( $strt_str == 1 );

$strt_str = 0 if ( $line =~ /^END/ );
}

print join("\n",@arr)."\n";

$ more test.txt
START
i am in first block
Now tell me i am...
END


START
i am in second block
now tell me i am
...
END

START
I am last block
did u asked me
END

$ perl test.pl
START
I am last block
did u asked me
END
  #6 (permalink)  
Old 10-11-2006
misenkiser misenkiser is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 36
the keywords START and END
are not the only words in the line so the code fails

START .........................
.....
.....
.....
......
END ............................

I only know that the keywords wil be START and END but there can be other chars in the line
  #7 (permalink)  
Old 10-11-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,423
Another way using awk:
Code:
awk '
     /^START/ { inside = 1 ; count = 0; next}
     /^END/   { inside = 0 ;next}
     inside  { line[count++] = $0 }
     END     { for (i=1; i<=count; i++) print line[i] }
    ' input_file

Jean-Pierre
Closed Thread

Bookmarks

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 04:44 PM.


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