remove \r and \n from a text file
How to remove "\r" and " \n" from a text file with a script?
e.g. The input is: # cat test.txt test it is $ od -txc test.txt 0000000 74736574 74690a0d 0a0d0a0d 0a0d7369 t e s t \r \n i t \r \n \r \n i s \r \n 0000020 The output should be: $ od -txc test.txt 0000000 74736574 74690a0d 73690a0d 00000a0d t e s t \r \n i t \r \n i s \r \n \0 \0 0000016 |
Your input and output are identical - \r and \n are in both.
Code:
tr -d '\r' < inputfile | tr -d '\n' > outputfile |
remove extra \r and \n from a text file
Quote:
|
The following command will remove all lines that contain \r\n only (and make a backup of the original file):
Code:
sed -i.bak -e '/^\r$/d' filename |
Hi. Try this:
Code:
cat file | awk 'BEGIN{RS="\0";} {gsub(/(\r\n)+/,"\r\n");print}'| od -txc |
sed s/\\r\\n\\r\\n/\\r\\n/g in a pipe line.
So a paired \r\n will become a single one. End |
Hello.
Here is an extraction of sed's info page (info sed) Code:
3.3 Overview of Regular Expression Syntax |
Yes, but once ported and given that it is "visible" as \r \n (`od -c`) in a given *nix box, sed will work. That I believe is what the OP wanted.
End |
As usual, lots of ways to do this...
The tr command example might deserve a little closer look because it's an unusual strategy (well, not for Unix)... In the Unix world, many programs act as filters which take some input-stream, do something to it, and write the results to an output-stream. The output-stream of one filter is often piped to another filter, becoming its input-stream. Piping is indicated by the '|' character. In the code: Code:
tr -d '\r' < inputfile | tr -d '\n' > outputfile The first part: Code:
tr -d '\r' < inputfile Code:
tr -d '\n' > outputfile So this approach solves the problem in a very Unix-like way: by running two small, generalized programs and piping them together to solve a problem. |
unix2dos, dos2unix,
|
All times are GMT -5. The time now is 03:22 PM. |