我有一个这种形式的文件:
2ThroughputRule.js:128 bw 3545891 3545891 3545891
ThroughputRule.js:128 bw 124776 1835334 1151111
ThroughputRule.js:128 bw 1959606 1876758 2435492
ThroughputRule.js:128 bw 3213333 1765905 2979981
2ThroughputRule.js:128 bw 1985927 2386289 2284143
我想要一种处理文件的方法,以便shell脚本可以读取每一行的第一个字符,如果它找到一个数字副本,那么该数字旁边的数字直到下面的EOL。
例如我有
2ThroughputRule.js:128 bw 3545891 3545891 3545891
结果将是
2ThroughputRule.js:128 bw 3545891 3545891 3545891
吞吐量Rule.js:128 bw 3545891 3545891 3545891
如果我有
4ThroughputRule.js:128 bw 3545891 3545891 3545891
吞吐量Rule.js:128 bw 3545891 3545891 3545891
吞吐量Rule.js:128 bw 3545891 3545891 3545891
吞吐量Rule.js:128 bw 3545891 3545891 3545891
有可能吗?
答案 0 :(得分:0)
你可以尝试这个程序。将其另存为pr.awk
。并运行awk -f pr.awk test.txt
function force_num(x) {return x+0}
match($0, "^[0-9]+") {
n = substr($0, RSTART, RLENGTH)
n = force_num(n)
rst = substr($0, RSTART + RLENGTH)
for (i=1; i<=n; i++)
print rst
next
}
{
print
}
答案 1 :(得分:0)
您可以使用此awk命令:
awk '1; /^[0-9]/{print substr($0,2)}' file
2ThroughputRule.js:128 bw 3545891 3545891 3545891
ThroughputRule.js:128 bw 3545891 3545891 3545891
ThroughputRule.js:128 bw 124776 1835334 1151111
ThroughputRule.js:128 bw 1959606 1876758 2435492
ThroughputRule.js:128 bw 3213333 1765905 2979981
2ThroughputRule.js:128 bw 1985927 2386289 2284143
ThroughputRule.js:128 bw 1985927 2386289 2284143
命令分手:
1; # prints each record
/^[0-9]/ # if a row starts with a digit
print substr($0,2) # prints from 2nd char onward in next line
答案 2 :(得分:0)
您可以使用grep
来匹配字符串,使用cut
来删除字符:
grep "^[0-9]" file | cut -c 2-