0

this code works fine from terminal/bash script:

 awk '/^(name|count|region)/{sep = (/^region/? ORS:OFS);gsub(/^[^:]+:[[:space:]]*|[[:space:]]*$/, "");printf "%s%s", $0, sep}' output.txt>1.txt

Trying to execute it from inside python:

 #!/usr/bin/python
    import sys
    import json
    import re
    import os
    import subprocess




  def bash_command(cmd):
    subprocess.Popen(cmd, shell=True, executable='/bin/bash')


bash_command = ('''"awk '/^(name|count|region)/{sep = (/^region/? ORS:OFS);gsub(/^[^:]+:[[:space:]]*|[[:space:]]*$/, "");printf "%s%s", $0, sep}' output.txt> 1.txt"''')

No errors but 1.txt is not created

3
  • 2
    Why on earth are you calling a scripting language from another scripting language? why not implement what awk is doing in python? Commented Mar 29, 2018 at 14:14
  • because can't get desired output from python itself Commented Mar 29, 2018 at 14:15
  • You should ask the question again, stating what data you have, and what you are trying to do with it, and what problems you are having, in python - rather than hacking things like this. Commented Mar 29, 2018 at 14:18

2 Answers 2

1

To call a function with a string parameter, you have to use this pseudo-code. Instead, you were setting your function name as a string tuple.

#!/usr/bin/python
import sys
import json
import re
import os
import subprocess

def bash_command(cmd):
    subprocess.Popen(cmd, shell=True, executable='/bin/bash')


bash_command("echo test")
Sign up to request clarification or add additional context in comments.

Comments

0

Not the answer you are looking for, but you can use the python-awk library

pawk is a python-based replacement for awk.

It uses python for line-by-line processing of files

Although the package looks like it could a cleanup.

https://github.com/jasontrigg0/pawk

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.