The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl -write values in a file to @array in perl meghana Shell Programming and Scripting 27 06-07-2009 06:05 PM
pass each elements in array to ssytem command jaganadh Shell Programming and Scripting 0 12-14-2007 06:57 AM
To return the elements of array Sudhakar333 Shell Programming and Scripting 5 08-06-2007 03:20 PM
How can i read array elements dynamically in bash? haisubbu UNIX for Dummies Questions & Answers 1 08-29-2006 03:19 AM
how to return an array of elements from oracle to shell script satyakiran Shell Programming and Scripting 3 08-02-2005 10:57 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-02-2008
bperl bperl is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 9
Perl - New line in array elements

Hello,
I have a comma delimited input feed file. The first field has directory location and the second field has file name.
Ex of input feed:

/export/appl/a,abc*.dat
/export/appl/b,xyz*.dat
/export/appl/c,pmn*.dat


Under each directory, there would be many files like...
.
.
.
abc.200801.dat
abc.200802.dat
abc.200803.dat
abc.200804.dat

I'm writing a perl script, in which part of the logic is to get the stat() of the latest file in the corresponding directory. (in this case I need to get stat() of abc.200804.dat

So, first I'm getting the directory and then chdir to that location and doing a ls -ltr with the wild card file names and storing them in array. Then using $


Code:
foreach my $input_var (@i_raw_data)
{
 chop($input_var);
 my ($d_loc,$wild_f_nme)=split(/\,/,$input_var);
 chdir($d_loc);
 my @list_files = `ls -c $wild_f_nme`;  #store all file names
 my $f_nme=$list_files[-1];    #it will have latest modified filename

 my ($accesstime, $modtime, $createtime, $fsize) = (stat($f_nme))[8,9,10,7];

The problem is, I think when I'm trying to get the last changed file into the array element, its storing the latest file in the last element, but I think its appending new line to the element along with file name. So, the stat($f_nme) isn't working.

Can you please let me know what I'm missing?
Thanks!
  #2 (permalink)  
Old 04-02-2008
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
Evaluating backticks in list context, Perl does not automatically drop the newlines from the command output for you. You should do that yourself.

Compare the following two cases:


Code:
[bernardchan@bernardchan ~]$ perl -MData::Dumper -e '@lines = `ls /usr/local`; print Dumper(\@lines)'
$VAR1 = [
          'Adobe
',
          'apps
',
          'bin
',
          'Brother
',
          'etc
',
          'games
',
          'include
',
          'lib
',
          'libexec
',
          'man
',
          'RealPlayer
',
          'sbin
',
          'share
',
          'src
'
        ];
[bernardchan@bernardchan ~]$ perl -MData::Dumper -e '@lines = map { chomp; $_ } (`ls /usr/local`); print Dumper(\@lines)'
$VAR1 = [
          'Adobe',
          'apps',
          'bin',
          'Brother',
          'etc',
          'games',
          'include',
          'lib',
          'libexec',
          'man',
          'RealPlayer',
          'sbin',
          'share',
          'src'
        ];

As you can see, there are trailing newline with the first case, but not after post-processing each item removing the trailing newlines.
  #3 (permalink)  
Old 04-02-2008
quine quine is offline
Registered User
  
 

Join Date: Mar 2008
Location: Bay Area California
Posts: 68
Basically what he said... Use chomp($f_nme[-1]) first, then assign it. Use chomp() above where you get the directory list from your input file too... Is safer. chop() will strip the last char of a string ALWAYS. chomp() strips only if is a LINE ENDING, so is much safer usually if you are not always SURE that the last char of your string is the line ending you want to remove....
  #4 (permalink)  
Old 04-02-2008
bperl bperl is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 9
Quote:
Originally Posted by quine View Post
Basically what he said... Use chomp($f_nme[-1]) first, then assign it. Use chomp() above where you get the directory list from your input file too... Is safer. chop() will strip the last char of a string ALWAYS. chomp() strips only if is a LINE ENDING, so is much safer usually if you are not always SURE that the last char of your string is the line ending you want to remove....
I should have mentioned it.... I've used both chomp($list_files[-1]) and chop($list_files[-1]). It doesn't seem to work.


Code:
my $f_nme=chomp($list_files[-1]); 
print " last file name is $f_nme \n";

It doesn't assign any value to $f_nme.

And chomp(($list_files[-1]) returns "1" ?!



Code:
foreach my $input_var (@i_raw_data)
{
 chop($input_var);
 my ($d_loc,$wild_f_nme)=split(/\,/,$input_var);
 chdir($d_loc);
 my @list_files = `ls -c $wild_f_nme`;  #store all file names
 my $f_nme=chomp($list_files[-1]);    #it will have latest modified filename

 my ($accesstime, $modtime, $createtime, $fsize) = (stat($f_nme))[8,9,10,7];

Anyways, I have used the way cbhikong suggested and it worked. Thanks!

Last edited by bperl; 04-02-2008 at 11:47 PM..
  #5 (permalink)  
Old 04-03-2008
quine quine is offline
Registered User
  
 

Join Date: Mar 2008
Location: Bay Area California
Posts: 68
Yes, you misunderstand chomp() chop(). They actually transform their argument. What they RETURN is a 1 or 0 (I think)...

So $y = chop($x); actually changes $x and assigns 1 (success) to $y. So you don't ASSIGN the results, you just do it....

$x = "abcd";

chop($x);

Now $x is "abc"
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:20 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0