简体   繁体   中英

How to fix "awk: Syntax error Context is: >>> ' -'' <<<" error in python subprocess?

Trying to do p4 files with subprocess.Popen and then pass that output to awk again via subprocess.Popen. Input will be of the form: //xxx/xx/xx/xx/xx/xx.xx#99 - edit change xxxxxxx (text+k)

Replaced original names with 'x'es here since it's all proprietary information. Would like to split based on the " -"(space hyphen) and get the file name with revision number.

awk_cmd = ["awk","-F\' -\'","\'{print $1}\'"]
awk_cmd_output = subprocess.Popen(awk_cmd, stdout=subprocess.PIPE, stdin = p4_files_output.stdout)

Seeing this error coming from awk: awk: Syntax error Context is:

' -'' <<<

When I run the same in cmd awk -F' -' '{print $1}', it works fine. It seems like awk is getting an extra single quote in the end. It should ideally be ' -' and not ' -''.

sticking to awk & subprocess solution, just use double quotes and drop the single quotes, that are only useful for the shell, but passed literally to the process when using subprocess , which make the command fail:

awk_cmd = ["awk","-F -","{print $1}"]

Let subprocess add the proper quoting. It won't split the arguments according to spaces.

But the best way would be to drop awk , and use str.split , something like:

output = []
for line in p4_files_output.stdout:
    output.append(line.decode().split(" -")[0])
output_text = "\n".join(output)

or single line using a comprehension:

output_text = "\n".join([line.decode().split(" -")[0] line in p4_files_output.stdout])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM