Sponsored Content
Top Forums Shell Programming and Scripting if match found go to a particular line in perl Post 302180370 by user_prady on Monday 31st of March 2008 12:58:20 AM
Old 03-31-2008
how to do it in perl

Quote:
Originally Posted by era
I don't think I have captured all your requirements correctly but at least it's a start. If I am allowed to guess, what would be easiest is collect the output in a variable and print it after you have seen *Main End and printed out the line which immediately preceded it.
I tried almost all way using next , redo and last command in perl , But I can't get these syntax correctly in perl. I know its easy ,but still I cant find the efficient way using while and next statement in perl.

I am trying to do the follwoing in perl

Code:
nawk ' {
        if($0 ~ /^\*Main Start/){
	     while( $0 !~ /^\*Main End/){
	     print $0;
             getline;
        }
     }
 } ' my.txt

One more code I want to do it in perl pls give me some basic idea how to do it without creating any temporary files ..

Code:
TMP=/tmp/my_tmp$$
nawk '
/\*Main End/{
split(x,arr,",")
print "Loop  " arr[1]
};
{x=$0
}
' my.txt > $TMP 

nawk '{
if($0 ~ /^Loop/){
    loop = $2 
    next;
  }
if($0 ~ /^\*Main Start/){
     printf "\nrepeat ("
     print  loop

} ' $TMP $my.txt


Thanks & Regards,
user_prady

Last edited by user_prady; 03-31-2008 at 02:48 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl: Match a line with multiple search patterns

Hi I'm not very good with the serach patterns and I'd need a sample how to find a line that has multiple patterns. Say I want to find a line that has "abd", "123" and "QWERTY" and there can be any characters or numbers between the serach patterns, I have a file that has thousands of lines and... (10 Replies)
Discussion started by: Juha
10 Replies

2. Shell Programming and Scripting

help with script to send email and if subject line match is found

Help with script that will check log, then find a match is found, add that as the subject line. 1. The script will always run as a deamon.. and scan the event.log file 2. when a new 101 line is added to the event.log file, have the script check position 5,6 and 7 which is the job name, which... (2 Replies)
Discussion started by: axdelg
2 Replies

3. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

4. UNIX for Dummies Questions & Answers

Display n lines after match found and other line

I have a file like this DoctorName Address1 Address2 DOB InsuredName Address1 Address2 DOB PatientName Address1 Address2 DOB ClaimNo1 DoctorName Address1 Address2 DOB InsuredName (2 Replies)
Discussion started by: nsuresh316
2 Replies

5. Shell Programming and Scripting

Displaying text till pattern match found in a line

Hi All, From the below line if we want to display all the text till found pattern dot/. I was trying with the below code but couldn't able to print text before the pattern. it display texts which is found after pattern. awk '/assed/{print;getline;print}' file_name | sed 's/^*. *//' input... (4 Replies)
Discussion started by: Optimus81
4 Replies

6. Shell Programming and Scripting

How to print the entire line if the mentioned match is found?

Hello Everyone, I have a file with 5 fields in each line just like mentioned below. Also the 4th field is time elapsed(hh:mm:ss) since the process is running xyz abc status 23:00:00 idle abc def status 24:00:00 idle def gji status 27:00:02 idle fgh gty status 00:00:00 idle Here I... (8 Replies)
Discussion started by: rahul2662
8 Replies

7. Shell Programming and Scripting

Perl removing line match with pattern in column

Hi, I have log like this: ... (1 Reply)
Discussion started by: justbow
1 Replies

8. Shell Programming and Scripting

Perl removing line match with pattern in column

Hi, I have log like this: ... (1 Reply)
Discussion started by: justbow
1 Replies

9. Shell Programming and Scripting

Add comment on last line if found match

Hi All, totally new on it , normally use it for just 1 line. i'm looking for help. i'm have 2 file. file 1 : -------------------------------------------------- c12 c1 c3 -------------------------------------------------- file 2: other content ... (10 Replies)
Discussion started by: kttan
10 Replies

10. UNIX for Beginners Questions & Answers

Modify text file if found multiple pattern match for every line.

Looking for help, i have input file like below and want to modify to expected output, if can without create additional file, hope can direct modify it. have 2 thing need do. 1st is adding a word (testplan generation off) after ! ! IPG: Tue Aug 07 14:31:17 2018 2nd is adding... (16 Replies)
Discussion started by: kttan
16 Replies
DBIx::Class::Manual::Example(3) 			User Contributed Perl Documentation			   DBIx::Class::Manual::Example(3)

NAME
DBIx::Class::Manual::Example - Simple CD database example DESCRIPTION
This tutorial will guide you through the process of setting up and testing a very basic CD database using SQLite, with DBIx::Class::Schema as the database frontend. The database consists of the following: table 'artist' with columns: artistid, name table 'cd' with columns: cdid, artist, title table 'track' with columns: trackid, cd, title And these rules exists: one artist can have many cds one cd belongs to one artist one cd can have many tracks one track belongs to one cd Installation Install DBIx::Class via CPAN should be sufficient. Create the database/tables First make and change the directory: mkdir app cd app mkdir db cd db This example uses SQLite which is a dependency of DBIx::Class, so you shouldn't have to install extra software. Save the following into a example.sql in the directory db CREATE TABLE artist ( artistid INTEGER PRIMARY KEY, name TEXT NOT NULL ); CREATE TABLE cd ( cdid INTEGER PRIMARY KEY, artist INTEGER NOT NULL REFERENCES artist(artistid), title TEXT NOT NULL ); CREATE TABLE track ( trackid INTEGER PRIMARY KEY, cd INTEGER NOT NULL REFERENCES cd(cdid), title TEXT NOT NULL ); and create the SQLite database file: sqlite3 example.db < example.sql Set up DBIx::Class::Schema Change directory back from db to the directory app: cd ../ Now create some more directories: mkdir MyDatabase mkdir MyDatabase/Main mkdir MyDatabase/Main/Result mkdir MyDatabase/Main/ResultSet Then, create the following DBIx::Class::Schema classes: MyDatabase/Main.pm: package MyDatabase::Main; use base qw/DBIx::Class::Schema/; __PACKAGE__->load_namespaces; 1; MyDatabase/Main/Result/Artist.pm: package MyDatabase::Main::Result::Artist; use base qw/DBIx::Class::Core/; __PACKAGE__->table('artist'); __PACKAGE__->add_columns(qw/ artistid name /); __PACKAGE__->set_primary_key('artistid'); __PACKAGE__->has_many('cds' => 'MyDatabase::Main::Result::Cd'); 1; MyDatabase/Main/Result/Cd.pm: package MyDatabase::Main::Result::Cd; use base qw/DBIx::Class::Core/; __PACKAGE__->load_components(qw/InflateColumn::DateTime/); __PACKAGE__->table('cd'); __PACKAGE__->add_columns(qw/ cdid artist title/); __PACKAGE__->set_primary_key('cdid'); __PACKAGE__->belongs_to('artist' => 'MyDatabase::Main::Result::Artist'); __PACKAGE__->has_many('tracks' => 'MyDatabase::Main::Result::Track'); 1; MyDatabase/Main/Result/Track.pm: package MyDatabase::Main::Result::Track; use base qw/DBIx::Class::Core/; __PACKAGE__->table('track'); __PACKAGE__->add_columns(qw/ trackid cd title /); __PACKAGE__->set_primary_key('trackid'); __PACKAGE__->belongs_to('cd' => 'MyDatabase::Main::Result::Cd'); 1; Write a script to insert some records insertdb.pl #!/usr/bin/perl use strict; use warnings; use MyDatabase::Main; my $schema = MyDatabase::Main->connect('dbi:SQLite:db/example.db'); # here's some of the SQL that is going to be generated by the schema # INSERT INTO artist VALUES (NULL,'Michael Jackson'); # INSERT INTO artist VALUES (NULL,'Eminem'); my @artists = (['Michael Jackson'], ['Eminem']); $schema->populate('Artist', [ [qw/name/], @artists, ]); my %albums = ( 'Thriller' => 'Michael Jackson', 'Bad' => 'Michael Jackson', 'The Marshall Mathers LP' => 'Eminem', ); my @cds; foreach my $lp (keys %albums) { my $artist = $schema->resultset('Artist')->find({ name => $albums{$lp} }); push @cds, [$lp, $artist->id]; } $schema->populate('Cd', [ [qw/title artist/], @cds, ]); my %tracks = ( 'Beat It' => 'Thriller', 'Billie Jean' => 'Thriller', 'Dirty Diana' => 'Bad', 'Smooth Criminal' => 'Bad', 'Leave Me Alone' => 'Bad', 'Stan' => 'The Marshall Mathers LP', 'The Way I Am' => 'The Marshall Mathers LP', ); my @tracks; foreach my $track (keys %tracks) { my $cdname = $schema->resultset('Cd')->find({ title => $tracks{$track}, }); push @tracks, [$cdname->id, $track]; } $schema->populate('Track',[ [qw/cd title/], @tracks, ]); Create and run the test scripts testdb.pl: #!/usr/bin/perl use strict; use warnings; use MyDatabase::Main; my $schema = MyDatabase::Main->connect('dbi:SQLite:db/example.db'); # for other DSNs, e.g. MySQL, see the perldoc for the relevant dbd # driver, e.g perldoc L<DBD::mysql>. get_tracks_by_cd('Bad'); get_tracks_by_artist('Michael Jackson'); get_cd_by_track('Stan'); get_cds_by_artist('Michael Jackson'); get_artist_by_track('Dirty Diana'); get_artist_by_cd('The Marshall Mathers LP'); sub get_tracks_by_cd { my $cdtitle = shift; print "get_tracks_by_cd($cdtitle): "; my $rs = $schema->resultset('Track')->search( { 'cd.title' => $cdtitle }, { join => [qw/ cd /], } ); while (my $track = $rs->next) { print $track->title . " "; } print " "; } sub get_tracks_by_artist { my $artistname = shift; print "get_tracks_by_artist($artistname): "; my $rs = $schema->resultset('Track')->search( { 'artist.name' => $artistname }, { join => { 'cd' => 'artist' }, } ); while (my $track = $rs->next) { print $track->title . " "; } print " "; } sub get_cd_by_track { my $tracktitle = shift; print "get_cd_by_track($tracktitle): "; my $rs = $schema->resultset('Cd')->search( { 'tracks.title' => $tracktitle }, { join => [qw/ tracks /], } ); my $cd = $rs->first; print $cd->title . " "; } sub get_cds_by_artist { my $artistname = shift; print "get_cds_by_artist($artistname): "; my $rs = $schema->resultset('Cd')->search( { 'artist.name' => $artistname }, { join => [qw/ artist /], } ); while (my $cd = $rs->next) { print $cd->title . " "; } print " "; } sub get_artist_by_track { my $tracktitle = shift; print "get_artist_by_track($tracktitle): "; my $rs = $schema->resultset('Artist')->search( { 'tracks.title' => $tracktitle }, { join => { 'cds' => 'tracks' } } ); my $artist = $rs->first; print $artist->name . " "; } sub get_artist_by_cd { my $cdtitle = shift; print "get_artist_by_cd($cdtitle): "; my $rs = $schema->resultset('Artist')->search( { 'cds.title' => $cdtitle }, { join => [qw/ cds /], } ); my $artist = $rs->first; print $artist->name . " "; } It should output: get_tracks_by_cd(Bad): Dirty Diana Smooth Criminal Leave Me Alone get_tracks_by_artist(Michael Jackson): Beat it Billie Jean Dirty Diana Smooth Criminal Leave Me Alone get_cd_by_track(Stan): The Marshall Mathers LP get_cds_by_artist(Michael Jackson): Thriller Bad get_artist_by_track(Dirty Diana): Michael Jackson get_artist_by_cd(The Marshall Mathers LP): Eminem Notes A reference implementation of the database and scripts in this example are available in the main distribution for DBIx::Class under the directory t/examples/Schema. With these scripts we're relying on @INC looking in the current working directory. You may want to add the MyDatabase namespaces to @INC in a different way when it comes to deployment. The testdb.pl script is an excellent start for testing your database model. This example uses "load_namespaces" in DBIx::Class::Schema to load in the appropriate Row classes from the MyDatabase::Main::Result namespace, and any required resultset classes from the MyDatabase::Main::ResultSet namespace (although we created the directory in the directions above we did not add, or need to add, any resultset classes). TODO
AUTHOR
sc_ from irc.perl.org#dbix-class Kieren Diment <kd@totaldatasolution.com> Nigel Metheringham <nigelm@cpan.org> perl v5.16.2 2012-08-16 DBIx::Class::Manual::Example(3)
All times are GMT -4. The time now is 07:12 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy