filmov
tv
Unix & Linux: How to add a carriage return before every newline? (6 Solutions!!)
Показать описание
Unix & Linux: How to add a carriage return before every newline?
The Question: I have a file that only uses \n for new lines, but I need it to have \r\n for
each new line. How can I do this?
For example, I solved it in Vim using :%s/\n/\r\n/g, but I would like to use a
script or command-line application. Any suggestions?
I tried looking this up with sed or grep, but I got immediately confused by the
escape sequence workarounds (I am a bit green with these commands).
If interested, the application is related to my question/answer https://
repo-hg-import-is-failing-but-the
Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful
== This solution helped 9 people ==
This is exactly what unix2dos does:
If you want to do it with sed, you can insert a carriage return at the end of
every line:
This replaces (s) the zero-size area right before the end of the line ($) with
that is a non-standard extension - if you don't have it, use a temporary file.
== This solution helped 39 people ==
Debian):
unix2dos file
Note that this implementation won't insert a CR before every LF, only before
those LFs that are not already preceded by one (and only one) CR and will skip
binary files (those that contain byte values in the 0x0 -> 0x1f range other
than LF, FF, TAB or CR).
or use sed:
CR=$(printf 'r')
sed "s/$/$CR/" file
or use awk:
awk '{printf "%srn", $0}' file
or use perl:
perl -pe 's|n|rn|' file
== This solution helped 1 person ==
In awk you can try
awk '{print $0 "r"}'
Or
awk -v r=$'r' '{print $0 r}'
The $'r' is an example of ANSI-C style quoting as supported by a few shells
It offers a general way to express weird characters, try this, for example:
awk -v r=$'U0001F608' '{print $0 r}'
== This solution helped 1 person ==
A portable shell function that will do this:
u2dos() (set -f; IFS='
'; printf '%srn' $(cat "$1"))
With that you can do:
u2dos file >dosfile
== This solution helped 2 people ==
Doing this with POSIX is tricky:
not support r or 15. Even if it did, the in place option -i is not POSIX
support r and 15, however the -i inplace option is not POSIX
not support r, 15, n or 12
To remove carriage returns:
awk 'BEGIN{RS="1";ORS="";getline;gsub("r","");print>ARGV[1]}' file
To add carriage returns:
awk 'BEGIN{RS="1";ORS="";getline;gsub("n","r&");print>ARGV[1]}' file
The Question: I have a file that only uses \n for new lines, but I need it to have \r\n for
each new line. How can I do this?
For example, I solved it in Vim using :%s/\n/\r\n/g, but I would like to use a
script or command-line application. Any suggestions?
I tried looking this up with sed or grep, but I got immediately confused by the
escape sequence workarounds (I am a bit green with these commands).
If interested, the application is related to my question/answer https://
repo-hg-import-is-failing-but-the
Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful
== This solution helped 9 people ==
This is exactly what unix2dos does:
If you want to do it with sed, you can insert a carriage return at the end of
every line:
This replaces (s) the zero-size area right before the end of the line ($) with
that is a non-standard extension - if you don't have it, use a temporary file.
== This solution helped 39 people ==
Debian):
unix2dos file
Note that this implementation won't insert a CR before every LF, only before
those LFs that are not already preceded by one (and only one) CR and will skip
binary files (those that contain byte values in the 0x0 -> 0x1f range other
than LF, FF, TAB or CR).
or use sed:
CR=$(printf 'r')
sed "s/$/$CR/" file
or use awk:
awk '{printf "%srn", $0}' file
or use perl:
perl -pe 's|n|rn|' file
== This solution helped 1 person ==
In awk you can try
awk '{print $0 "r"}'
Or
awk -v r=$'r' '{print $0 r}'
The $'r' is an example of ANSI-C style quoting as supported by a few shells
It offers a general way to express weird characters, try this, for example:
awk -v r=$'U0001F608' '{print $0 r}'
== This solution helped 1 person ==
A portable shell function that will do this:
u2dos() (set -f; IFS='
'; printf '%srn' $(cat "$1"))
With that you can do:
u2dos file >dosfile
== This solution helped 2 people ==
Doing this with POSIX is tricky:
not support r or 15. Even if it did, the in place option -i is not POSIX
support r and 15, however the -i inplace option is not POSIX
not support r, 15, n or 12
To remove carriage returns:
awk 'BEGIN{RS="1";ORS="";getline;gsub("r","");print>ARGV[1]}' file
To add carriage returns:
awk 'BEGIN{RS="1";ORS="";getline;gsub("n","r&");print>ARGV[1]}' file