![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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..
Code:
last Last edited by ghostdog74; 10-11-2006 at 10:06 AM.. |
|
||||
|
#! /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 |
|
||||
|
#!/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 |
|
||||
|
$ 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 |
|
||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|