Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to print between to matching patterns 1

Status
Not open for further replies.

komark

Technical User
Sep 12, 2005
134
0
0
US
Hi,

Suppose I have the following lines in a file:

TOM {
1
2
3
4
5
}
David {
6
7
8
9
10
}


I want to search for TOM and print out all the lines till I get the curly brace. So my output will be:
Tom {
1
2
3
4
5
}

I can do an awk but when I put that in a perl script I get errors:
> awk '/Tom/,/\}|\^$/'

Any help will be appreciated.

Thanks,
Omar.
 
If you know how to do something in awk, but not perl, you can always use the a2p tool to either convert your script, or at least give you some clues.

When I tried it gave me:

Code:
#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
    if $running_under_some_shell;
                        # this emulates #! processing on NIH machines.
                        # (remove #! line above if indigestible)

eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
                        # process any FOO=bar switches

while (<>) {
    print $_ if /Tom/ .. /\}|\^$/;
}

Annihilannic.
 
Thanks Annihilannic.

I have also figured it out and seems quite simple:

while (<flist>) {

if ( /Tom/../\}/ ) {
print;
}

Thanks,
Omar.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top