By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,852 Members | 2,198 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,852 IT Pros & Developers. It's quick & easy.

Appending the required string between the variable(concatenation) ?

P: 65
Hi Everyone,

i need to add a string in between the filename.so that new file will have the
required filename.

what am doing is reading the files from the directory & extract only the filename and then pass this failename to create new file.

now what i need is instead of just passing filename, i need to add some string to this filename & then pass the filename to create a new file.

Expand|Select|Wrap|Line Numbers
  1. sub loadavg_file(){
  2. my $dir = 'C:\Performance_svap\INPUT_FILES\*.xls'; 
  3. my @file=glob("$dir"); 
  4. my @output;
  5. my $loadavg;
  6. my $str;
  7. my $str1;
  8. my @data;
  9. my $date;
  10. foreach my $f (@file){
  11.  $str = substr($f,32,39);
  12.  $str1 = substr($str,0,9);
  13.  $loadavg ="c:\\Performance_svap\\OUTPUT_FILES\\$str";
  14.  
  15. blaah blaah...
  16. ....
  17. ....
  18. }
  19. open(EX,">$loadavg") or die "Can't open open $loadavg:$!";
  20. print EX "$output[0]\t$output[1]\t$output[2]\t$output[3]\t$output[4]\n"; 
  21. close EX;
  22. }
  23.  
so, in the above script, $str variable holds the filename. now to this variable i need to append the required string in between ie,
Expand|Select|Wrap|Line Numbers
  1. $str will have = prstat-Ls-20080118-1800.xls value.
  2.  
now i need to append the string to this variable & it should look like
Expand|Select|Wrap|Line Numbers
  1. $str = prstat-Ls-LAVG-20080118-1800.xls
  2.  
i tried changing with first instance ie,
Expand|Select|Wrap|Line Numbers
  1. $str = substr($f,32,39);
  2. $str1 = substr($str,0,9);
  3. $str = "LAVG".$str;
  4. $loadavg ="c:\\Performance_svap\\OUTPUT_FILES\\$str";
  5.  
am not getting how append the string in between the file name.

any help or suggestions will great full for me.

Thanks,
Vijayarl
Oct 24 '08 #1
Share this Question
Share on Google+
6 Replies


P: 5
You can use the period operator like this:

$str = $str1 . "LAVG" . $str;
Oct 24 '08 #2

P: 65
hey thanks for reply !!! i try it right away !!!

but what i feel is what you have suggested will give this i guess :
Expand|Select|Wrap|Line Numbers
  1. $str1 = substr($str,0,9); # holds the value =prstat-Ls
  2.  
so i guess i will get this ouput from your logic
Expand|Select|Wrap|Line Numbers
  1. $str=prstat-Ls-LAVG-prstat-Ls-20080118-1800.xls
  2.  
anyway i will cm back !!!

Regards,
Vijayarl
Oct 24 '08 #3

P: 65
hi scruff6119,

my guess was right... am getting output like this
Expand|Select|Wrap|Line Numbers
  1. prstat-Ls-LAVG-prstat-Ls-20080118-1800.xls
  2.  
i instead want the file name to look like this:
Expand|Select|Wrap|Line Numbers
  1. prstat-Ls-LAVG-20080118-1800.xls
  2.  
thanks for your reply

any help or idea ???

Regards,
Vijayarl
Oct 24 '08 #4

P: 65
hi there,

i got it...it was simply..
Expand|Select|Wrap|Line Numbers
  1. foreach my $f (@file){
  2. $str = substr($f,32,39);
  3. $str1 = substr($str,0,9);
  4. $str2 = substr($str,10,27);
  5. $str = $str1 . "-LAVG-" . $str2;
  6. $loadavg ="c:\\Performance_svap\\OUTPUT_FILES\\$str";
  7. }
  8.  
Thanks you...

Regards,
Vijayarl
Oct 24 '08 #5

P: 5
Could do it by regex too:

$str =~ s/(.*?Ls-)(\d{8}.*?\.xls)/$1LANG-$2/
Oct 24 '08 #6

P: 65
hey thanks scruff6119 !!!!

Even this worked...As am not so good at regex.. i didn't thought of this idea..

anyway's thanks for you help :-)

Regards,
Vijayarl
Oct 24 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.