我有一个文件A.txt:
RS0255_RS0083:115,124,129,141,143,168,170,180
RS0343_RS0083:112,113,163,175,181
RS0343_RS0255:94,101,107,164,179,183
我想对 后的数字执行数学运算: 例如,我想为每个数字加 10: 输出:
RS0255_RS0083:125,134,139,151,153,178,180,190
RS0343_RS0083:122,123,173,185,191
RS0343_RS0255:104,111,117,174,189,193
perl
我知道如何在 R 中执行此操作,但如何对or中文件中的数字进行数学运算awk
?
答案1
实际上,有几十种工具可用于对文本文件进行不同的操作。对于您提到的特定情况,我可能会使用 perl:
$ perl -pe 's/\b(\d+)\b/$1 + 10/ge' fileA.txt
RS0255_RS0083:125,134,139,151,153,178,180,190
RS0343_RS0083:122,123,173,185,191
RS0343_RS0255:104,111,117,174,189,193
答案2
gawk '$0+0 == $0 {$0 += 10} {ORS = RT} 1' RS='[:,\n]' file
答案3
答案awk
:将每一行解析为两个:
分隔的字段。使用split()
第二个字段将其拆分为逗号上的字段,并在循环中修改拆分字段,创建新的输出记录。然后输出新记录,以逗号作为分隔符,并以原来的第一个字段作为“前缀”。
awk -F : '
BEGIN { OFS = "," }
{
prefix = $1
nf = split($2,a,",")
$0 = ""
for (i = 1; i <= nf; ++i)
$i = a[i] + 10
printf "%s:%s\n", prefix, $0
}' fileA.txt
输出将是
RS0255_RS0083:125,134,139,151,153,178,180,190
RS0343_RS0083:122,123,173,185,191
RS0343_RS0255:104,111,117,174,189,193
上述代码的更紧凑的表示(对于那些认为一行在某种程度上“更好”的人):
awk -F: '{p=$1;n=split($2,a,",");$0="";for(i=1;i<n;++i)$i=a[i]+10;printf "%s:%s\n",p,$0}' OFS=, fileA.txt
较旧的答案,不如上面的答案“好”:
使用并将每一行解释为由或awk
分隔的字段列表,在每行的第二个字段中添加 10::
,
awk -F '[:,]' 'BEGIN { OFS="," } { for (i = 2; i <= NF; ++i) $i += 10 }; 1' fileA.txt
这会给你
RS0255_RS0083,125,134,139,151,153,178,180,190
RS0343_RS0083,122,123,173,185,191
RS0343_RS0255,104,111,117,174,189,193
要将每行的第一个逗号更改回 a :
,请使用sed 's/,/:/'
:
awk -F '[:,]' 'BEGIN { OFS="," } { for (i = 2; i <= NF; ++i) $i += 10 }; 1' fileA.txt |
sed 's/,/:/'
答案4
我建议这样awk
:
$ awk -F':|,' 'BEGIN { OFS="" }
$1 = $1":" {
OFS="";
for(i=2; i<=NF; i++) {
$i = $i + 10;
if ( i != NF ) {
$i = $i","
}
};
}1' file
RS0255_RS0083:125,134,139,151,153,178,180,190
RS0343_RS0083:122,123,173,185,191
RS0343_RS0255:104,111,117,174,189,193