I'm trying to convert new lines in a text file to "\n" characters in a string by running an Awk command using os.system:
awk '{printf "%s\\n", $0}' /tmp/file
My problem is that escaping the quotes is not working:
$ replace = "awk '{printf \"%s\\n\", $0}' /tmp/file"
$ print(replace)
awk '{printf "%s\n", $0}' /tmp/file
I've tried using shlex as well:
$ import shlex
$ line = "awk '{printf \"%s\\n\", $0}'"
$ lexer = shlex.shlex(line, posix=True)
$ lexer.escapedquotes = "'\""
$ mystring = ','.join(lexer)
$ print(mystring)
awk,{printf "%s\n", $0}
I either need to know how to run this awk command with Python or how to convert carriage returns/new lines in a file to "\n" in a string.
EDIT: Here's an example file:
oneline
twoline
threeline
A method should return the following from this file:
oneline\ntwoline\n\nthreeline