6

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='\015'  # 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
Tuna
  • 2,937
  • 4
  • 37
  • 61
user375191
  • 123
  • 2
  • 4
  • 7
  • 1
    http://www.google.com/search?q=unix2dos.bat – Heinzi Jun 24 '10 at 12:50
  • 2
    @Heinzi: http://meta.stackexchange.com/questions/5280/embrace-the-non-googlers – Joey Jun 24 '10 at 18:15
  • 1
    @Johannes: Actually, my comment did not just google the question but contain an answer. Yes, I was being overly concise; the verbose version would be: "You don't need to do that yourself; there's a script called unix2dos which does exactly what you want and there are Windows ports available, usually called `unix2dos.bat`. If you google for that keyword, you will find a lot of sources to download it." (Still, I do get your point, thanks for the link.) – Heinzi Jun 24 '10 at 20:49
  • 1
    Possible duplicate of [Windows command to convert Unix line endings?](http://stackoverflow.com/questions/17579553/windows-command-to-convert-unix-line-endings) – phuclv Mar 13 '17 at 01:48

2 Answers2

13

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 :-)).

Joey
  • 344,408
  • 85
  • 689
  • 683
0

Building on the generally noted way of doing this using the type command, you can also convert all of the files (or whatever wildcard you may prefer) and dump them in a temp folder using the following:

md temp
for %a in (*.*) do type "%a" | find /v "" > temp\"%a"

If the general idea was to replace the originals then you can just move the files back out of the temporary location and delete the temp folder

JJohnston2
  • 111
  • 6