带有变量的python中的awk命令

我有以下命令传递给python脚本。

11-06 12:49:45.939 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - MA: in onCreate()
11-06 12:49:45.940 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - MA: starting service
11-06 12:49:45.942 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - MA: binding service
11-06 12:49:45.949 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - BC: in constructor
11-06 12:49:45.973 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - BC: in onStart()
11-06 12:49:46.011 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService: in constructor
    in onCreate()
11-06 12:49:46.025 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService: in onBind()
11-06 12:49:46.111 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - MA: service connected
11-06 12:50:27.048 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - GH: in onLocationChanged()
.... (lots more locations here - below is where it swapped to activity #2)
11-06 12:51:05.044 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - GH: in onLocationChanged()
11-06 12:51:05.650 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - MAP: in onCreate()
    binding service
11-06 12:51:06.773 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - MAP: service connected
11-06 12:51:06.776 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - GH: in onLocationChanged()
11-06 12:51:07.040 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - GH: in onLocationChanged()
11-06 12:51:07.411 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - BC: in onStop()
11-06 12:51:08.046 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - GH: in onLocationChanged()
.... (here is where I put the 2nd activity into the background)
11-06 12:51:22.058 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - GH: in onLocationChanged()
11-06 12:51:22.379 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - MAP: in onDestroy() (unbinding service)
11-06 12:51:22.383 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - MAP: in onDestroy (stopping service)
11-06 12:51:23.038 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - GH: in onLocationChanged()
11-06 12:51:24.040 25852-25852/uk.co.nightshadearts.gpscompass V/BackgroundLocationService - GH: in onLocationChanged()

无论我尝试什么,都会造成混乱(操作系统,系统,popen,subprocess.call ...) 我最后的尝试是:

awk '/^>/{n=split($0,a,"_")} /string/{sum+=a[n]} END{print sum}' filein.fasta

这样,我现在没有错误,但是由于它会导致脚本中的错误,因此无法正常工作。 如何在python中正确传递此命令?在这种情况下,引号是一个痛苦的话题

luvlu 回答:带有变量的python中的awk命令

请避免在awk脚本中使用python命令。

我非常喜欢awk,但是python可以轻松完成awk可以做到的事情。

awk '/^>/{n=split($0,a,"_")} /string/{sum+=a[n]} END{print sum}' filein.fasta

确实

  

对于开头包含>的每一行,它使用定界符_进行分割。它会继续进行解析,并且在找到/ string /时,会将拆分行的最后一个字段添加到变量sum中。

使用python:

sum = 0
with open("filein.fasta") as input:
    for line in input:
        if line[0] == '>':
            fields = line.split('_')
        if (string in line) and fields:
            sum += int(fields[-1]) # or float
print(sum)

调用子进程将使代码的可移植性降低,更难以调试或监视。

顺便说一句,awk脚本不好,应该是:

awk '/^>/{n=split($0,"_")} /string/&&n{sum+=a[n]} END{print sum}' filein.fasta
本文链接:https://www.f2er.com/3151430.html

大家都在问