Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

batch file + convert LF to CR+LF

We have a shell script file named LineFeed.sh which does a function of converting a Linefeed(LF) to Carriage Return + LineFeed. We want the same to be done by a batch file in windows . Is it possible?

Linux shell file

E_WRONGARGS=65
cat OutputList|while read -r Line 
do 
if [ -z "$Line" ]
then
echo "Usage: `basename $0` filename-to-convert"
exit $E_WRONGARGS
fi
NEWFILENAME=$Line.unx
CR='15'  # Carriage return.
       # 015 is octal ASCII code for CR.
       # Lines in a DOS text file end in CR-LF.
       # Lines in a UNIX text file end in LF only.
tr -d $CR < $1 > $NEWFILENAME // here its deleting CR but i need to append LF
# Delete CR's and write to new file.
done
echo "Original DOS text file is "$1"."
echo "Converted UNIX text file is "$NEWFILENAME"."
exit 0
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can find one way on this Wikipedia page:

TYPE unix_file | FIND "" /V > dos_file

Remember that you can't redirect the output to the same file you're reading from. This applies to pretty much all systems and shells, so an additional rename is necessary.

The key here is that type knows how to read LF line endings and find will then convert them do CRLF. type alone won't do anything with the output (it's supposed to, because having a command that simply dumps the file contents messing with them isn't good :-)).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...