Grep and map in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep and map in perl
# 1  
Old 11-21-2010
Grep and map in perl

Hi guys,

I'm trying to learn grep and map and having a little problem.

Let's say I have a file which contains:
Apple: abcdcabdadddbac

I want to replace any combinations of three of abcd, thus when I do this:

Code:
print grep {s/[abcd]{3}/X/g} <F>; # will do the subtitution fine, output XXXX

but what if I want to replace [abcd]{3} by a variable $string and enter it on the command line such this:

Code:
grep {s/$string/X/g} <F>;
and when I run: perl test.pl file "bda" # this will not subtitute any combinations of bda, but this exact order only

Can someone tell me what I did wrong here?

Thanks alot,

new bie,
# 2  
Old 11-22-2010
Assuming your script is something like this
Code:
open(F, $ARGV[0]);
print  grep {s/$ARGV[1]/X/g} <F>;

and your command line is
Code:
perl test.pl file "bda"

then you got the expected output as there is exactly only one match for that search term i.e.
Code:
print  grep {s/bda/X/g} <F>;

if you want to match your first regular expression you need to enter
Code:
perl test.pl file "[abcd]{3}"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Understanding an example of perl map() function

Hello, I have many folders under which there is always a file with the same name, which contains the data I need to process later. A perl oneliner was borrowed perl -e 'print "gene_id\t", join("\t", map {/(.*)\//; $1} @ARGV),"\n";' *_test.trim/level.csvto make a header so that each column... (5 Replies)
Discussion started by: yifangt
5 Replies

2. Shell Programming and Scripting

Perl combine multiple map statements

I have a file like file. file.TODAY.THISYEAR file.TODAY.LASTYEARI want to substitute the words in caps with their actual values so that output should look like file.140805 file.140805.2014 file.140805.2013For this I am reading the file line bye line in an array and using multiple map... (1 Reply)
Discussion started by: sam05121988
1 Replies

3. Shell Programming and Scripting

Looping in Perl based on defined keys in Map

Hello All, I am writing the below script where it will connect to database and returns the results. #!/sw/gcm/perl510/bin/perl use SybaseC; &openConnection; &loadvalues; sub openConnection { $dbproc = new SybaseC(SYDB}, $ENV{DBDFLTUSR}, $ENV{DBDFLTPWD}); if... (2 Replies)
Discussion started by: filter
2 Replies

4. Shell Programming and Scripting

Grep in PERL

Hi, Can anybody let me know how this grep will work. The input and output is not known. Also can you give me the details of any link where i can find clearly about grep Thanks in advance (1 Reply)
Discussion started by: irudayaraj
1 Replies

5. Shell Programming and Scripting

Perl + and Grep

Hi All i have this script that uses glob to look in /var/log/messages.* my @messagefiles = glob "/var/log/messages.*"; and the code that uses it is this grep { /NVRM: Xid/ } @messages) but this spits out this /var/log/messages-20111030:Oct 25 13:43:04 brent kernel: NVRM:... (10 Replies)
Discussion started by: ab52
10 Replies

6. Shell Programming and Scripting

How to map the values of an array in perl?

Hi, I have 2 arrays: @names=qw(amith veena chaitra); @files=qw(file.txt file1.txt file3.txt); There is one to one relationship between names and files. There needs to be mapping created between names and files. The output should be like this: amith --> file.txt veena --->... (3 Replies)
Discussion started by: vanitham
3 Replies

7. Shell Programming and Scripting

grep in perl

Hello I want to grep a line from a file saved in some directory. Can anyone please correct the code below: #!/usr/bin/perl -w $file = "/home/output.txt" $grep_line = "closing zip for topic"; `grep $grep_line* $file`; (1 Reply)
Discussion started by: sureshcisco
1 Replies

8. Shell Programming and Scripting

Perl map doubt

Hello , Please can someone tell me what exactly happens when the below filehandler is chomped into an array and later mapped. $lcpLog="logcopy\@".getTimestamp."\log"; open CFg ,"< $lcpcfg"; chomp(@cfg = <CFG>); close CFG; @cfg=grep { $_ ne ' ' } map { lc + (split /\s*\/\//) }... (0 Replies)
Discussion started by: rmv
0 Replies

9. Shell Programming and Scripting

Perl grep

OK here's the situation: I have got these lines which I have got to parse. If the line contains a particular string and any element from a previously defined array I need to take that particular line and do some further processing. if ((grep(/$_/,$1)) && (grep($pattern,@myarr))) { #Do... (2 Replies)
Discussion started by: King Nothing
2 Replies

10. Shell Programming and Scripting

grep using Perl

I'm using perl to do a grep of each line in a vendor file and find its occurrences in a specific directory. Any values found is saved in @dir. .....(file opened, etc.) .... while ($line=<FILE>){ @dir = `grep $line * `; } It's the specific usage of the system grep that I'm having... (7 Replies)
Discussion started by: gavineq
7 Replies
Login or Register to Ask a Question