I have an output file on Linux File System that I need to strip CRLF in the file anywhere there is not a space proceeding the CRLF. All Lines in the file that are valid have many spaces proceeding the CRLF. The CRLF that I want to remove have NO Space in front of the CRLF. Welcome to TR | SED | AWK. I have tried many methods with no luck.
I should mention the file is a fixed length file, if that helps.
I'm game with any of these methods TR | SED | AWK
I have attempted many different commands this evening but with various results but none that solve my issue.
echo -n $(tr -d "?<-c\x20\r\n" < file) > output.txt
echo -n $(tr -d "?<\x20\r\n" < file) > output.txt;
(tr -d "\r\n" < file.txt | fold -w 2538;echo) | sed 's/$/\r/' > output.txt
awk 'length($0) != 2536 {sub(/\r$/,""); printf "%s", $0; next} {print}' file.txt > output.txt
The line bellow will do it using Python (in place of sed or awk). It is just less used for this kind of things because there are no shortcuts in Python to reading/writting to stdin/stdout - the one liner requires full function calls to
open("input.txt", "rb")- and the write converse - to be written, and is otherwise more verbose - as a proper Python program this would be 4 lines long, maybe:Or, otherwise, for using pipes, and relying on the automatic text decoding/encoding - this version:
(the final code size is the same, so unless you may replace "cat input.txt" for the input or you want to pipe the result somewhere else, the other version is the same size, and could avoid shell pitfalls)