Perl combine multiple map statements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl combine multiple map statements
# 1  
Old 08-05-2014
RedHat Perl combine multiple map statements

I have a file like

Code:
file.
file.TODAY.THISYEAR
file.TODAY.LASTYEAR

I want to substitute the words in caps with their actual values so that output should look like
Code:
file.140805
file.140805.2014
file.140805.2013

For this I am reading the file line bye line in an array and using multiple map statements for substitution, as below
Code:
@files = map { $_ =~ s/\.$/\.$TODAY/g; $_ } @files;
@files = map { $_ =~ s/TODAY/$TODAY/g; $_ } @files;
@files = map { $_ =~ s/THISYEAR/$THISYEAR/g; $_ } @files;
@files = map { $_ =~ s/LASTYEAR/$LASTYEAR/g; $_ } @files;

can anyone suggest how can I combine these multiple map statements in one

PS: answers only in perl please

Thanks
Sam
# 2  
Old 08-05-2014
Quote:
Originally Posted by sam05121988
...For this I am reading the file line bye line in an array and using multiple map statements for substitution, as below
Code:
@files = map { $_ =~ s/\.$/\.$TODAY/g; $_ } @files;
@files = map { $_ =~ s/TODAY/$TODAY/g; $_ } @files;
@files = map { $_ =~ s/THISYEAR/$THISYEAR/g; $_ } @files;
@files = map { $_ =~ s/LASTYEAR/$LASTYEAR/g; $_ } @files;

can anyone suggest how can I combine these multiple map statements in one
...
Code:
$
$ perl -le '@files = qw (file. file.TODAY.THISYEAR file.TODAY.LASTYEAR);
            $TODAY = "140805"; $THISYEAR = "2014"; $LASTYEAR = "2013";  # should not be hard-coded
            foreach $i (@files) { print $i }
            @new_files = map { $_ =~ s/\.$/.$TODAY/;
                               $_ =~ s/\.TODAY\.THISYEAR/.$TODAY.$THISYEAR/;
                               $_ =~ s/\.TODAY\.LASTYEAR/.$TODAY.$LASTYEAR/; $_
                             } @files;
            print "==========================";
            foreach $i (@new_files) { print $i }
           '
file.
file.TODAY.THISYEAR
file.TODAY.LASTYEAR
==========================
file.140805
file.140805.2014
file.140805.2013
 
$
$

This User Gave Thanks to durden_tyler For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combine 4 awk pattern count statements into 1

Hello fellow awkers, I am trying to combine the following awk statements into 1 so that the results are more accurate: awk '/\=\+/ { count++ } END { print count}' filename awk '/\=\?/ { count++ } END { print count}' filename awk '/\=\-/ { count++ } END { print count}' filename awk... (8 Replies)
Discussion started by: ux4me
8 Replies

2. Shell Programming and Scripting

Combine multiple unique lines from event log text file into one line, use PERL or AWK?

I can't decide if I should use AWK or PERL after pouring over these forums for hours today I decided I'd post something and see if I couldn't get some advice. I've got a text file full of hundreds of events in this format: Record Number : 1 Records in Seq : ... (3 Replies)
Discussion started by: Mayday22
3 Replies

3. UNIX for Dummies Questions & Answers

Combine two awk statements into one

Hi, I have the following two awk statements which I'd like to consolidate into one by piping the output from the first into the second awk statement (rather than having to write kat.txt out to a file and then reading back in). awk 'BEGIN {FS=OFS=" "} {printf("%s ", $2);for (x=7; x<=10;... (3 Replies)
Discussion started by: kasan0
3 Replies

4. Shell Programming and Scripting

simplify/combine if statements would be nice

below is something i inherited: if && && ; then HOST_SELECT="-m quadcore" fi if && && ; then HOST_SELECT="-m quadcore" fi if && && ; then HOST_SELECT="-m octocore1" fibelow is what i changed it to: if && && ; then HOST_SELECT="-m quadcore"... (2 Replies)
Discussion started by: crimso
2 Replies

5. UNIX for Dummies Questions & Answers

Struggling to combine two Greps statements

Greetings! I have been tasked to create a report off files we receive from our hardware suppliers. I need to grep these files for two fields 'Test_Version' and 'Model-Manufacturer' ; for each field, I need to capture their corresponding values. When running each statement separately, I get... (4 Replies)
Discussion started by: alan
4 Replies

6. Shell Programming and Scripting

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: print grep {s/{3}/X/g} <F>; # will do the subtitution fine, output XXXX ... (1 Reply)
Discussion started by: new bie
1 Replies

7. Shell Programming and Scripting

Combine awk statements

I have an awk statement that works but I am calling awk twice and I know there has to be a way to combine the two statements into one. The purpose is to pull out just the ip address from loopback1. cat config.txt | nawk 'BEGIN {FS="\n"}{RS="!"}{if ( $0 ~ "interface loopback1" ) print$4}' | nawk... (5 Replies)
Discussion started by: numele
5 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

combine two grep statements

Hi I am wondering is it possible to combine two greps together I have two greps. grep "^,, *\." file (grep the line which has a '.' in the third column) grep "=" file (grep the line which has = anywhere) How to put them together so that if the content of the file that match either... (1 Reply)
Discussion started by: tiger66
1 Replies

10. UNIX for Dummies Questions & Answers

How to combine case statements

Hi, I need to change military time to regular time. I know to use case to indicate whether a.m. or p.m. as follows: case "$hour" in 0? | 1 ) echo a.m.;; 1 ) echo p.m.;; * ) echo p.m.;; esac My question is how do I add the hour and minute... (2 Replies)
Discussion started by: karp3158
2 Replies
Login or Register to Ask a Question