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

How to convert text files between Unix and DOS formats

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article will explain in detail how to convert text files between Unix and DOS format. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

As a Linux administrator, you may have noticed that some developers have requested that files be converted from DOS format to Unix format, and vice versa.

This is because these files are created on the Windows system and copied to the Linux system for some reason.

This is fine in itself, but some applications on Linux systems may not understand these new line breaks, so you need to convert them before using them.

DOS text files have carriage return (CR or\ r) and newline (LF or\ n) pairs of characters as their newline characters, while Unix text has only newline (LF) characters.

There are many ways to convert DOS text files to Unix format.

However, I recommend using a special tool called dos2unix / unix2dos to convert text between DOS and Unix formats.

Dos2unix: converts a text file from DOS format to Unix format.

Unix2dos: converts a text file from Unix format to DOS format.

Tr, awk, and sed commands: these can be used for the same purpose.

Using the od (octal dump octal dump) command, you can easily tell whether the file is in DOS or Unix format, as shown in the following figure:

# od-bc windows.txt0000000 125 156 151 170 040 151 163 040 141 141 040 162 145 145 040 157U n i x i s a f r e e o0000020 160 145 156 163 165 162 143 145 040 157 165 162 141 164p e n s o u r c e o p e r a t0000040 151,147,040 161,165,155,0123165i n g s y s t e m\ r\ n S u p e0000060162 040145165165166166040162 1414040r c o m p u t e r S a r e0000100 162 165,155,15157,147,040,157,157,040,125,111,015R u n n i n g o n U N I X\ r0000120,071,065,045,040,157,145,145,145,144,144,145,145,14014040\ n 9% o f w e b s i t e s000014014040141 162,142,145,040A r e r u n n i n g o n0000160 114151515170401173150401173015012 101 171150151L i n u x O S\ r\ n A n Y t h i0000200 156 147 040 143 141 156 040 142 145 040 144 157 156 145 040 157n g c a n b e d o n e o00220 156 040 114 151 156 165 170 015 012n L i n u x\ r\ n0000231

The above output clearly indicates that this is a file in DOS format because it contains the escape sequence\ r\ n.

At the same time, when you print the file output on the terminal, you will get the following output:

# cat windows.txtUnix is a free opensource operating systemSuper computers are running on UNIX95% of websites are running on Linux OSAnything can be done on Linux how to install dos2unix on Linux?

Dos2unix can be easily installed from the official repository of the distribution.

For the RHEL/CentOS 6 yum 7 system, use the install command to install dos2unix.

$sudo yum install-y dos2unix

For RHEL/CentOS 8 and Fedora systems, use the dnf command to install dos2unix.

$sudo yum install-y dos2unix

For Debian-based systems, use the apt command or the apt-get command to install dos2unix.

$sudo apt-get update$ sudo apt-get install dos2unix

For openSUSE systems, use the zypper command to install dos2unix.

$sudo zypper install-y dos2unix1) how do I convert a DOS file to UNIX format?

The following command converts the windows.txt file from DOS to Unix format.

The modification to the file is to delete each line of the file.

# dos2unix windows.txtdos2unix: converting file windows.txt to Unix format... # cat windows.txt0000000 125 156 151 170 040 151 163 040 141 141 040 162 145 145 040 157U n i x i s a f r e e o0000020 160 145 156 163 157 165 162 143 145 164p e n s o u r c e o p e r a t0000040 151 156 147 040 163 171,163,161,15512123165 165162i n g s y s t e m\ n S u p e r0000060040 14715516516616640162 145040 1414040 162c o m p u t e r s a r e R0000100 165,155,155,157,040,157,040,125,111,13012071u n n i n g o n U N I X\ n 90000120 065,045,040157,146,040,167,142,144,165,040 1415% o f w e b s i t e s a0000140162,040165,15157,147,0401515147401514151r e r u n n i n g o n L i001601651651704011616147147040n u x O S\ n A n y t h i n g002001414141414141151c 040 142 145 040 144 157 156 145 040 157 156 040 114c a n b e d o n e o n L0000220 151 156 165 170 012i n u x\ n0000225

The above command will overwrite the original file.

If you want to keep the original file, please use the following command. This will save the converted output as a new file.

# dos2unix-n windows.txt unix.txtdos2unix: converting file windows.txt to file unix.txt in Unix format... 1a) how to use the tr command to convert DOS files to UNIX format.

As discussed at the beginning of the article, you can use the tr command to convert the DOS file to Unix format as shown below.

Syntax: tr-d'\ r'

< source_file >

Output_file

The following tr command converts the file windows.txt in DOS format to the file unix.txt in Unix format.

# tr-d'\ r'

< windows.txt >

Unix.txt

Note: you cannot use the tr command to convert files from Unix format to Windows (DOS).

1B) how to use the awk command to convert DOS files to UNIX format.

Use the following awk command format to convert the DOS file to Unix format.

Syntax: awk'{sub ("\ r $", "); print} 'source_file.txt > output_file.txt

The following awk command converts the DOS file windows.txt to the Unix format file unix.txt.

# awk'{sub ("\ r $", "); print} 'windows.txt > unix.txt2) how to convert a UNIX file to DOS format?

When you convert a file from UNIX to DOS format, it adds a carriage return (CR or\ r) to each line.

# unix2dos unix.txtunix2dos: converting file unix.txt to DOS format...

This command retains the original file.

# unix2dos-n unix.txt windows.txtunix2dos: converting file unix.txt to file windows.txt in DOS format... 2a) how to use the awk command to convert UNIX files to DOS format?

Use the following awk command format to convert the UNIX file to DOS format.

Syntax: awk 'sub ("$", "\ r")' source_file.txt > output_file.txt

The following awk command converts the unix.txt file to the DOS format file windows.txt.

# awk 'sub ("$", "\ r")' unix.txt > windows.txt on "how to convert text files between Unix and DOS format" this article shares here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

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