-2

What is the best way to execute awk command below in Python 3?

awk -F'"' -v OFS='' '{ for (i=2; i<=NF; i+=2) gsub(",", " ", $i) } 1' file1.csv > file2.csv
martineau
  • 119,623
  • 25
  • 170
  • 301
Flake
  • 1
  • 1

2 Answers2

0

Use the os module to perform arbitrary commands from Python:

import os

os.system('do some command')
David
  • 424
  • 3
  • 16
  • Thanks David for your quick response. When I try that command with the single quotes, I get SyntaxError: EOL while scanning string literal. My command was os.system('awk -F'"' -v OFS= '' '{for (i=2; i<=NF; i+=2) gsub(",", " ", $i) } 1' dhcp_option6.csv > dhcp_option6_modified.csv') – Flake May 16 '20 at 20:24
  • it's seeing the quotation marks and thinking the string is complete, read more here: https://stackoverflow.com/questions/35817/how-to-escape-os-system-calls – David May 16 '20 at 20:59
0

I'd use the subprocess module - check_output Example:

subprocess.check_output("awk...", shell=True)