Solved

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

Posted on 2008-06-20
8
1,595 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

706 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

18 Experts available now in Live!

Get 1:1 Help Now