Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        4年前关闭。
                                                                                            
                
        
我有这种形式的文件:

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

ThroughputRule.js:128 bw 3545891 3545891 3545891

如果我有

4ThroughputRule.js:128 bw 3545891 3545891 3545891

ThroughputRule.js:128 bw 3545891 3545891 3545891

ThroughputRule.js:128 bw 3545891 3545891 3545891

ThroughputRule.js:128 bw 3545891 3545891 3545891

有可能这样做吗?

最佳答案

您可以尝试此程序。将其另存为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
}

10-08 04:44