3

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
Ken J
  • 877
  • 12
  • 21

1 Answers1

2

in that case you can use raw prefix + triple quoting:

s = r"""awk '{printf "%s\\n", $0}' /tmp/file"""
print(s)

result:

awk '{printf "%s\\n", $0}' /tmp/file

So os.system(s) works.

in pure python, replace newline by literal \n (adapted from another answer of mine):

with open(file1,"rb") as f:
   contents = f.read().replace(b"\n",b"\\n")
with open(file1+".bak","wb") as f:
   f.write(contents)
os.remove(file1)
os.rename(file1+".bak",file1)
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • Your pure python method doesn't concat the file...it just adds a bunch of "\n"'s wherever it should. Example file: asd ffd Your method creates this: asd\n ffd\n \n Where it should create this: asdffd\n – Ken J Apr 25 '18 at 13:19
  • can you edit your question to see the input & expected output? because it's unclear in comments. – Jean-François Fabre Apr 25 '18 at 14:17
  • Added an example with expected output – Ken J Apr 25 '18 at 14:30
  • thanks. In general, calling `awk` or `sed` from python isn't a very good idea because it makes your script non-portable and performs a system call, which breaks the chain of exception, when everything can be done even simpler in pure python. – Jean-François Fabre Apr 26 '18 at 13:30