Solved

Perl code question!

Posted on 2007-03-26
7
295 Views
Last Modified: 2010-04-22
I am trying to create a file of file names in a directory. Here is my script:

-------------------------------------------------------
#!/bin/ksh

#Parameters
# $1: /devetl/ptqs/DFS2_Assr_03_02/

cd "$1"

/bin/ls FinalFile*.txt* > Out.fil
perl -i -ne 's/FinalFile/,FinalFile/g; print;' Out.fil
cat Out.fil | perl -e '@f=<STDIN>; $f[$#f] =~ s/(.*),(.*)/$1$2/; print @f;' > Out2.txt

#Remove the extra ^@ characters that appreas in the file
rm -f Out.fil
tr -d '\n' <Out2.txt > Out.fil
rm -f Out2.txt
cat Out.fil
-------------------------------------------------------

The input parameter, $1, is the directory path.

The files in my directory are:
FinalFile1.txtaa
FinalFile1.txtab
FinalFile2.txtaa
FinalFile2.txtab

I would like them in this format:
FinalFile1.txtaa,FinalFile1.txtab,FinalFile2.txtaa,FinalFile2.txtab

The output I get from the script above is:
,FinalFile1.txtaa,FinalFile1.txtab,FinalFile2.txtaaFinalFile2.txtab

Note that the last comma separator is missing.

One major flaw in the script above is that, it uses $2 as the second
parameter eventhough only one parameter is passed in. This errors out if I
remove the $2 from the $1$2 which is strange!

Any help would be appreciated.
0
Comment
Question by:soccerplayer
  • 4
  • 3
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 18794661
perl -le 'print join",",<FinalFile*.txt*>' >Out.fil
0
 

Author Comment

by:soccerplayer
ID: 18794688
ozo, where does this line go in the script?
0
 

Author Comment

by:soccerplayer
ID: 18794694
Also, can you explain the line so that I can learn? Thanks.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:soccerplayer
ID: 18794742
ozo, two questions.
1)How come we are not using the passed in parameter, $1, which is the directory path, anywhere?
2)Can I replace "FinalFile" by a second parameter? That is, wherever "FinalFile"
appears, can I replace it with "$2"?
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 18794787
#!/bin/ksh

#Parameters
# $1: /devetl/ptqs/DFS2_Assr_03_02/

cd "$1"
perl -e "print join',',<$2*.txt*>"
0
 
LVL 84

Expert Comment

by:ozo
ID: 18795102
<FinalFile*.txt*>
is a lis of filenames
perldoc -f glob
join ','
puts ',' between elements of a list
perldoc -f join
print
prints
perldoc -f print
0
 

Author Comment

by:soccerplayer
ID: 18795330
Thank you very much for the solution. Worked like a charm!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now