Solved

Using Perl to replace LF with CR LF in all files in a directory

Posted on 2008-06-20
8
1,596 Views
Last Modified: 2008-06-24
I'm trying to run the following command over multiple files in a directory (the syntax below works for individual files).  The command replaces all the LF in the files with CRLF.  I want to place this command in a batch file that i can schedule to run automatically.  I also tried using wild cards *.* to execute over all files, but it doesn't work.  any suggestions?  thanks

perl -pe "s/\x{0d}/\x{0a}/g" c:\source\input1 > c:\destination\output1
0
Comment
Question by:snakahara7
  • 4
  • 3
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 21834092
perl -ic:/destination/* -ple "BEGIN{chdir 'c:/source';@ARGV=<*>;$/=qq(\n);$\=qq(\r\n)}"
0
 

Author Comment

by:snakahara7
ID: 21834385
i tried it and it does go through all the files in the directory, but it doesn't do the conversion.  thanks.
0
 

Author Comment

by:snakahara7
ID: 21834978
it goes through all the files in the directory and makes a back up of them, but does it then convert the file and put the output somewhere else? or is it supposed to overwrite the original file?
0
 
LVL 9

Expert Comment

by:ghostdog74
ID: 21836398
since you are using Perl, then do everything in Perl
#!/usr/bin/perl
 
 

while ( <*.*> ) {

  

  open(FH, "<", $_);

  

   while( <FH> ) {

    $line=$_;

    $line =~ s/\n/\r\n/g;

    print $line;

   

   }

  close(FH);
 

}

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 84

Expert Comment

by:ozo
ID: 21836454
#!/usr/bin/perl
chdir 'c:/source';
$^I='destination/*';
@ARGV=<*>:
$^I=.bak;
while( <>  ){
  s/\n/\r\n/;
  print;
}
0
 

Author Comment

by:snakahara7
ID: 21861679
thanks for the code snippets... i used the code and changed a few things and it's working properly (saved the program as processfiles.pl).  If i call it from the command line, it works fine, but if i place it in a .bat file and run the batch file, it opens a command window and shows "c:\test>perl processfiles.pl".  It just stays like that and never returns from the batch program.  when i eventually close the command window, it's as if the perl program was not run at all from the batch routine.  i have the following in the batch program:

move c:\source\* c:\destination            moves files to directory for processing
perl processfiles.pl                               perl program


processfiles.pl code:

#!/usr/bin/perl
chdir 'c:/source';
$^I='c:/destination/*';
@ARGV=<*>;
while( <>  ){
  s/\x{0d}/\x{0a}/g;
  print;
}

Any idea why my batch routine never returns/ends?  it just seems like it's stuck or in an infinite loop.
Thanks in advance for all your help...
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 21861726
after
move c:\source\* c:\destination
are there any files in c:\source left for the perl script to process?
If you want replace LF with CR LF in all files  all files in c:\destination
#!/usr/bin/perl
$^I='.bak';
@ARGV=<c:/destination/*>;
while( <>  ){
  s/\x{0d}/\x{0a}/g;
  print;
}
0
 

Author Comment

by:snakahara7
ID: 21861803
you are right... i had my directories mixed up and had moved all my files OUT of the processing folder.  Now that there's files to process, it is working GREAT!!!

Thanks ozo for all your help!!!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about a problem I had getting the GPS LocationListener working.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

930 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

12 Experts available now in Live!

Get 1:1 Help Now