You are not logged in.

#1 2018-09-21 15:10:59

stf92
Member
Registered: 2018-05-06
Posts: 23

[SOLVEDUnix style to ms-dos style line terminator one-liner converter.

Hi: I want to convert a whole batch of plain ASCII text files, so the vim command would be a little too much work. Sed or tr, preferably the latter, could do the job, I think. Exactly how?

Last edited by stf92 (2018-09-23 13:22:51)

Offline

#2 2018-09-21 15:18:17

loqs
Member
Registered: 2014-03-06
Posts: 19,113

Re: [SOLVEDUnix style to ms-dos style line terminator one-liner converter.

What about dos2unix ?

Offline

#3 2018-09-21 15:18:32

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: [SOLVEDUnix style to ms-dos style line terminator one-liner converter.

man dos2unix

No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#4 2018-09-21 16:59:06

stf92
Member
Registered: 2018-05-06
Posts: 23

Re: [SOLVEDUnix style to ms-dos style line terminator one-liner converter.

Let's say 'foo file_1 file_2' converts file_1 into file_2 and I have these files in directory ./

$ ls -1
text01.unix
..........
textNN.unix
$

Then  I need to use foo only once and produce the output

$ ls -1 *dos
text01.dos
.........
textNN.dos
$

Pipes and wildcards are a solution. BUT dos2unix won't allow me the use of wildcards (-n option). Plus, why use such a complex program as its manual shows it is when tr can accomplish the same job in the most straightforward way?

Offline

#5 2018-09-21 18:57:20

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 78,840

Re: [SOLVEDUnix style to ms-dos style line terminator one-liner converter.

Well, of course you can use tr to change newline variants, so why did you ask in the first place if you already knew that???

Offline

#6 2018-09-21 19:14:12

teckk
Member
Registered: 2013-02-21
Posts: 590

Re: [SOLVEDUnix style to ms-dos style line terminator one-liner converter.

Examples:
Loop through files in a directory and change them.

for i in *; do sed -i 's/$/\r/' "$i"; done

for i in *; do perl -pi -e 's/\n/\r\n/'; done

for i in *; do unix2dos "$i"; done
sed 's/^M$//'     # DOS to Unix
sed 's/$/^M/'     # Unix to DOS
sed $'s/\r$//'     # DOS to Unix
sed $'s/$/\r/'     # Unix to DOS
awk -v ORS='\r\n' '1' unix.txt > dos.txt
vim file.txt -c "set ff=dos" -c ":wq"

Or keep the old files, make new ones

tr -d '\r' < windows.txt > unix.txt
perl -p -e 's/\r$//' < windows.txt > unix.txt
sed 's/\r$//' windows.txt > unix.txt

Found a POSIX way

ex -bsc '%!awk "{sub(/\r/,\"\")}1"' -cx file

Echo those commands first to see what they do.
Read the man pages.

Last edited by teckk (2018-09-21 19:14:27)

Offline

#7 2018-09-23 13:22:04

stf92
Member
Registered: 2018-05-06
Posts: 23

Re: [SOLVEDUnix style to ms-dos style line terminator one-liner converter.

Thanks a lot. Job accomplished.

Offline

#8 2018-09-23 14:53:13

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,541
Website

Re: [SOLVEDUnix style to ms-dos style line terminator one-liner converter.

There is no need for all those for loops all those commands can take multiple file arguments:

sed -i 's/$/\r/' *

"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

Board footer

Powered by FluxBB