Perl Unix Script Writing


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Perl Unix Script Writing
# 1  
Old 02-01-2008
Bug Perl Unix Script Writing

Hi Folks,

I posted a few days ago, thanks for the responses. My original question was for renaming files of sort 3p2325294.dgn in a directory containing multiple files. I need to drop the first 2 characters and the last in a unix script using Perl. How does it differ from using the Unix Shells? Syntax examples please.

Much appreciated.

Dinkster
# 2  
Old 02-01-2008
Well, if You use the shell You are working in, from the command line or in a script, You are using less resources than You would do if You invoke an external program. You are using functions that are "already there". It's not very important in day to day work if a job takes 10 seconds instead of 2. The trade-off comes into play when You are dealing with very large amounts of data. Perl can be very efficient but if You are only traversing Your home directory for renaming dgn-files, it is probably much easier to just use what You already have. Portability and complexity are other considerations.

Your example could be expressed in a shell (this works for bash on the command line) as:

Code:
for x in *.dng;do mv $x ${x:2};done

meaning, for each file that matches the pattern *.dng, rename it to the same name but cut out the first two characters, or rather, start at character index 2. The index starts from 0, so Your file 3p2325294.dgn would be renamed to 2325294.dgn

I think that once You get used to the shell You realise the power of it. There are so many examples of piping stuff through sed and awk and perl, when the answer is already at Your fingertips. I've done it myself a lot. It may be easier because You know how sed works so You go for it instead of exploring the shell equivalent.

And I'm not entirely sure about how it would be written in Perl, I'm a bit rusty in that department...

/Lakris
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Writing xml from excel sheet .xls using perl script

Hi all. I am working on the below requirement of generating .xml file from .xls file which i have , can someone please help me or in writing the perl script for the same: The xls file format is as below which has two columns and number of rows are not fixed: Fixlet Name ... (12 Replies)
Discussion started by: omkar.jadhav
12 Replies

2. Shell Programming and Scripting

Need help in writing perl script

Hi, I am new to perl. I am trying to write a small perl script for search and replace in a file : ======================================================== #!/usr/bin/perl my $searchStr = "register_inst\.write_t\("; my $replaceStr = "model\.fc_block\."; open(FILE,"temp.sv") ||... (2 Replies)
Discussion started by: chettyravi
2 Replies

3. Shell Programming and Scripting

Perl script for Calling a function and writing all its contents to a file

I have a function which does awk proceessing sub mergeDescription { system (q@awk -F'~' ' NR == FNR { A = $1 B = $2 C = $0 next } { n = split ( C, V, "~" ) if... (3 Replies)
Discussion started by: crypto87
3 Replies

4. Programming

REQUIRE HELP IN WRITING A PERL SCRIPT

Hi everyone I am a beginner in perl and I am trying to write a perl script. Basically I want to separate gene entries from phenotype entries in a text file which contains huge number of records and copy them in a separate file. The gene entries will have * symbol after the line FIELD TI. A... (7 Replies)
Discussion started by: kaav06
7 Replies

5. Shell Programming and Scripting

Writing a Perl Script that processes multiple files

I want to write a Perl script that manipulates multiple files. In the directory, I have files 250.*chr$.ped where * is from 1 to 1000 and $ is from 1-22 for a total of 22 x 10,000 = 22,000 files. I want to write a script that only manipulates files 250.1chr*.ped where * is from 1 to 22.... (10 Replies)
Discussion started by: evelibertine
10 Replies

6. Shell Programming and Scripting

need help writing this unix script

Create an executable script file called "newname" that will perform the followings: 1. Rename a file upon the user's request. If the file exists, prompt the user for confirmation before renaming the file. The screen should prompt the user for a. "Name of file you want to rename." Use the "\c"... (7 Replies)
Discussion started by: wiggles
7 Replies

7. Shell Programming and Scripting

help writing this unix script

I am working on writing scripts. Here is a script I need help with. I have also wrote what I think it is. I would really appreciate any help that I can get. Create an executable script file called "newname" that will perform the followings: 1. Rename a file upon the user's request. If the... (2 Replies)
Discussion started by: wiggles
2 Replies

8. Shell Programming and Scripting

Need help with writing a perl script

Hi all! I have to write a perl script that gets trashholds from a file and match them with an output of a command. The trashhold file looks like this: "pl-it_prod.GW.Sync.reply.*" "500" "-1" "" "" "pl-it_prod.A.*" "100" "-1" "" "" "application.log" ... (29 Replies)
Discussion started by: eliraza6
29 Replies

9. Shell Programming and Scripting

help for a perl script - writing to a data file

Hi, Here is my problem.. i have 2 files (file1, file2).. i have wrote the last two lines and first 4 lines of "file2" into two different variables .. say.. my $firstrec = `head -4 $file2`; my $lastrec = `tail -2 $file2`; and i write the rest of the file2 to a tmpfile and cat it with head... (2 Replies)
Discussion started by: meghana
2 Replies

10. Shell Programming and Scripting

Need help in writing a unix script

OS: Solaris Shell : KSH Please help me in writing a script that captures a error message from a log file ( which updates continiously ) and send an email alert as soon as the systems throws a error message into that log. i.e With out monitoring the log Thanks in advance.. (1 Reply)
Discussion started by: pray44u
1 Replies
Login or Register to Ask a Question