Regex 在我的输出文本文件中附加了两个浮点数
在我的输出文件中,与两个浮点数对应的两列连接在一起,形成一列。这里显示了一个示例,是否有必要将这两列彼此分离 在这里,这应该是5列,由空格分隔,但第3列和第4列之间缺少空格。对于某些UNIX命令,如cut、awk、sed甚至正则表达式,是否有任何方法可以纠正此错误Regex 在我的输出文本文件中附加了两个浮点数,regex,bash,sed,awk,output,Regex,Bash,Sed,Awk,Output,在我的输出文件中,与两个浮点数对应的两列连接在一起,形成一列。这里显示了一个示例,是否有必要将这两列彼此分离 在这里,这应该是5列,由空格分隔,但第3列和第4列之间缺少空格。对于某些UNIX命令,如cut、awk、sed甚至正则表达式,是否有任何方法可以纠正此错误 3.77388 0.608871 -8216.342.42161 1.88655 4.39243 0.625 -8238.241.49211 0.889258 4.38903 0.608871 -7871.71.52994 0.883
3.77388 0.608871 -8216.342.42161 1.88655
4.39243 0.625 -8238.241.49211 0.889258
4.38903 0.608871 -7871.71.52994 0.883976
4.286 0.653226 -8287.322.3195 2.13736
4.29313 0.629032 -7954.651.59168 1.02046
更正后的版本应如下所示:
3.77388 0.608871 -8216.34 2.42161 1.88655
4.39243 0.625 -8238.24 1.49211 0.889258
4.38903 0.608871 -7871.7 1.52994 0.883976
4.286 0.653226 -8287.32 2.3195 2.13736
4.29313 0.629032 -7954.65 1.59168 1.02046
更多信息:第4列总是小于10,因此它在小数点左侧只有一位数字
我已尝试使用awk:
tail -n 5 output.dat | awk '{print $3}'
-8216.342.42161
-8238.241.49211
-7871.71.52994
-8287.322.3195
-7954.651.59168
有没有办法将此列分为两列?一种解决方案:
sed 's/\(\.[0-9]*\)\([0-9]\.\)/\1 \2/'
使用Perl一行程序:
perl -pe 's/(\d+\.\d+)(\d\.\d+)/$1 $2/' < output.dat > fixed_output.dat
perl-pe的/(\d+\.\d+)(\d\.\d+)/$1$2/”fixed\u output.dat
您的输入文件
$ cat file
3.77388 0.608871 -8216.342.42161 1.88655
4.39243 0.625 -8238.241.49211 0.889258
4.38903 0.608871 -7871.71.52994 0.883976
4.286 0.653226 -8287.322.3195 2.13736
4.29313 0.629032 -7954.651.59168 1.02046
Awk方法
awk '{
n = index($3,".") # index of dot from field 3
x = substr($3,1,n+3) ~/\.$/ ? n+1 : n+2 # Decision for no of char to consider
$3 = substr($3,1,x) OFS substr($3,x+1) # separate out fields
$0 = $0 # Recalculate fields (number of fields NF)
$1 = $1 # recalculate the record, removing excess spacing (the new field separator becomes OFS, default is a single space)
}1' OFS='\t' file
结果
3.77388 0.608871 -8216.34 2.42161 1.88655
4.39243 0.625 -8238.24 1.49211 0.889258
4.38903 0.608871 -7871.7 1.52994 0.883976
4.286 0.653226 -8287.32 2.3195 2.13736
4.29313 0.629032 -7954.65 1.59168 1.02046
并非没有关于第3列和第4列中数字范围的一些先验知识。第4列的范围始终在0和10之间。所以我知道第4列的小数点左边只有一个数字!10包括在这个范围内吗?不,一点也不!只有一个数字0到9我认为最好的答案是修复生成原始输出的程序…+1。将其限制为第三个单词:
perl-lane'$F[2]=~s/(\.\d+)(\d\)/$1$2/;打印“@F”