Results 1 to 2 of 2
hi all,
i have a text file which goes like this..
text1..
text2..
text3...
text4..
session1
text 5..
.
text1..
text2..
session2
text5...
.
text1
text2
text3
session1
text4..
-------------------
...
- 01-17-2012 #1Just Joined!
- Join Date
- Dec 2009
- Posts
- 6
filtering text in this manner, is it possible ?
hi all,
i have a text file which goes like this..
text1..
text2..
text3...
text4..
session1
text 5..
.
text1..
text2..
session2
text5...
.
text1
text2
text3
session1
text4..
-------------------
1) each paragraph is seperated by a . follow by a linefeed
2) each paragraph of text belong to a session
3) a session might span 2 different paragraph (e.g. session 1)
4) the amount of line(text1,2,3) etc is variable for each paragraph (sometime 10 lines, sometime 4lines)
================
how do i grep only paragraph belonging to a particular session ?
e.g. (you can see paragraph belonging to session 2 is omitted)
text1..
text2..
text3...
text4..
session1
text 5..
.
text1
text2
text3
session1
text4..
.
========
Regards,
Noob
- 01-18-2012 #2Linux Guru
- Join Date
- May 2011
- Posts
- 1,838
You could certainly do this with awk and grep, but here's a perl script that I think will do it:
Create a file with this code in it (call it "read-file.pl" or whatever), make it executable ("chmod +x ./read-file.pl") and pass your text file as an argument to it:Code:#!/usr/bin/perl use strict; use warnings; my $file = shift || die "Give me a file\n"; my $cnt = 0; my %hash = (); open(FH,'<',$file) or die "can't read '$file': $!\n"; while(<FH>){ chomp; if(/./ .. /^\.$/){ push(@{$hash{$cnt}},$_) unless(/^\.$/); }else{ $cnt+=1; } } close(FH); my %sessions = (); for my $par(sort { $a <=> $b } keys %hash){ my $session; my @array = (); for(@{$hash{$par}}){ if(/^session([0-9]*)$/){ $session = $1; }else{ push(@array,$_); } } die "No session defined for paragraph $par\n" unless(defined($session)); for(@array){ push(@{$sessions{$session}},$_); } } for my $session(sort { $a <=> $b } keys %sessions){ print "Session $session\n"; my @lines = @{$sessions{$session}}; for(@lines){ print " ",$_,"\n"; } }
Code:./read-file.pl text.txt


Reply With Quote