Select last block from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Select last block from a file
# 1  
Old 10-11-2006
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  
Old 10-11-2006
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  
Old 10-11-2006
#! /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  
Old 10-11-2006
#!/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  
Old 10-11-2006
$ 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  
Old 10-11-2006
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  
Old 10-11-2006
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Select table name from file

i have a file tabel.out which contain table name in this format. xyz abc qwe Plz help me writing one script which actually takes tablename from file table.out file one by one and then select count (*) from $tablename limit 10; and print error in output file if any tabel is not... (4 Replies)
Discussion started by: netdbaind
4 Replies

2. Shell Programming and Scripting

Short program to select lines from a file based on a second file

Hello, I use UBUNTU 12.04. I want to write a short program using awk to select some lines in a file based on a second file. My first file has this format with about 400,000 lines and 47 fields: SNP1 1 12.1 SNP2 1 13.2 SNP3 1 45.2 SNP4 1 23.4 My second file has this format: SNP2 SNP3... (1 Reply)
Discussion started by: Homa
1 Replies

3. Shell Programming and Scripting

Select data from a file

Hi , I want to select data from a file .the datas in a file will be in both upper and lower case. can able to select particular data using awk. file is like Ram Selva 24332 UNIXTEAM Karthi Siva 43322 UNIXTEAM In read if i enter selva as lowercase its not displaying result. (9 Replies)
Discussion started by: boopal
9 Replies

4. Shell Programming and Scripting

Block of records to select from a file

Hello: I am new to shell script programming. Now I would like to select specific records block from a file. For example, current file "xyz.txt" is containing 1million records and want to select the block of records from line number 50000 to 100000 and save into a file. Can anyone suggest me how... (3 Replies)
Discussion started by: nvkuriseti
3 Replies

5. Shell Programming and Scripting

Select older file if multiple file exists with same name

Hi I want to select older file if multiple files exist with same name. eg:There are five files in UNIX folder Sample1_20091231 Sample1_20100110 Sample1_20100115 Sample2_20100115 Sample3_20100115 However in file list i.e FileName.DAT I have data like Sample1 Sample2 Sample3 I am... (5 Replies)
Discussion started by: dashing201
5 Replies

6. UNIX for Dummies Questions & Answers

select first column of file

Hi, Following is my file output 247 Sleep 25439 NULL 259 Sleep 25460 NULL 277 Sleep 15274 NULL 361 Sleep 2 NULL 362 Sleep 202 NULL I want to select only first column to other file How can... (2 Replies)
Discussion started by: kaushik02018
2 Replies

7. Shell Programming and Scripting

Select some lines from a txt file and create a new file with awk

Hi there, I have a text file with several colums separated by "|;#" I need to search the file extracting all columns starting with the value of "1" or "2" saving in a separate file just the first 7 columns of each row maching the criteria, with replacement of the saparators in the nearly created... (4 Replies)
Discussion started by: capnino
4 Replies

8. Shell Programming and Scripting

Select a pattern from file

Hi, I have a requirement to select only a specific pattern from a flat file and delete its occurance. For eg. If my file contains : <A1>1234</A1> <A2>5678</A2> <ABC>1234</ABC> <A3>0987</A3> Then, i want to delete <ABC>1234</ABC> from the file and have the contents as :-... (7 Replies)
Discussion started by: DTechBuddy
7 Replies

9. Shell Programming and Scripting

Select block of text around matching braces

Hi, I have several block of text that I need to select, however this text may be spread over several lines and contains the '{' and '}' within it. For e.g., ABC=100{ DEF = 200 { GHI, JKL } } #2nd Block 123 { 456{78,910}} }I am trying to figure out how to remove... (2 Replies)
Discussion started by: BootComp
2 Replies

10. Shell Programming and Scripting

select a record from one file matching from second file using awk

I need help :) I have two input files and I'd like to generate a report based on the two. filea: hostname,account1,password ,account2,password hostname,account1,password hostname,account1,password ,account1,password ,account2,password repeating hostnames are blank fileb: hosta... (7 Replies)
Discussion started by: synmag
7 Replies
Login or Register to Ask a Question