简体   繁体   English

将空/空白列替换为最后一个已知列(sed / awk /脚本)

[英]Replace null/blank columns with last known column (sed / awk / script)

Can someone help me figure out how to replace the empty columns with the last known value. 有人可以帮我弄清楚如何用最近的已知值替换空列。 Here is a line that i would like the number "0.7588044" to replace the null values in this line: 这是我希望数字“ 0.7588044”替换此行中的空值的行:

0.7723808|0.767398|0.7645381|0.7605125|0.759718|0.7588044|0.7588044|0.7588044|0.7588044|0.7588044|0.7588044||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

In other words, I would like "0.7588044" to be between the empty/null "|" 换句话说,我希望“ 0.7588044”介于空/空“ |”之间 delimeters at the end of the line. 行尾的距离。

I can't figure out how to do this with something like sed. 我不知道如何使用sed这样的方法。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Here are the first 3 lines of my file: 这是我文件的前三行:

66943|0.9939215|0.9873032|0.9791299|0.9708792|0.9623731|0.9535987|0.945847|0.9379317|0.9286675|0.9203091|0.9127985|0.9041528|0.8966769|0.8902251|0.8832675|0.8778407|0.8734665|0.8679647|0.8616999|0.8560756|0.8518617|0.8463235|0.8410841|0.8342401|0.8311638|0.8261909|0.8252836|0.8218218|0.8177906|0.815474|0.8122096|0.8115648|0.8108233|0.8108233|0.8108233|0.8108233|0.8108233|0.8108233||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
69550|0.9946427|0.9888051|0.9815896|0.9742986|0.966774|0.9590039|0.9521323|0.9451087|0.9368793|0.9294462|0.9227601|0.9150554|0.9083862|0.9026252|0.896407|0.8915528|0.8876377|0.8827099|0.8770942|0.8720485|0.8682655|0.8632902|0.8585799|0.8524216|0.8496516|0.8451712|0.8443534|0.8412323|0.8375956|0.8355048|0.8325575|0.8319751|0.8313053|0.8313053|0.8313053|0.8313053|0.8313053|0.8313053||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
380713|0.9942899|0.9880703|0.9803859|0.9726248|0.9646193|0.9563567|0.9490533|0.941592|0.9328543|0.9249665|0.917875|0.9097072|0.9026409|0.8965395|0.8899569|0.8848204|0.8806788|0.8754678|0.8695317|0.8642001|0.8602043|0.8549507|0.8499787|0.8434811|0.8405594|0.8358352|0.834973|0.8316831|0.8278509|0.8256481|0.8225436|0.8219303|0.8212249|0.8212249|0.8212249|0.8212249|0.8212249|0.8212249||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

The awk code works but just the first line: awk代码有效,但仅第一行:

You can use the following awk script: 您可以使用以下awk脚本:

awk -F'|' 'BEGIN{OFS="|"}{for(i=1;i<NF;i++){if($i==""){$i=l}else{l=$i}}print}'

It is better readable in this form: 这种形式的可读性更好:

BEGIN {
    OFS="|" # set output field separator to |
}
{
    for(i=1;i<NF;i++) { # iterate through columns
        if($i=="") { # if current column is empty
            $i=l # use the last value
        } else {
            l=$i # else store the value
        }
    }
    print # print the line
}

这可能对您有用(GNU sed):

sed -r ':a;s/^(.*\|([^|]+)\|)\|/\1\2|/;ta' file

解决方案hek2mgl的一些简化版本

awk '{for(i=1;i<NF;i++) $i=($i=="")?l:l=$i}1' FS=\| OFS=\| file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM