Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Perl code question!

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


# $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:

I would like them in this format:

The output I get from the script above is:

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.
Question by:soccerplayer
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 84

Expert Comment

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

Author Comment

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

Author Comment

ID: 18794694
Also, can you explain the line so that I can learn? Thanks.
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.


Author Comment

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"?
LVL 84

Accepted Solution

ozo earned 2000 total points
ID: 18794787

# $1: /devetl/ptqs/DFS2_Assr_03_02/

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

Expert Comment

ID: 18795102
is a lis of filenames
perldoc -f glob
join ','
puts ',' between elements of a list
perldoc -f join
perldoc -f print

Author Comment

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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

636 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