Python 需要分析多行和多列中字符之间的子字符串

Python 需要分析多行和多列中字符之间的子字符串,python,regex,awk,sed,grep,Python,Regex,Awk,Sed,Grep,我有一个文件(input.file),看起来像: sampleID_001 1/1:1.3:0.1225,0.455,0.4225 1/1:1.3:0.1225,0.455,0.4225 0/0:0.2:0.525,0.055,0.0025 sampleID_002 0/0:0.1:0.9025,0.095,0.0025 0/0:0.1:0.9025,0.095,0.0025 0/0:0.1:0.9025,0.095,0.0025 sampleID_003 0/0:0.3:0.7025,0.29

我有一个文件(input.file),看起来像:

sampleID_001 1/1:1.3:0.1225,0.455,0.4225 1/1:1.3:0.1225,0.455,0.4225 0/0:0.2:0.525,0.055,0.0025
sampleID_002 0/0:0.1:0.9025,0.095,0.0025 0/0:0.1:0.9025,0.095,0.0025 0/0:0.1:0.9025,0.095,0.0025
sampleID_003 0/0:0.3:0.7025,0.295,0.0025 0/0:0.3:0.7025,0.295,0.0025 0/0:0.3:0.7025,0.295,0.0025
sampleID_001 1.3 1.3 0.2  
sampleID_002 0.1 0.1 0.1  
sampleID_003 0.3 0.3 0.3
但我只想提取:'之间的值,以便所需的输出如下所示:

sampleID_001 1/1:1.3:0.1225,0.455,0.4225 1/1:1.3:0.1225,0.455,0.4225 0/0:0.2:0.525,0.055,0.0025
sampleID_002 0/0:0.1:0.9025,0.095,0.0025 0/0:0.1:0.9025,0.095,0.0025 0/0:0.1:0.9025,0.095,0.0025
sampleID_003 0/0:0.3:0.7025,0.295,0.0025 0/0:0.3:0.7025,0.295,0.0025 0/0:0.3:0.7025,0.295,0.0025
sampleID_001 1.3 1.3 0.2  
sampleID_002 0.1 0.1 0.1  
sampleID_003 0.3 0.3 0.3
但是,请注意,实际文件中还有更多的行和列需要处理

我试过:

grep -oP '(?<=from:)*?(?=:)' <<< "input.file" > output.file
sed 's/.*:\(.*\):.*/\1/' input.file > output.file
awk "/:/,/:/ { print }" input.file > output.file

grep-oP'(?使用空格、逗号或冒号作为字段分隔符:

awk-F'[:,]'{print$1,$3,$8,$13}文件

考虑到标签中包含python,我会这样做:

import re
str = "sampleID_001 1/1:1.3:0.1225,0.455,0.4225 1/1:1.3:0.1225,0.455,0.4225 0/0:0.2:0.525,0.055,0.0025"
s = re.findall('\:(.*?)\:', str)
print("{} {} {} {}".format(str.split(' ')[0], s[0], s[1], s[2]))

对于每一行。

使用给定输入的解决方案可能会因其他输入而失败,因为很难判断您将遇到多少空格和其他字符。要进行良好的控制,请使用
awk

awk -F ":| " '{print $1 " " $3 " " $6 " " $9}' input.file
尝试使用
sed
时,必须写出每个步骤:

sed -r 's/([^ ]*)[^:]*:([^:]*):[^:]*:([^:]*):[^:]*:([^:]*):.*/\1 \2 \3 \4/g' input.file
将第一个字段也放在冒号中时,可以缩短行,但这很难理解/更改/调试:

sed -r 's/([^ ]*)/:\1:/;s/:([^:]*):[^:]*/\1 /g' input.file
您可能更喜欢采取一些小步骤:

sed 's/ /:/' input.file | cut -d":" -f 1,3,5,7 | tr ':' ' ' 

Python是如何参与的?