fhg
Volume 6, Number 41 -- November 15, 2006

Stream Files and End-of-Line Delimiters, Take Three

Published: November 15, 2006

Hey, Ted:

I do a bit of development in Cygwin under Windows and then move it to the iSeries using Qshell. It turns out that Qshell doesn't like the carriage-return/linefeed combination (CRLF) in shell scripts, so I often need to change CRLF to LF--as you wrote about in two previous articles--in bulk. Here are some methods I use to convert end-of-line delimiters in stream files.

The first method is a simple Perl command.

perl -i.bak -pe 's/\r\n/\n/g' * 

This command adds the extension .bak to the end of the files and strips the CRLF, putting in LF instead. By using a wildcard, I can convert a whole directory at a time rather than one file at a time. One could just as easily handle a single file by changing the wildcard at the end from * to the name of the file that one wants to convert.

Another way is to use a Qshell script like this one, which I call dos2unix, after the Linux command of the same name:

#!/bin/bash 

# Replace CRLF with LF 

E_WRONGARGS=65 

if [ -z "$1" ] 
then 
echo "Usage: `basename $0` filename-to-convert" 
exit $E_WRONGARGS 
fi 

file="$1" 
sed 's/\r\n/\n/g' $file > $file.fixed 
cp -f $file.fixed $file 
rm -f $file.fixed 

An alternative to sed is tr:

tr -d '\r' < $file > $file.fixed 

--Buck Calabro


Thanks, Buck. I appreciate the solutions. Little junk like this tends to eat up my day.

--Ted


RELATED STORIES

EDTF and End-of-Line Delimiters

EDTF and End-of-Line Delimiters, Take Two



Sponsored By
ITERA

HIGH AVAILABILITY CASE STUDY:

 

When the tornados hit,
TheBANK of Edwardsville was prepared.

 

Spring's balmy 70-degree weather brought several tornados to St. Claire County, Illinois. One powerful storm left dozens of businesses and residents without electricity for nearly a week.

 

While thousands of people were caught by surprise, TheBANK of Edwardsville was not.

 

Read the complete story here.



Senior Technical Editor: Ted Holt
Technical Editors: Howard Arner, Joe Hertvik, Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Brian Kelly, Marc Logemann, David Morris
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

Patrick Townsend & Associates:  Alliance AES/400 - database field encryption
New Generation Software:  Leading provider of iSeries BI and financial management software
COMMON:  Join us at the Annual 2007 Conference & Expo, April 29 - May 3, in Anaheim, California

 


 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement