Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

Remove carriage return in Unix

What is the simplest way to remove all the carriage returns from a file in Unix?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm going to assume you mean carriage returns (CR, " ", 0x0d) at the ends of lines rather than just blindly within a file (you may have them in the middle of strings for all I know). Using this test file with a CR at the end of the first line only:

$ cat infile
hello
goodbye

$ cat infile | od -c
0000000   h   e   l   l   o  
  
   g   o   o   d   b   y   e  

0000017

dos2unix is the way to go if it's installed on your system:

$ cat infile | dos2unix -U | od -c
0000000   h   e   l   l   o  
   g   o   o   d   b   y   e  

0000016

If for some reason dos2unix is not available to you, then sed will do it:

$ cat infile | sed 's/
$//' | od -c
0000000   h   e   l   l   o  
   g   o   o   d   b   y   e  

0000016

If for some reason sed is not available to you, then ed will do it, in a complicated way:

$ echo ',s/
/
/
> w !cat
> Q' | ed infile 2>/dev/null | od -c
0000000   h   e   l   l   o  
   g   o   o   d   b   y   e  

0000016

If you don't have any of those tools installed on your box, you've got bigger problems than trying to convert files :-)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...