Unix grep in perl script


 
Thread Tools Search this Thread
Top Forums Programming Unix grep in perl script
# 1  
Old 05-25-2012
Unix grep in perl script

Hello,

Fairly simple really I have an xml file and I want to check to see if it contains a pattern. The pattern is "../"

On the command line I can type: grep "\.\./" myFile.xml
and I get desired result.

To do the same thing in a perl script I thought it was as simple as putting the `` around the command but it won't work no matter what I do.

Code:
$check = `grep "\.\./" $myFile`;

I've tried different variations of the command such as escaping the / but still no joy. No matter what it seems to be returning every line from the file that contains a . OR a /

Anyone have any insight into this?
# 2  
Old 05-25-2012
Perl has a built-in grep function.

i.e.
Code:
#!/usr/bin/perl

use strict;
use diagnostics;

my $file = "/tmp/file";

open(FILE, "<$file") or die "Unable to open: $file\n";
while(<FILE>) 
    {if(grep(/\.\.\//, $_)) {
        print;
    }
}
close(FILE);
exit(0);

grep - perldoc.perl.org

Hope this helps.
# 3  
Old 05-28-2012
@in2nix4life

Delicious thanks very much. I knew about perl's grep function but never perceived $_ as a list! duh... Smilie
# 4  
Old 05-28-2012
Quote:
Originally Posted by in2nix4life
Code:
while(<FILE>) 
    {if(grep(/\.\.\//, $_)) {
        print;
    }
}
close(FILE);

Use pattern match m// instead of grep.
Code:
while (<FILE>) {
    if (m/\.\.\//) { print }
}

# 5  
Old 05-28-2012
Quote:
Originally Posted by balajesuri
Use pattern match m// instead of grep.
Code:
while (<FILE>) {
    if (m/\.\.\//) { print }
}

Any particular reason or is it just better programming?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep the exact process in perl script

I am working on one script where I need to grep the exact process in perl script. for e.g. when I run simple grep command on the linux machine it gives me two process mGateway_mvc_q01 and mGateway_mvc_q01_v7 which is not the correct result.I tried to use ( ps -eAf | grep ^mGateway_mvc_q01$) but... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

2. Shell Programming and Scripting

Need a perl script similiar to grep -r 'word' /path/to/dir"

Hi , i am looking for a perl script to grep for a string in all files inside a directory . bash command . grep -r 'word' /path/to/dir Thanks, Nvil (3 Replies)
Discussion started by: nevil
3 Replies

3. Shell Programming and Scripting

shell or perl script using grep and regex

Hi, I have file stored in a directory containing information about subnet mask and next hop address in the following format 10.1.1.0/16, 255.255.0.0, 10.1.1.1 10.1.2.0/16, 255.255.0.0,10.1.2.1 here 10.1.1.0/16 represent range of ip address 10.1.1.1-10.1.1.16 given say an IP address... (1 Reply)
Discussion started by: termeric
1 Replies

4. Shell Programming and Scripting

[Solved] perl and grep: get a script to look at more then one file

hi guys i have this very messy script, that looks in /var/log/messages.all for an error and reports if it finds the key works how can i get it to look at more then one file, i.e /var/log/message.all * so it looks in old logs as well thanks exit 0 if (isRenderNode(hostname)); my... (4 Replies)
Discussion started by: ab52
4 Replies

5. Shell Programming and Scripting

perl/unix: script in command line works but not in perl

so in unix this command works works and shows me a list of directories find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt but when i try running a perl script to run this command my $query = 'find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt';... (2 Replies)
Discussion started by: kpddong
2 Replies

6. Shell Programming and Scripting

Need to grep 2 words from Perl Script results in Terminal....

Hey guys. I have a .pl script that scans my hosts to see if they are down or up. I can run it anytime I want. The script uses a conf file that contains text lines of the IP addresses of the servers. I run the script from the command line of my terminal (MAC OS) I run: sudo ./scanner.pl brings... (3 Replies)
Discussion started by: yoyoyo777
3 Replies

7. Shell Programming and Scripting

ftp from windows to unix using a perl script on unix machine

i need to ftp a file from windows to a unix machine by executing a sript(perl/shell/php) from that unix machine.i can also use HTML and javascript to build forms. (3 Replies)
Discussion started by: raksha.s
3 Replies

8. Shell Programming and Scripting

[Help] PERL Script - grep multiple lines

Hi Gurus, I need some help with the "grep" command or whatever command that you think suitable for me. I'm about to write a perl script to extract a report from the system and submit it to the end users. The input for the script will consist of 3 element. 1) Generation ID 2) Month 3) Year... (6 Replies)
Discussion started by: miskin
6 Replies

9. UNIX for Dummies Questions & Answers

Using Grep within a UNIX Script

Hi, I'm trying to save the wc from a grep command in my unix script and then later in my script I try to reference the variable but I have no luck. Any help is greatly appreciated! -------------------- set xTest = 'grep -i lisa daily.log' if ; then echo "TEST" >> daily.log fi... (6 Replies)
Discussion started by: lisa_0801
6 Replies

10. Shell Programming and Scripting

grep ^M in file using perl script....

hi i am using perl on windows ( active state perl 5.8 ) and i want to check for Control-M (^M) in file. files to be checked are in unix format so new line character is (\n). This perl script is called from Batch file ( windows .BAT file ) my script is while (<PROGRAM>) { ... (12 Replies)
Discussion started by: zedex
12 Replies
Login or Register to Ask a Question