You are not logged in.
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
What about dos2unix ?
Offline
Offline
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
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
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"; donesed '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.txtFound a POSIX way
ex -bsc '%!awk "{sub(/\r/,\"\")}1"' -cx fileEcho those commands first to see what they do.
Read the man pages.
Last edited by teckk (2018-09-21 19:14:27)
Offline
Thanks a lot. Job accomplished.
Offline
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