Convert CRLF's to line feeds on Linux
What's the best way to convert CRLF's to line feeds in files on Linux?
I've seen sed commands, but is there anything simpler?
linux carriage-return
add a comment |
What's the best way to convert CRLF's to line feeds in files on Linux?
I've seen sed commands, but is there anything simpler?
linux carriage-return
4
Dupe: superuser.com/questions/38744/…. The link provided in the accepted answer covers the dos2unix, perl and vi options among others.
– nagul
Oct 7 '09 at 9:13
2
This already has better answers though (so if one of these is to be closed, it should probably be that one)
– Jonik
Oct 7 '09 at 12:02
add a comment |
What's the best way to convert CRLF's to line feeds in files on Linux?
I've seen sed commands, but is there anything simpler?
linux carriage-return
What's the best way to convert CRLF's to line feeds in files on Linux?
I've seen sed commands, but is there anything simpler?
linux carriage-return
linux carriage-return
edited Apr 4 '17 at 12:49
karel
9,28093239
9,28093239
asked Oct 7 '09 at 5:06
JoelFanJoelFan
1,697103647
1,697103647
4
Dupe: superuser.com/questions/38744/…. The link provided in the accepted answer covers the dos2unix, perl and vi options among others.
– nagul
Oct 7 '09 at 9:13
2
This already has better answers though (so if one of these is to be closed, it should probably be that one)
– Jonik
Oct 7 '09 at 12:02
add a comment |
4
Dupe: superuser.com/questions/38744/…. The link provided in the accepted answer covers the dos2unix, perl and vi options among others.
– nagul
Oct 7 '09 at 9:13
2
This already has better answers though (so if one of these is to be closed, it should probably be that one)
– Jonik
Oct 7 '09 at 12:02
4
4
Dupe: superuser.com/questions/38744/…. The link provided in the accepted answer covers the dos2unix, perl and vi options among others.
– nagul
Oct 7 '09 at 9:13
Dupe: superuser.com/questions/38744/…. The link provided in the accepted answer covers the dos2unix, perl and vi options among others.
– nagul
Oct 7 '09 at 9:13
2
2
This already has better answers though (so if one of these is to be closed, it should probably be that one)
– Jonik
Oct 7 '09 at 12:02
This already has better answers though (so if one of these is to be closed, it should probably be that one)
– Jonik
Oct 7 '09 at 12:02
add a comment |
11 Answers
11
active
oldest
votes
Use this command:
fromdos yourtextfile
The other way around:
todos yourtextfile
These commands are found in the tofrodos package (on most recent distributions), which also provides the two wrappers unix2dos and dos2unix that mimic the old unix tools of the same name.
2
+1 Much more useful than the currently top-voted "Use dos2unix" answer.
– Jonik
Oct 7 '09 at 10:00
1
Yeah, even I'm voting this one up. Mine was more of a drive-by suggestion.
– Ryan Thompson
Oct 8 '09 at 7:26
I would give extra bonus if you say how to make it recursive. Currently works only with wildcards.
– sorin
Jul 3 '10 at 7:47
2
@SorinSbarnea: something likefind . -name '*.txt' -print0 | xargs -null fromdos
– bstpierre
Jul 25 '12 at 18:49
@Jonik what makes it "Much more useful"? Serious question
– andrewtweber
Jan 31 '15 at 22:43
|
show 1 more comment
Use dos2unix
.
dos2unix - DOS/MAC to UNIX text file format converter
dos2unix [options] [-c convmode] [-o file ...] [-n infile outfile ...]
Options:
[-hkqV] [--help] [--keepdate] [--quiet] [--version]
2
and unix2dos for the other way 'round.
– quack quixote
Oct 7 '09 at 5:41
Quack, are you following me? Not that I don't appreciate it, with all the upvotes.
– Ryan Thompson
Oct 7 '09 at 6:19
1
dude, i'm ~quack. pronounce "~" as "not". :) but no, not following you, tho i do appear to run into you frequently.
– quack quixote
Oct 7 '09 at 7:55
1
Consider elaborating on how to get this utility for your Linux system. At least on Ubuntu it's not installed by default (but by installing tofrodos package you get something very similar: packages.ubuntu.com/jaunty/tofrodos).
– Jonik
Oct 7 '09 at 9:33
add a comment |
I prefer perl:
perl -lne 's/r//g; print' winfile.txt > unixfile.txt
But that's well-suited to my uses, and it's very easy for me to remember. Not all systems have a dos2unix command, but most that I work on have a perl interpreter.
Another is recode, a powerful replacement for dos2unix and iconv; it's available in the "recode" package in Debian repositories:
recode ibmpc..lat1 winfile.txt # dos2unix
recode lat1..ibmpc unixfile.txt # unix2dos
For awk fans:
awk '{ sub("r$", ""); print }' winfile.txt > unixfile.txt
...and sed:
sed 's/r$//' winfile.txt > unixfile.txt
And now, only slightly-less-convoluted than deleting the CR's by hand in a hex editor, straight from one of our stackoverflow.com friends, useable with the beef interpreter (located on your friendly neighborhood Debian repository),
dos2unix in brainfuck!
,[[->+>+<<]>>>,[<-------------[+++++++++++++.>>>]<[>>----------[>+++++++++++++.-------------]<++++++++++>]<<<<[-]>>>[-<<<+>>>]]<[-]<[-]<]++++++++++.
big thanks to jk for wasting an hour of his life to write this!
1
(useless use of cat and) perl is as complicated as sed... thus you are not really answering the question but rather collecting reputation :)
– akira
Oct 7 '09 at 6:28
2
"best way" is subjective. this works best for me (i'm tons more comfortable with perl than sed). i didn't promise it would work best for you.
– quack quixote
Oct 7 '09 at 6:32
@akira: a question can have multiple valid answers. I use this method as well, occasionally, mostly in combination with other changes, so it is definitely a valid answer; but "use dos2unix" is definitely the more practical answer in most situations. So I think the ratings are fine.
– reinierpost
Oct 7 '09 at 9:47
@akira: if you find it simpler, please post it as an answer and enlighten the rest of us.
– quack quixote
Oct 7 '09 at 10:31
@~quack: that is the point: it is not simpler. thats the same for your perl answer. u2d or fromdos/todos are the right answers because they are simpler than any stuff expressed in any other programming language.
– akira
Oct 7 '09 at 11:22
|
show 6 more comments
I do this on Bash:
cat cr_stuffed.file | tr -d r > no_more_crs.file
nice. i saw another mention of tr earlier today. it's not a program that gets mentioned very often is it?
– quack quixote
Oct 7 '09 at 23:46
add a comment |
I think you can use tr
, as well (though I have no funny format files on which to try):
tr -d 'r' < file1 > file2
add a comment |
In vi or Vim:
:%s/^V^M//g
add a comment |
I found a very easy way… Open 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.
1
Could you edit your answer to clarify which toggle settings will replicate the behaviour requested by the OP?
– Burgi
May 1 '16 at 1:52
The OP wants to toggle off DOS line endings, soAlt+d
. Sometimes alt gets intercepted by the terminal program, so you can useesc+d
instead.
– spinup
Aug 25 '16 at 14:56
1
Lots of nano shortcuts also work with Shift pressed, which often prevents terminal interception, so 'Alt-Shift-D' works too.
– mwfearnley
Mar 2 '17 at 10:45
add a comment |
I prefer Vim and :set fileformat=unix
. While not the fastest, it does give me a preview. It is especially useful in the case of a file with mixed endings.
add a comment |
If you want a GUI method, try the Kate text editor (other advanced text editors may be able to handle this too). Open the find / Replace dialog (Ctrl+R), and replace rn
with n
. (NB: you'll need to choose "Regular expression" from the drop down and deselect "Selection only" from the options.)
EDIT: Or, if you simply want to convert to Unix format, then use the menu option Tools
> End of Line
> Unix
.
There are text editors, such as jEdit, that can do these transformations automatically - you just tell it if you want Unix, Windows or Mac line separators.
– Jonik
Oct 7 '09 at 10:24
Actually, KATE can do that too through the Tools > End of Line menu. Maybe I should have thought more laterally than answering the question exactly as it was worded - but if you know you specifically want to convertrn
ton
then using search/replace is easier than remembering which OS uses which line ending. ;)
– DisgruntledGoat
Oct 10 '09 at 23:22
add a comment |
Paste this into dos2unix.py Python script.
#!/usr/bin/env python
"""
convert dos linefeeds (crlf) to unix (lf)
usage: dos2unix.py <input> <output>
"""
import sys
if len(sys.argv[1:]) != 2:
sys.exit(__doc__)
content = ''
outsize = 0
with open(sys.argv[1], 'rb') as infile:
content = infile.read()
with open(sys.argv[2], 'wb') as output:
for line in content.splitlines():
outsize += len(line) + 1
output.write(line + 'n')
print("Done. Saved %s bytes." % (len(content)-outsize))
Should work on any platform with Python installed. Public domain.
add a comment |
CR LF
to LF
using awk:
awk -v RS='r?n' 1
command | awk -v RS='r?n' 1
awk -v RS='r?n' 1 filename
Usage example:
echo -e 'foonbarrnbaz' | awk -v RS='r?n' 1 | hexdump -C
Explanation:
-v RS='r?n'
sets variable RS (input record separator) to r?n
, meaning input is read line by line separated by LF (n
) which may (?
) be preceded by CR (r
).
1
is the script awk executes. A script consists of condition { action }
. In this case, 1
is the condition which evaluates to true. The action is omitted, so the default action is executed, which means print the current line (which could also be written as {print $0}
or simply {print}
).
LF
to CR LF
: You can set the variable ORS
(output record separator) to modify the line ends of the output. Example:
echo -e 'foonbarrnbaz' | awk -v RS='r?n' -v ORS='rn' 1 | hexdump -C
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f52044%2fconvert-crlfs-to-line-feeds-on-linux%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
11 Answers
11
active
oldest
votes
11 Answers
11
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use this command:
fromdos yourtextfile
The other way around:
todos yourtextfile
These commands are found in the tofrodos package (on most recent distributions), which also provides the two wrappers unix2dos and dos2unix that mimic the old unix tools of the same name.
2
+1 Much more useful than the currently top-voted "Use dos2unix" answer.
– Jonik
Oct 7 '09 at 10:00
1
Yeah, even I'm voting this one up. Mine was more of a drive-by suggestion.
– Ryan Thompson
Oct 8 '09 at 7:26
I would give extra bonus if you say how to make it recursive. Currently works only with wildcards.
– sorin
Jul 3 '10 at 7:47
2
@SorinSbarnea: something likefind . -name '*.txt' -print0 | xargs -null fromdos
– bstpierre
Jul 25 '12 at 18:49
@Jonik what makes it "Much more useful"? Serious question
– andrewtweber
Jan 31 '15 at 22:43
|
show 1 more comment
Use this command:
fromdos yourtextfile
The other way around:
todos yourtextfile
These commands are found in the tofrodos package (on most recent distributions), which also provides the two wrappers unix2dos and dos2unix that mimic the old unix tools of the same name.
2
+1 Much more useful than the currently top-voted "Use dos2unix" answer.
– Jonik
Oct 7 '09 at 10:00
1
Yeah, even I'm voting this one up. Mine was more of a drive-by suggestion.
– Ryan Thompson
Oct 8 '09 at 7:26
I would give extra bonus if you say how to make it recursive. Currently works only with wildcards.
– sorin
Jul 3 '10 at 7:47
2
@SorinSbarnea: something likefind . -name '*.txt' -print0 | xargs -null fromdos
– bstpierre
Jul 25 '12 at 18:49
@Jonik what makes it "Much more useful"? Serious question
– andrewtweber
Jan 31 '15 at 22:43
|
show 1 more comment
Use this command:
fromdos yourtextfile
The other way around:
todos yourtextfile
These commands are found in the tofrodos package (on most recent distributions), which also provides the two wrappers unix2dos and dos2unix that mimic the old unix tools of the same name.
Use this command:
fromdos yourtextfile
The other way around:
todos yourtextfile
These commands are found in the tofrodos package (on most recent distributions), which also provides the two wrappers unix2dos and dos2unix that mimic the old unix tools of the same name.
edited Oct 7 '09 at 9:31
answered Oct 7 '09 at 9:22
avelldirollavelldiroll
1,9531115
1,9531115
2
+1 Much more useful than the currently top-voted "Use dos2unix" answer.
– Jonik
Oct 7 '09 at 10:00
1
Yeah, even I'm voting this one up. Mine was more of a drive-by suggestion.
– Ryan Thompson
Oct 8 '09 at 7:26
I would give extra bonus if you say how to make it recursive. Currently works only with wildcards.
– sorin
Jul 3 '10 at 7:47
2
@SorinSbarnea: something likefind . -name '*.txt' -print0 | xargs -null fromdos
– bstpierre
Jul 25 '12 at 18:49
@Jonik what makes it "Much more useful"? Serious question
– andrewtweber
Jan 31 '15 at 22:43
|
show 1 more comment
2
+1 Much more useful than the currently top-voted "Use dos2unix" answer.
– Jonik
Oct 7 '09 at 10:00
1
Yeah, even I'm voting this one up. Mine was more of a drive-by suggestion.
– Ryan Thompson
Oct 8 '09 at 7:26
I would give extra bonus if you say how to make it recursive. Currently works only with wildcards.
– sorin
Jul 3 '10 at 7:47
2
@SorinSbarnea: something likefind . -name '*.txt' -print0 | xargs -null fromdos
– bstpierre
Jul 25 '12 at 18:49
@Jonik what makes it "Much more useful"? Serious question
– andrewtweber
Jan 31 '15 at 22:43
2
2
+1 Much more useful than the currently top-voted "Use dos2unix" answer.
– Jonik
Oct 7 '09 at 10:00
+1 Much more useful than the currently top-voted "Use dos2unix" answer.
– Jonik
Oct 7 '09 at 10:00
1
1
Yeah, even I'm voting this one up. Mine was more of a drive-by suggestion.
– Ryan Thompson
Oct 8 '09 at 7:26
Yeah, even I'm voting this one up. Mine was more of a drive-by suggestion.
– Ryan Thompson
Oct 8 '09 at 7:26
I would give extra bonus if you say how to make it recursive. Currently works only with wildcards.
– sorin
Jul 3 '10 at 7:47
I would give extra bonus if you say how to make it recursive. Currently works only with wildcards.
– sorin
Jul 3 '10 at 7:47
2
2
@SorinSbarnea: something like
find . -name '*.txt' -print0 | xargs -null fromdos
– bstpierre
Jul 25 '12 at 18:49
@SorinSbarnea: something like
find . -name '*.txt' -print0 | xargs -null fromdos
– bstpierre
Jul 25 '12 at 18:49
@Jonik what makes it "Much more useful"? Serious question
– andrewtweber
Jan 31 '15 at 22:43
@Jonik what makes it "Much more useful"? Serious question
– andrewtweber
Jan 31 '15 at 22:43
|
show 1 more comment
Use dos2unix
.
dos2unix - DOS/MAC to UNIX text file format converter
dos2unix [options] [-c convmode] [-o file ...] [-n infile outfile ...]
Options:
[-hkqV] [--help] [--keepdate] [--quiet] [--version]
2
and unix2dos for the other way 'round.
– quack quixote
Oct 7 '09 at 5:41
Quack, are you following me? Not that I don't appreciate it, with all the upvotes.
– Ryan Thompson
Oct 7 '09 at 6:19
1
dude, i'm ~quack. pronounce "~" as "not". :) but no, not following you, tho i do appear to run into you frequently.
– quack quixote
Oct 7 '09 at 7:55
1
Consider elaborating on how to get this utility for your Linux system. At least on Ubuntu it's not installed by default (but by installing tofrodos package you get something very similar: packages.ubuntu.com/jaunty/tofrodos).
– Jonik
Oct 7 '09 at 9:33
add a comment |
Use dos2unix
.
dos2unix - DOS/MAC to UNIX text file format converter
dos2unix [options] [-c convmode] [-o file ...] [-n infile outfile ...]
Options:
[-hkqV] [--help] [--keepdate] [--quiet] [--version]
2
and unix2dos for the other way 'round.
– quack quixote
Oct 7 '09 at 5:41
Quack, are you following me? Not that I don't appreciate it, with all the upvotes.
– Ryan Thompson
Oct 7 '09 at 6:19
1
dude, i'm ~quack. pronounce "~" as "not". :) but no, not following you, tho i do appear to run into you frequently.
– quack quixote
Oct 7 '09 at 7:55
1
Consider elaborating on how to get this utility for your Linux system. At least on Ubuntu it's not installed by default (but by installing tofrodos package you get something very similar: packages.ubuntu.com/jaunty/tofrodos).
– Jonik
Oct 7 '09 at 9:33
add a comment |
Use dos2unix
.
dos2unix - DOS/MAC to UNIX text file format converter
dos2unix [options] [-c convmode] [-o file ...] [-n infile outfile ...]
Options:
[-hkqV] [--help] [--keepdate] [--quiet] [--version]
Use dos2unix
.
dos2unix - DOS/MAC to UNIX text file format converter
dos2unix [options] [-c convmode] [-o file ...] [-n infile outfile ...]
Options:
[-hkqV] [--help] [--keepdate] [--quiet] [--version]
edited Apr 29 '12 at 14:17
Tom Wijsman
50.4k24164247
50.4k24164247
answered Oct 7 '09 at 5:17
Ryan ThompsonRyan Thompson
6,87883860
6,87883860
2
and unix2dos for the other way 'round.
– quack quixote
Oct 7 '09 at 5:41
Quack, are you following me? Not that I don't appreciate it, with all the upvotes.
– Ryan Thompson
Oct 7 '09 at 6:19
1
dude, i'm ~quack. pronounce "~" as "not". :) but no, not following you, tho i do appear to run into you frequently.
– quack quixote
Oct 7 '09 at 7:55
1
Consider elaborating on how to get this utility for your Linux system. At least on Ubuntu it's not installed by default (but by installing tofrodos package you get something very similar: packages.ubuntu.com/jaunty/tofrodos).
– Jonik
Oct 7 '09 at 9:33
add a comment |
2
and unix2dos for the other way 'round.
– quack quixote
Oct 7 '09 at 5:41
Quack, are you following me? Not that I don't appreciate it, with all the upvotes.
– Ryan Thompson
Oct 7 '09 at 6:19
1
dude, i'm ~quack. pronounce "~" as "not". :) but no, not following you, tho i do appear to run into you frequently.
– quack quixote
Oct 7 '09 at 7:55
1
Consider elaborating on how to get this utility for your Linux system. At least on Ubuntu it's not installed by default (but by installing tofrodos package you get something very similar: packages.ubuntu.com/jaunty/tofrodos).
– Jonik
Oct 7 '09 at 9:33
2
2
and unix2dos for the other way 'round.
– quack quixote
Oct 7 '09 at 5:41
and unix2dos for the other way 'round.
– quack quixote
Oct 7 '09 at 5:41
Quack, are you following me? Not that I don't appreciate it, with all the upvotes.
– Ryan Thompson
Oct 7 '09 at 6:19
Quack, are you following me? Not that I don't appreciate it, with all the upvotes.
– Ryan Thompson
Oct 7 '09 at 6:19
1
1
dude, i'm ~quack. pronounce "~" as "not". :) but no, not following you, tho i do appear to run into you frequently.
– quack quixote
Oct 7 '09 at 7:55
dude, i'm ~quack. pronounce "~" as "not". :) but no, not following you, tho i do appear to run into you frequently.
– quack quixote
Oct 7 '09 at 7:55
1
1
Consider elaborating on how to get this utility for your Linux system. At least on Ubuntu it's not installed by default (but by installing tofrodos package you get something very similar: packages.ubuntu.com/jaunty/tofrodos).
– Jonik
Oct 7 '09 at 9:33
Consider elaborating on how to get this utility for your Linux system. At least on Ubuntu it's not installed by default (but by installing tofrodos package you get something very similar: packages.ubuntu.com/jaunty/tofrodos).
– Jonik
Oct 7 '09 at 9:33
add a comment |
I prefer perl:
perl -lne 's/r//g; print' winfile.txt > unixfile.txt
But that's well-suited to my uses, and it's very easy for me to remember. Not all systems have a dos2unix command, but most that I work on have a perl interpreter.
Another is recode, a powerful replacement for dos2unix and iconv; it's available in the "recode" package in Debian repositories:
recode ibmpc..lat1 winfile.txt # dos2unix
recode lat1..ibmpc unixfile.txt # unix2dos
For awk fans:
awk '{ sub("r$", ""); print }' winfile.txt > unixfile.txt
...and sed:
sed 's/r$//' winfile.txt > unixfile.txt
And now, only slightly-less-convoluted than deleting the CR's by hand in a hex editor, straight from one of our stackoverflow.com friends, useable with the beef interpreter (located on your friendly neighborhood Debian repository),
dos2unix in brainfuck!
,[[->+>+<<]>>>,[<-------------[+++++++++++++.>>>]<[>>----------[>+++++++++++++.-------------]<++++++++++>]<<<<[-]>>>[-<<<+>>>]]<[-]<[-]<]++++++++++.
big thanks to jk for wasting an hour of his life to write this!
1
(useless use of cat and) perl is as complicated as sed... thus you are not really answering the question but rather collecting reputation :)
– akira
Oct 7 '09 at 6:28
2
"best way" is subjective. this works best for me (i'm tons more comfortable with perl than sed). i didn't promise it would work best for you.
– quack quixote
Oct 7 '09 at 6:32
@akira: a question can have multiple valid answers. I use this method as well, occasionally, mostly in combination with other changes, so it is definitely a valid answer; but "use dos2unix" is definitely the more practical answer in most situations. So I think the ratings are fine.
– reinierpost
Oct 7 '09 at 9:47
@akira: if you find it simpler, please post it as an answer and enlighten the rest of us.
– quack quixote
Oct 7 '09 at 10:31
@~quack: that is the point: it is not simpler. thats the same for your perl answer. u2d or fromdos/todos are the right answers because they are simpler than any stuff expressed in any other programming language.
– akira
Oct 7 '09 at 11:22
|
show 6 more comments
I prefer perl:
perl -lne 's/r//g; print' winfile.txt > unixfile.txt
But that's well-suited to my uses, and it's very easy for me to remember. Not all systems have a dos2unix command, but most that I work on have a perl interpreter.
Another is recode, a powerful replacement for dos2unix and iconv; it's available in the "recode" package in Debian repositories:
recode ibmpc..lat1 winfile.txt # dos2unix
recode lat1..ibmpc unixfile.txt # unix2dos
For awk fans:
awk '{ sub("r$", ""); print }' winfile.txt > unixfile.txt
...and sed:
sed 's/r$//' winfile.txt > unixfile.txt
And now, only slightly-less-convoluted than deleting the CR's by hand in a hex editor, straight from one of our stackoverflow.com friends, useable with the beef interpreter (located on your friendly neighborhood Debian repository),
dos2unix in brainfuck!
,[[->+>+<<]>>>,[<-------------[+++++++++++++.>>>]<[>>----------[>+++++++++++++.-------------]<++++++++++>]<<<<[-]>>>[-<<<+>>>]]<[-]<[-]<]++++++++++.
big thanks to jk for wasting an hour of his life to write this!
1
(useless use of cat and) perl is as complicated as sed... thus you are not really answering the question but rather collecting reputation :)
– akira
Oct 7 '09 at 6:28
2
"best way" is subjective. this works best for me (i'm tons more comfortable with perl than sed). i didn't promise it would work best for you.
– quack quixote
Oct 7 '09 at 6:32
@akira: a question can have multiple valid answers. I use this method as well, occasionally, mostly in combination with other changes, so it is definitely a valid answer; but "use dos2unix" is definitely the more practical answer in most situations. So I think the ratings are fine.
– reinierpost
Oct 7 '09 at 9:47
@akira: if you find it simpler, please post it as an answer and enlighten the rest of us.
– quack quixote
Oct 7 '09 at 10:31
@~quack: that is the point: it is not simpler. thats the same for your perl answer. u2d or fromdos/todos are the right answers because they are simpler than any stuff expressed in any other programming language.
– akira
Oct 7 '09 at 11:22
|
show 6 more comments
I prefer perl:
perl -lne 's/r//g; print' winfile.txt > unixfile.txt
But that's well-suited to my uses, and it's very easy for me to remember. Not all systems have a dos2unix command, but most that I work on have a perl interpreter.
Another is recode, a powerful replacement for dos2unix and iconv; it's available in the "recode" package in Debian repositories:
recode ibmpc..lat1 winfile.txt # dos2unix
recode lat1..ibmpc unixfile.txt # unix2dos
For awk fans:
awk '{ sub("r$", ""); print }' winfile.txt > unixfile.txt
...and sed:
sed 's/r$//' winfile.txt > unixfile.txt
And now, only slightly-less-convoluted than deleting the CR's by hand in a hex editor, straight from one of our stackoverflow.com friends, useable with the beef interpreter (located on your friendly neighborhood Debian repository),
dos2unix in brainfuck!
,[[->+>+<<]>>>,[<-------------[+++++++++++++.>>>]<[>>----------[>+++++++++++++.-------------]<++++++++++>]<<<<[-]>>>[-<<<+>>>]]<[-]<[-]<]++++++++++.
big thanks to jk for wasting an hour of his life to write this!
I prefer perl:
perl -lne 's/r//g; print' winfile.txt > unixfile.txt
But that's well-suited to my uses, and it's very easy for me to remember. Not all systems have a dos2unix command, but most that I work on have a perl interpreter.
Another is recode, a powerful replacement for dos2unix and iconv; it's available in the "recode" package in Debian repositories:
recode ibmpc..lat1 winfile.txt # dos2unix
recode lat1..ibmpc unixfile.txt # unix2dos
For awk fans:
awk '{ sub("r$", ""); print }' winfile.txt > unixfile.txt
...and sed:
sed 's/r$//' winfile.txt > unixfile.txt
And now, only slightly-less-convoluted than deleting the CR's by hand in a hex editor, straight from one of our stackoverflow.com friends, useable with the beef interpreter (located on your friendly neighborhood Debian repository),
dos2unix in brainfuck!
,[[->+>+<<]>>>,[<-------------[+++++++++++++.>>>]<[>>----------[>+++++++++++++.-------------]<++++++++++>]<<<<[-]>>>[-<<<+>>>]]<[-]<[-]<]++++++++++.
big thanks to jk for wasting an hour of his life to write this!
edited May 23 '17 at 12:41
Community♦
1
1
answered Oct 7 '09 at 5:44
quack quixotequack quixote
35.3k1087119
35.3k1087119
1
(useless use of cat and) perl is as complicated as sed... thus you are not really answering the question but rather collecting reputation :)
– akira
Oct 7 '09 at 6:28
2
"best way" is subjective. this works best for me (i'm tons more comfortable with perl than sed). i didn't promise it would work best for you.
– quack quixote
Oct 7 '09 at 6:32
@akira: a question can have multiple valid answers. I use this method as well, occasionally, mostly in combination with other changes, so it is definitely a valid answer; but "use dos2unix" is definitely the more practical answer in most situations. So I think the ratings are fine.
– reinierpost
Oct 7 '09 at 9:47
@akira: if you find it simpler, please post it as an answer and enlighten the rest of us.
– quack quixote
Oct 7 '09 at 10:31
@~quack: that is the point: it is not simpler. thats the same for your perl answer. u2d or fromdos/todos are the right answers because they are simpler than any stuff expressed in any other programming language.
– akira
Oct 7 '09 at 11:22
|
show 6 more comments
1
(useless use of cat and) perl is as complicated as sed... thus you are not really answering the question but rather collecting reputation :)
– akira
Oct 7 '09 at 6:28
2
"best way" is subjective. this works best for me (i'm tons more comfortable with perl than sed). i didn't promise it would work best for you.
– quack quixote
Oct 7 '09 at 6:32
@akira: a question can have multiple valid answers. I use this method as well, occasionally, mostly in combination with other changes, so it is definitely a valid answer; but "use dos2unix" is definitely the more practical answer in most situations. So I think the ratings are fine.
– reinierpost
Oct 7 '09 at 9:47
@akira: if you find it simpler, please post it as an answer and enlighten the rest of us.
– quack quixote
Oct 7 '09 at 10:31
@~quack: that is the point: it is not simpler. thats the same for your perl answer. u2d or fromdos/todos are the right answers because they are simpler than any stuff expressed in any other programming language.
– akira
Oct 7 '09 at 11:22
1
1
(useless use of cat and) perl is as complicated as sed... thus you are not really answering the question but rather collecting reputation :)
– akira
Oct 7 '09 at 6:28
(useless use of cat and) perl is as complicated as sed... thus you are not really answering the question but rather collecting reputation :)
– akira
Oct 7 '09 at 6:28
2
2
"best way" is subjective. this works best for me (i'm tons more comfortable with perl than sed). i didn't promise it would work best for you.
– quack quixote
Oct 7 '09 at 6:32
"best way" is subjective. this works best for me (i'm tons more comfortable with perl than sed). i didn't promise it would work best for you.
– quack quixote
Oct 7 '09 at 6:32
@akira: a question can have multiple valid answers. I use this method as well, occasionally, mostly in combination with other changes, so it is definitely a valid answer; but "use dos2unix" is definitely the more practical answer in most situations. So I think the ratings are fine.
– reinierpost
Oct 7 '09 at 9:47
@akira: a question can have multiple valid answers. I use this method as well, occasionally, mostly in combination with other changes, so it is definitely a valid answer; but "use dos2unix" is definitely the more practical answer in most situations. So I think the ratings are fine.
– reinierpost
Oct 7 '09 at 9:47
@akira: if you find it simpler, please post it as an answer and enlighten the rest of us.
– quack quixote
Oct 7 '09 at 10:31
@akira: if you find it simpler, please post it as an answer and enlighten the rest of us.
– quack quixote
Oct 7 '09 at 10:31
@~quack: that is the point: it is not simpler. thats the same for your perl answer. u2d or fromdos/todos are the right answers because they are simpler than any stuff expressed in any other programming language.
– akira
Oct 7 '09 at 11:22
@~quack: that is the point: it is not simpler. thats the same for your perl answer. u2d or fromdos/todos are the right answers because they are simpler than any stuff expressed in any other programming language.
– akira
Oct 7 '09 at 11:22
|
show 6 more comments
I do this on Bash:
cat cr_stuffed.file | tr -d r > no_more_crs.file
nice. i saw another mention of tr earlier today. it's not a program that gets mentioned very often is it?
– quack quixote
Oct 7 '09 at 23:46
add a comment |
I do this on Bash:
cat cr_stuffed.file | tr -d r > no_more_crs.file
nice. i saw another mention of tr earlier today. it's not a program that gets mentioned very often is it?
– quack quixote
Oct 7 '09 at 23:46
add a comment |
I do this on Bash:
cat cr_stuffed.file | tr -d r > no_more_crs.file
I do this on Bash:
cat cr_stuffed.file | tr -d r > no_more_crs.file
edited Mar 12 '17 at 5:03
Ben Voigt
5,53613055
5,53613055
answered Oct 7 '09 at 23:31
JustJeffJustJeff
3401522
3401522
nice. i saw another mention of tr earlier today. it's not a program that gets mentioned very often is it?
– quack quixote
Oct 7 '09 at 23:46
add a comment |
nice. i saw another mention of tr earlier today. it's not a program that gets mentioned very often is it?
– quack quixote
Oct 7 '09 at 23:46
nice. i saw another mention of tr earlier today. it's not a program that gets mentioned very often is it?
– quack quixote
Oct 7 '09 at 23:46
nice. i saw another mention of tr earlier today. it's not a program that gets mentioned very often is it?
– quack quixote
Oct 7 '09 at 23:46
add a comment |
I think you can use tr
, as well (though I have no funny format files on which to try):
tr -d 'r' < file1 > file2
add a comment |
I think you can use tr
, as well (though I have no funny format files on which to try):
tr -d 'r' < file1 > file2
add a comment |
I think you can use tr
, as well (though I have no funny format files on which to try):
tr -d 'r' < file1 > file2
I think you can use tr
, as well (though I have no funny format files on which to try):
tr -d 'r' < file1 > file2
edited Mar 12 '17 at 4:54
Steven Penny
1
1
answered Oct 11 '09 at 2:56
warrenwarren
5,7552173127
5,7552173127
add a comment |
add a comment |
In vi or Vim:
:%s/^V^M//g
add a comment |
In vi or Vim:
:%s/^V^M//g
add a comment |
In vi or Vim:
:%s/^V^M//g
In vi or Vim:
:%s/^V^M//g
edited May 2 '12 at 8:42
Peter Mortensen
8,376166185
8,376166185
answered Oct 7 '09 at 12:24
fpmurphyfpmurphy
1,086512
1,086512
add a comment |
add a comment |
I found a very easy way… Open 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.
1
Could you edit your answer to clarify which toggle settings will replicate the behaviour requested by the OP?
– Burgi
May 1 '16 at 1:52
The OP wants to toggle off DOS line endings, soAlt+d
. Sometimes alt gets intercepted by the terminal program, so you can useesc+d
instead.
– spinup
Aug 25 '16 at 14:56
1
Lots of nano shortcuts also work with Shift pressed, which often prevents terminal interception, so 'Alt-Shift-D' works too.
– mwfearnley
Mar 2 '17 at 10:45
add a comment |
I found a very easy way… Open 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.
1
Could you edit your answer to clarify which toggle settings will replicate the behaviour requested by the OP?
– Burgi
May 1 '16 at 1:52
The OP wants to toggle off DOS line endings, soAlt+d
. Sometimes alt gets intercepted by the terminal program, so you can useesc+d
instead.
– spinup
Aug 25 '16 at 14:56
1
Lots of nano shortcuts also work with Shift pressed, which often prevents terminal interception, so 'Alt-Shift-D' works too.
– mwfearnley
Mar 2 '17 at 10:45
add a comment |
I found a very easy way… Open 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.
I found a very easy way… Open 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.
answered Apr 30 '16 at 18:17
Stefan SjöbergStefan Sjöberg
391
391
1
Could you edit your answer to clarify which toggle settings will replicate the behaviour requested by the OP?
– Burgi
May 1 '16 at 1:52
The OP wants to toggle off DOS line endings, soAlt+d
. Sometimes alt gets intercepted by the terminal program, so you can useesc+d
instead.
– spinup
Aug 25 '16 at 14:56
1
Lots of nano shortcuts also work with Shift pressed, which often prevents terminal interception, so 'Alt-Shift-D' works too.
– mwfearnley
Mar 2 '17 at 10:45
add a comment |
1
Could you edit your answer to clarify which toggle settings will replicate the behaviour requested by the OP?
– Burgi
May 1 '16 at 1:52
The OP wants to toggle off DOS line endings, soAlt+d
. Sometimes alt gets intercepted by the terminal program, so you can useesc+d
instead.
– spinup
Aug 25 '16 at 14:56
1
Lots of nano shortcuts also work with Shift pressed, which often prevents terminal interception, so 'Alt-Shift-D' works too.
– mwfearnley
Mar 2 '17 at 10:45
1
1
Could you edit your answer to clarify which toggle settings will replicate the behaviour requested by the OP?
– Burgi
May 1 '16 at 1:52
Could you edit your answer to clarify which toggle settings will replicate the behaviour requested by the OP?
– Burgi
May 1 '16 at 1:52
The OP wants to toggle off DOS line endings, so
Alt+d
. Sometimes alt gets intercepted by the terminal program, so you can use esc+d
instead.– spinup
Aug 25 '16 at 14:56
The OP wants to toggle off DOS line endings, so
Alt+d
. Sometimes alt gets intercepted by the terminal program, so you can use esc+d
instead.– spinup
Aug 25 '16 at 14:56
1
1
Lots of nano shortcuts also work with Shift pressed, which often prevents terminal interception, so 'Alt-Shift-D' works too.
– mwfearnley
Mar 2 '17 at 10:45
Lots of nano shortcuts also work with Shift pressed, which often prevents terminal interception, so 'Alt-Shift-D' works too.
– mwfearnley
Mar 2 '17 at 10:45
add a comment |
I prefer Vim and :set fileformat=unix
. While not the fastest, it does give me a preview. It is especially useful in the case of a file with mixed endings.
add a comment |
I prefer Vim and :set fileformat=unix
. While not the fastest, it does give me a preview. It is especially useful in the case of a file with mixed endings.
add a comment |
I prefer Vim and :set fileformat=unix
. While not the fastest, it does give me a preview. It is especially useful in the case of a file with mixed endings.
I prefer Vim and :set fileformat=unix
. While not the fastest, it does give me a preview. It is especially useful in the case of a file with mixed endings.
edited May 2 '12 at 8:43
Peter Mortensen
8,376166185
8,376166185
answered Oct 7 '09 at 5:39
opelloopello
67657
67657
add a comment |
add a comment |
If you want a GUI method, try the Kate text editor (other advanced text editors may be able to handle this too). Open the find / Replace dialog (Ctrl+R), and replace rn
with n
. (NB: you'll need to choose "Regular expression" from the drop down and deselect "Selection only" from the options.)
EDIT: Or, if you simply want to convert to Unix format, then use the menu option Tools
> End of Line
> Unix
.
There are text editors, such as jEdit, that can do these transformations automatically - you just tell it if you want Unix, Windows or Mac line separators.
– Jonik
Oct 7 '09 at 10:24
Actually, KATE can do that too through the Tools > End of Line menu. Maybe I should have thought more laterally than answering the question exactly as it was worded - but if you know you specifically want to convertrn
ton
then using search/replace is easier than remembering which OS uses which line ending. ;)
– DisgruntledGoat
Oct 10 '09 at 23:22
add a comment |
If you want a GUI method, try the Kate text editor (other advanced text editors may be able to handle this too). Open the find / Replace dialog (Ctrl+R), and replace rn
with n
. (NB: you'll need to choose "Regular expression" from the drop down and deselect "Selection only" from the options.)
EDIT: Or, if you simply want to convert to Unix format, then use the menu option Tools
> End of Line
> Unix
.
There are text editors, such as jEdit, that can do these transformations automatically - you just tell it if you want Unix, Windows or Mac line separators.
– Jonik
Oct 7 '09 at 10:24
Actually, KATE can do that too through the Tools > End of Line menu. Maybe I should have thought more laterally than answering the question exactly as it was worded - but if you know you specifically want to convertrn
ton
then using search/replace is easier than remembering which OS uses which line ending. ;)
– DisgruntledGoat
Oct 10 '09 at 23:22
add a comment |
If you want a GUI method, try the Kate text editor (other advanced text editors may be able to handle this too). Open the find / Replace dialog (Ctrl+R), and replace rn
with n
. (NB: you'll need to choose "Regular expression" from the drop down and deselect "Selection only" from the options.)
EDIT: Or, if you simply want to convert to Unix format, then use the menu option Tools
> End of Line
> Unix
.
If you want a GUI method, try the Kate text editor (other advanced text editors may be able to handle this too). Open the find / Replace dialog (Ctrl+R), and replace rn
with n
. (NB: you'll need to choose "Regular expression" from the drop down and deselect "Selection only" from the options.)
EDIT: Or, if you simply want to convert to Unix format, then use the menu option Tools
> End of Line
> Unix
.
edited May 2 '12 at 8:46
Peter Mortensen
8,376166185
8,376166185
answered Oct 7 '09 at 10:13
DisgruntledGoatDisgruntledGoat
2,44382843
2,44382843
There are text editors, such as jEdit, that can do these transformations automatically - you just tell it if you want Unix, Windows or Mac line separators.
– Jonik
Oct 7 '09 at 10:24
Actually, KATE can do that too through the Tools > End of Line menu. Maybe I should have thought more laterally than answering the question exactly as it was worded - but if you know you specifically want to convertrn
ton
then using search/replace is easier than remembering which OS uses which line ending. ;)
– DisgruntledGoat
Oct 10 '09 at 23:22
add a comment |
There are text editors, such as jEdit, that can do these transformations automatically - you just tell it if you want Unix, Windows or Mac line separators.
– Jonik
Oct 7 '09 at 10:24
Actually, KATE can do that too through the Tools > End of Line menu. Maybe I should have thought more laterally than answering the question exactly as it was worded - but if you know you specifically want to convertrn
ton
then using search/replace is easier than remembering which OS uses which line ending. ;)
– DisgruntledGoat
Oct 10 '09 at 23:22
There are text editors, such as jEdit, that can do these transformations automatically - you just tell it if you want Unix, Windows or Mac line separators.
– Jonik
Oct 7 '09 at 10:24
There are text editors, such as jEdit, that can do these transformations automatically - you just tell it if you want Unix, Windows or Mac line separators.
– Jonik
Oct 7 '09 at 10:24
Actually, KATE can do that too through the Tools > End of Line menu. Maybe I should have thought more laterally than answering the question exactly as it was worded - but if you know you specifically want to convert
rn
to n
then using search/replace is easier than remembering which OS uses which line ending. ;)– DisgruntledGoat
Oct 10 '09 at 23:22
Actually, KATE can do that too through the Tools > End of Line menu. Maybe I should have thought more laterally than answering the question exactly as it was worded - but if you know you specifically want to convert
rn
to n
then using search/replace is easier than remembering which OS uses which line ending. ;)– DisgruntledGoat
Oct 10 '09 at 23:22
add a comment |
Paste this into dos2unix.py Python script.
#!/usr/bin/env python
"""
convert dos linefeeds (crlf) to unix (lf)
usage: dos2unix.py <input> <output>
"""
import sys
if len(sys.argv[1:]) != 2:
sys.exit(__doc__)
content = ''
outsize = 0
with open(sys.argv[1], 'rb') as infile:
content = infile.read()
with open(sys.argv[2], 'wb') as output:
for line in content.splitlines():
outsize += len(line) + 1
output.write(line + 'n')
print("Done. Saved %s bytes." % (len(content)-outsize))
Should work on any platform with Python installed. Public domain.
add a comment |
Paste this into dos2unix.py Python script.
#!/usr/bin/env python
"""
convert dos linefeeds (crlf) to unix (lf)
usage: dos2unix.py <input> <output>
"""
import sys
if len(sys.argv[1:]) != 2:
sys.exit(__doc__)
content = ''
outsize = 0
with open(sys.argv[1], 'rb') as infile:
content = infile.read()
with open(sys.argv[2], 'wb') as output:
for line in content.splitlines():
outsize += len(line) + 1
output.write(line + 'n')
print("Done. Saved %s bytes." % (len(content)-outsize))
Should work on any platform with Python installed. Public domain.
add a comment |
Paste this into dos2unix.py Python script.
#!/usr/bin/env python
"""
convert dos linefeeds (crlf) to unix (lf)
usage: dos2unix.py <input> <output>
"""
import sys
if len(sys.argv[1:]) != 2:
sys.exit(__doc__)
content = ''
outsize = 0
with open(sys.argv[1], 'rb') as infile:
content = infile.read()
with open(sys.argv[2], 'wb') as output:
for line in content.splitlines():
outsize += len(line) + 1
output.write(line + 'n')
print("Done. Saved %s bytes." % (len(content)-outsize))
Should work on any platform with Python installed. Public domain.
Paste this into dos2unix.py Python script.
#!/usr/bin/env python
"""
convert dos linefeeds (crlf) to unix (lf)
usage: dos2unix.py <input> <output>
"""
import sys
if len(sys.argv[1:]) != 2:
sys.exit(__doc__)
content = ''
outsize = 0
with open(sys.argv[1], 'rb') as infile:
content = infile.read()
with open(sys.argv[2], 'wb') as output:
for line in content.splitlines():
outsize += len(line) + 1
output.write(line + 'n')
print("Done. Saved %s bytes." % (len(content)-outsize))
Should work on any platform with Python installed. Public domain.
answered Oct 31 '13 at 9:32
anatoly techtonikanatoly techtonik
163112
163112
add a comment |
add a comment |
CR LF
to LF
using awk:
awk -v RS='r?n' 1
command | awk -v RS='r?n' 1
awk -v RS='r?n' 1 filename
Usage example:
echo -e 'foonbarrnbaz' | awk -v RS='r?n' 1 | hexdump -C
Explanation:
-v RS='r?n'
sets variable RS (input record separator) to r?n
, meaning input is read line by line separated by LF (n
) which may (?
) be preceded by CR (r
).
1
is the script awk executes. A script consists of condition { action }
. In this case, 1
is the condition which evaluates to true. The action is omitted, so the default action is executed, which means print the current line (which could also be written as {print $0}
or simply {print}
).
LF
to CR LF
: You can set the variable ORS
(output record separator) to modify the line ends of the output. Example:
echo -e 'foonbarrnbaz' | awk -v RS='r?n' -v ORS='rn' 1 | hexdump -C
add a comment |
CR LF
to LF
using awk:
awk -v RS='r?n' 1
command | awk -v RS='r?n' 1
awk -v RS='r?n' 1 filename
Usage example:
echo -e 'foonbarrnbaz' | awk -v RS='r?n' 1 | hexdump -C
Explanation:
-v RS='r?n'
sets variable RS (input record separator) to r?n
, meaning input is read line by line separated by LF (n
) which may (?
) be preceded by CR (r
).
1
is the script awk executes. A script consists of condition { action }
. In this case, 1
is the condition which evaluates to true. The action is omitted, so the default action is executed, which means print the current line (which could also be written as {print $0}
or simply {print}
).
LF
to CR LF
: You can set the variable ORS
(output record separator) to modify the line ends of the output. Example:
echo -e 'foonbarrnbaz' | awk -v RS='r?n' -v ORS='rn' 1 | hexdump -C
add a comment |
CR LF
to LF
using awk:
awk -v RS='r?n' 1
command | awk -v RS='r?n' 1
awk -v RS='r?n' 1 filename
Usage example:
echo -e 'foonbarrnbaz' | awk -v RS='r?n' 1 | hexdump -C
Explanation:
-v RS='r?n'
sets variable RS (input record separator) to r?n
, meaning input is read line by line separated by LF (n
) which may (?
) be preceded by CR (r
).
1
is the script awk executes. A script consists of condition { action }
. In this case, 1
is the condition which evaluates to true. The action is omitted, so the default action is executed, which means print the current line (which could also be written as {print $0}
or simply {print}
).
LF
to CR LF
: You can set the variable ORS
(output record separator) to modify the line ends of the output. Example:
echo -e 'foonbarrnbaz' | awk -v RS='r?n' -v ORS='rn' 1 | hexdump -C
CR LF
to LF
using awk:
awk -v RS='r?n' 1
command | awk -v RS='r?n' 1
awk -v RS='r?n' 1 filename
Usage example:
echo -e 'foonbarrnbaz' | awk -v RS='r?n' 1 | hexdump -C
Explanation:
-v RS='r?n'
sets variable RS (input record separator) to r?n
, meaning input is read line by line separated by LF (n
) which may (?
) be preceded by CR (r
).
1
is the script awk executes. A script consists of condition { action }
. In this case, 1
is the condition which evaluates to true. The action is omitted, so the default action is executed, which means print the current line (which could also be written as {print $0}
or simply {print}
).
LF
to CR LF
: You can set the variable ORS
(output record separator) to modify the line ends of the output. Example:
echo -e 'foonbarrnbaz' | awk -v RS='r?n' -v ORS='rn' 1 | hexdump -C
edited Feb 7 at 10:28
answered Feb 6 at 15:58
MartinMartin
1236
1236
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f52044%2fconvert-crlfs-to-line-feeds-on-linux%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
Dupe: superuser.com/questions/38744/…. The link provided in the accepted answer covers the dos2unix, perl and vi options among others.
– nagul
Oct 7 '09 at 9:13
2
This already has better answers though (so if one of these is to be closed, it should probably be that one)
– Jonik
Oct 7 '09 at 12:02