your programing

vim에서 DOS 줄 끝을 Linux 줄 끝으로 변환

lovepro 2020. 9. 30. 11:09
반응형

vim에서 DOS 줄 끝을 Linux 줄 끝으로 변환


Windows에서 만든 파일을 열면 줄이 모두 ^M.
이 문자를 한꺼번에 삭제하려면 어떻게해야합니까?


DOS2UNIX는 이 작업을 수행하는 명령 줄 유틸리티입니다, 또는 :%s/^M//g당신이 사용하는 경우 Ctrl- v Ctrl- m입력 ^ M으로, 또는 당신은 할 수 :set ff=unix와 정력이 당신을 위해 그것을 할 것입니다.

'파일 형식'설정에 대한 문서는 여기 에 있으며 vim 위키에는 줄 끝 변환에 대한 포괄적 인 페이지가 있습니다 .

또는 파일을 앞뒤로 많이 이동하는 경우 파일을 변환하는 것이 아니라 변환하기를 원할 수 :set ff=dos있으므로 vim은 파일이 DOS 파일임을 인식하고 줄 끝을 위해 DOS 규칙을 사용합니다.


보기에서 줄 끝을 변경합니다.

:e ++ff=dos
:e ++ff=mac
:e ++ff=unix

이것은 저장 작업으로도 사용할 수 있습니다 (: w만으로는 화면에 표시되는 줄 끝을 사용하여 저장하지 않습니다).

:w ++ff=dos
:w ++ff=mac
:w ++ff=unix

그리고 명령 줄에서 사용할 수 있습니다.

for file in *.cpp
do 
    vi +':w ++ff=unix' +':q' "$file"
done

나는 일반적으로

:%s/\r/\r/g

약간 이상해 보이지만 vim이 줄 바꿈과 일치하는 방식 때문에 작동합니다. 또한 기억하기가 더 쉽습니다. :)


다음 명령을 사용하는 것을 선호합니다.

:set fileformat=unix

당신은 또한 사용할 수 있습니다 mac또는 dos각각에 매킨토시 또는 MS-DOS / MS-Windows 파일 규칙에 파일을 변환합니다. 파일이 이미 올바른 형식이면 아무 작업도 수행하지 않습니다.

자세한 내용은 vim 도움말을 참조하십시오.

:help fileformat

:%s/\r+//g

Vim에서는 모든 캐리지 리턴을 제거하고 개행 만 남깁니다.


:set fileformat=unix dos에서 unix로 변환합니다.


출처 : http://vim.wikia.com/wiki/Change_end-of-line_format_for_dos-mac-unix

[Esc] : % s / \ r $ //


dos2unix 파일 내용을 직접 수정할 수 있습니다.

임시 파일 리디렉션없이 파일에서 직접 사용할 수 있습니다.

dos2unix input.txt input.txt

위는 가정 된 미국 키보드를 사용합니다. -영국 키보드를 사용하려면 437 옵션을 사용하십시오.

dos2unix -437 input.txt input.txt

Dos에서 Unix로 파일 디렉토리 변환

명령 줄과 sed를 사용하여 현재 디렉토리에서 확장명이 ".ext"인 모든 파일을 찾고 "^ M"을 모두 제거합니다.

@ https://gist.github.com/sparkida/7773170

find $(pwd) -type f -name "*.ext" | while read file; do sed -e 's/^M//g' -i "$file"; done;

또한 위에서 언급했듯이 ^ M = Ctrl+V+ Ctrl+M(캐럿 "^"기호와 M 만 입력하지 마십시오)


tr -d '\15\32' < winfile.txt > unixfile.txt

(참조 : http://kb.iu.edu/data/acux.html )


다음 단계는 dos의 파일 형식을 unix로 변환 할 수 있습니다.

:e ++ff=dos  Edit file again, using dos file format ('fileformats' is ignored).[A 1]
:setlocal ff=unix    This buffer will use LF-only line endings when written.[A 2]
:w   Write buffer using unix (LF-only) line endings.

참조 : http://vim.wikia.com/wiki/Change_end-of-line_format_for_dos-mac-unix


다음 명령을 사용합니다.

:%s/^M$//g 

Get the ^M to appear type CtrlV then CtrlM. CtrlV tells Vim to take the next character entered literally.


I found a very easy way: Open the file with nano: nano file.txt

Press CTRL+O to save, but before pressing Enter, press: ALT+D to toggle betwen DOS and Unix/Linux line-endings, or: ALT+M to toggle betwen Mac and Unix/Linux line-endings then press Enter to save and CTRL+X to quit.


:g/Ctrl-v Ctrl-m/s///

CtrlM is the character \r, or carriage return, which DOS line endings add. CtrlV tells vim to insert a literal CtrlM character at the command line.

Taken as a whole, this command replaces all \r with nothing, removing them from the ends of lines.


You can use:

vim somefile.txt +"%s/\r/\r/g" +wq

or dos2unix utility .


The comment about getting the ^M to appear is what worked for me. Merely typing "^M" in my vi got nothing (not found). The CTRL+V CTRL+M sequence did it perfectly though.

My working substitution command was

:%s/Ctrl-V Ctrl-M/\r/g

and it looked like this on my screen:

:%s/^M/\r/g

You can use the following command:
:%s/^V^M//g
where the '^' means use CTRL key.


below command is used for reformat all .sh file in current directory, I tested it on my Fedora OS.

for file in *.sh; do awk '{ sub("\r$", ""); print }' $file >luxubutmp; cp -f luxubutmp $file; rm -f luxubutmp ;done

Usually there is a dos2unix command you can use for this, just make sure you read the manual as the GNU and BSD versions differ on how they deal with the arguments.

BSD version:

dos2unix $FILENAME $FILENAME_OUT
mv $FILENAME_OUT $FILENAME

GNU version:

dos2unix $FILENAME

Alternatively, you can create your own dos2unix with any of the proposed answers here, for example:

function dos2unix(){
    [ "${!}" ] && [ -f "{$1}" ] || return 1;

    { echo ':set ff=unix';
      echo ':wq';
    } | vim "${1}";
}

I knew I'd seen this somewhere. Here is the FreeBSD login tip:

Need to remove all those ^M characters from a DOS file? Try

tr -d \\r < dosfile > newfile
    -- Originally by Dru <genesis@istar.ca>

In vim, type:

:w !dos2unix %

This will pipe the contents of your current buffer to the dos2unix command and write the results over the current contents. Vim will ask to reload the file after


To run directly into linux console: vim file.txt +"set ff=unix" +wq


Though this topic is very old, I'd like to put another stuff from wikia:

%s/\r\+$//g

that fill find all carriage return signs (one and more reps) up to the end of line and delete, so just \n will stay at eol.


This is my way. I opened a file in dos EOL and when I save the file that will automatically convert to unix EOL

autocmd BufWrite * :set ff=unix

if you create a file in NotePad or NotePad ++ in windows and bring it to Linux and open it by vim, you will see ^M at the end of each line. To remove this,

At your Linux terminal, type

dos2unix filename.ext

This will do the required magic.


I wanted newlines in place of the ^M's. Perl to the rescue:

perl -pi.bak -e 's/\x0d/\n/g' excel_created.txt

Or to write to stdout:

perl -p -e 's/\x0d/\n/g' < excel_created.txt

참고URL : https://stackoverflow.com/questions/82726/convert-dos-line-endings-to-linux-line-endings-in-vim

반응형