Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

File format conversion between DOS/Windows and Linux/Unix

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

DOS/Windows and Linux/Unix have different line feed formats. DOS/Windows-based text files have a CR (carriage return) and LF (line feed) at the end of each line, while UNIX text has only one line feed.

1) move the files under Dos/Windows to the Linux/Unix system

While many programs don't care about CR/LF text files in DOS/Windows format, a few do-the most famous is bash, which goes wrong whenever it encounters an enter. The following sed call converts text in DOS/Windows format to trusted UNIX format:

$sed-e's Compact. Hammer Grease 'mydos.txt > myunix.txt

The script works simply: the alternative regular expression matches the last character of a line, which happens to be a carriage return. We replace it with an empty character, thus removing it completely from the output. If you use this script and notice that the last character of each line in the output has been deleted, you specify a text file that is already in UNIX format. There's no need to do that!

2) move the Linux/UNIX text to the Windows system, and use the following script to perform the necessary format conversion:

$sed-e's myunix.txt myunix.txt > mydos.txt

In this script, the'$'regular expression matches the end of the line, while'\ r 'tells sed to insert a carriage return before it. Insert carriage returns before line breaks, and immediately, each line ends with CR/LF. Note that'\ r'is replaced with CR only if you are using GNU sed 3.02.80 or later.

3) using the dos2unix and unix2dos commands, this method is the easiest.

Create a random file Noname2.txt on window, with the following contents:

Sfadadfad

Sfasd

Fads

Fasdfads

Use the hexdump tool to view it on Linux:

A52 > hexdump Noname2.txt

0000000 6673 6461 6461 6166 0d64 730a 6166 6473

0000010 0a0d 6166 7364 0a0d 6166 6473 6166 7364

0000020 0a0d

0000022

After conversion with the dos2unix tool:

A52 > dos2unix Noname2.txt

Dos2unix: converting file Noname2.txt to UNIX format...

A52 > hexdump Noname2.txt

0000000 6673 6461 6461 6166 0a64 6673 7361 0a64

0000010 6166 7364 660a 7361 6664 6461 0a73

000001e

Then convert it back using unix2dos:

A52 > unix2dos Noname2.txt

Unix2dos: converting file Noname2.txt to DOS format...

A52 > hexdump Noname2.txt

0000000 6673 6461 6461 6166 0d64 730a 6166 6473

0000010 0a0d 6166 7364 0a0d 6166 6473 6166 7364

0000020 0a0d

0000022

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report