3

I am having trouble with Windows files running as part of a shell script on a Linux box and I was wondering if it is possible to "convert" a Windows created file to a Linux one using Java or possibly a Linux command?

For example:

If I upload a CSV file created in Windows, then on the server using nano I can see the file was saved in DOS Format. I can toggle between DOS and Linux format using M-D and save it as a Linux file.

My question is whether it is possible to achieve this via Java (<-preference) or through a Linux command?

I have tried stripping carriage returns sed -i 's/{ctrl-v}{ctrl-m}//g' [file] but this does not help with the issue.

My Head Hurts
  • 37,315
  • 16
  • 75
  • 117

4 Answers4

11

dos2unix and unix2dos unix commands, from the dos2unix package.

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
3

if you don't have dos2unix utility available but you have vim or vi installed

open your file:

vi yourfile.csv or vim yourfile.csv

To change the file format to unix type:

:setlocal ff=unix

Then save the file:

:wq!

Reference: http://vim.wikia.com/wiki/File_Format

brunocrt
  • 720
  • 9
  • 11
  • This answer deserves much more votes as it doesn't require any package installation. +1 for the reference. – dbaltor Jul 27 '23 at 16:42
1

You can use BufferedReader with a contained FileReader to get the File line by line and then do whatever you want with that lines (e.G. push them into another File that has the correct line endings.

File relFile = new File(".....");
BufferedReader read = new BufferedReader(new FileReader(relFile));
File targetFile = new File("....");
FileWriter fwri = new FileWriter(targetFile);
String line;
while ((line = read.readLine()) != null) {
    fwri.write(line+"\n"); // if you want the system line delimiter use the System property for that.
}
fwri.flush();
fwri.close();
Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
0

use vim to check your encoding format:

:set fileencoding

in windows, it is normally gb2312, but in linux it is utf-8.

So you'd better to convert it to linux format by following method:

:set fileencoding=utf-8

and then save it by :wq!

Haimei
  • 12,577
  • 3
  • 50
  • 36