awk to Fill Empty Column value with Previous Non-E

2020-04-11 14:05发布

Would like to read the first column then Fill downward Empty Column value with Previous Non-Empty Column value.

Input.txt

20                        0 ABC          1   N   DEFABC       0     CHARGE      
                          1 ABC          1   N   GHIABC       0     CHARGE      
                          2 ABC          1   N   JKLABC       0     CHARGE      
                          3 ABC          1   N   MNOABC       0     CHARGE      
                          4 ABC          1   N   PQRABC       0     CHARGE      
210&&-2                   0 ABC          1   N   DEFABC       0     CHARGE      
                          1 ABC          1   N   GHIABC       0     CHARGE      
                          2 ABC          1   N   JKLABC       0     CHARGE      
                          3 ABC          1   N   MNOABC       0     CHARGE      
                          4 ABC          1   N   PQRABC       0     CHARGE      
2130&&-4&-6&&-9           0 ABC          1   N   DEFABC       0     CHARGE      
                          1 ABC          1   N   GHIABC       0     CHARGE      
                          2 ABC          1   N   JKLABC       0     CHARGE      
                          3 ABC          1   N   MNOABC       0     CHARGE      
                          4 ABC          1   N   PQRABC       0     CHARGE 

Have tried below command script and it is working fine if the file separted "," de-limiter and it is not working for FS="" and FS ="\t" for the above sample input.

$ awk -f FillEmpty.awk Input.txt

$ cat FillEmpty.awk

BEGIN { FS = "" }

$1 != "" { print }

$1 == "" {
        # fill in blanks
        for (i = 1; i <= NR; i++)
                if ($i == "")
                        $i = Saved[i]

        print
}

{
        # save all fields
        for (i = 1; i <= NR; i++)
                Saved[i] = $i
}

Desired Output:

20                        0 ABC          1   N   DEFABC       0     CHARGE      
20                        1 ABC          1   N   GHIABC       0     CHARGE      
20                        2 ABC          1   N   JKLABC       0     CHARGE      
20                        3 ABC          1   N   MNOABC       0     CHARGE      
20                        4 ABC          1   N   PQRABC       0     CHARGE      
210&&-2                   0 ABC          1   N   DEFABC       0     CHARGE      
210&&-2                   1 ABC          1   N   GHIABC       0     CHARGE      
210&&-2                   2 ABC          1   N   JKLABC       0     CHARGE      
210&&-2                   3 ABC          1   N   MNOABC       0     CHARGE      
210&&-2                   4 ABC          1   N   PQRABC       0     CHARGE      
2130&&-4&-6&&-9           0 ABC          1   N   DEFABC       0     CHARGE      
2130&&-4&-6&&-9           1 ABC          1   N   GHIABC       0     CHARGE      
2130&&-4&-6&&-9           2 ABC          1   N   JKLABC       0     CHARGE      
2130&&-4&-6&&-9           3 ABC          1   N   MNOABC       0     CHARGE      
2130&&-4&-6&&-9           4 ABC          1   N   PQRABC       0     CHARGE

Any suggestions ...!

标签: awk
4条回答
▲ chillily
2楼-- · 2020-04-11 14:28

Awk way with formatting preserved

awk '/^ /{$0=(x)substr($0,21)}{x=substr($0,0,20)}1' file

And another way without needing the length of fields(very similar to tom feneches answer)

awk '/^ /{$0=(x)substr($0,length(x)+1)}{x=$1}1' file

Output of both

20                        0 ABC          1   N   DEFABC       0     CHARGE
20                        1 ABC          1   N   GHIABC       0     CHARGE
20                        2 ABC          1   N   JKLABC       0     CHARGE
20                        3 ABC          1   N   MNOABC       0     CHARGE
20                        4 ABC          1   N   PQRABC       0     CHARGE
210&&-2                   0 ABC          1   N   DEFABC       0     CHARGE
210&&-2                   1 ABC          1   N   GHIABC       0     CHARGE
210&&-2                   2 ABC          1   N   JKLABC       0     CHARGE
210&&-2                   3 ABC          1   N   MNOABC       0     CHARGE
210&&-2                   4 ABC          1   N   PQRABC       0     CHARGE
2130&&-4&-6&&-9           0 ABC          1   N   DEFABC       0     CHARGE
2130&&-4&-6&&-9           1 ABC          1   N   GHIABC       0     CHARGE
2130&&-4&-6&&-9           2 ABC          1   N   JKLABC       0     CHARGE
2130&&-4&-6&&-9           3 ABC          1   N   MNOABC       0     CHARGE
2130&&-4&-6&&-9           4 ABC          1   N   PQRABC       0     CHARGE
查看更多
The star\"
3楼-- · 2020-04-11 14:31

This works for fixed width:

awk 'substr($0,0,24) ~ $1 { f=$1 }{ $0=f substr($0, length(f)+1) } 1' file

If there is something in the first column, save the value to f. Either way, substitute the value into the line. The 1 at the end ensures that the line is printed.

Testing it out:

$ awk 'substr($0,0,24) ~ $1 { f=$1 }{ $0=f substr($0, length(f)+1) } 1' file
20                        0 ABC          1   N   DEFABC       0     CHARGE      
20                        1 ABC          1   N   GHIABC       0     CHARGE      
20                        2 ABC          1   N   JKLABC       0     CHARGE      
20                        3 ABC          1   N   MNOABC       0     CHARGE      
20                        4 ABC          1   N   PQRABC       0     CHARGE      
210&&-2                   0 ABC          1   N   DEFABC       0     CHARGE      
210&&-2                   1 ABC          1   N   GHIABC       0     CHARGE      
210&&-2                   2 ABC          1   N   JKLABC       0     CHARGE      
210&&-2                   3 ABC          1   N   MNOABC       0     CHARGE      
210&&-2                   4 ABC          1   N   PQRABC       0     CHARGE      
2130&&-4&-6&&-9           0 ABC          1   N   DEFABC       0     CHARGE      
2130&&-4&-6&&-9           1 ABC          1   N   GHIABC       0     CHARGE      
2130&&-4&-6&&-9           2 ABC          1   N   JKLABC       0     CHARGE      
2130&&-4&-6&&-9           3 ABC          1   N   MNOABC       0     CHARGE      
2130&&-4&-6&&-9           4 ABC          1   N   PQRABC       0     CHARGE
查看更多
姐就是有狂的资本
4楼-- · 2020-04-11 14:39
awk '/^[ \t]/ { sub(/^[ \t]+/, ""); print t $0; next }
    { match($0, /^[^ \t]+[ \t]+/); t = substr($0, RSTART, RLENGTH) }
    1' file

Output:

20                        0 ABC          1   N   DEFABC       0     CHARGE      
20                        1 ABC          1   N   GHIABC       0     CHARGE      
20                        2 ABC          1   N   JKLABC       0     CHARGE      
20                        3 ABC          1   N   MNOABC       0     CHARGE      
20                        4 ABC          1   N   PQRABC       0     CHARGE      
210&&-2                   0 ABC          1   N   DEFABC       0     CHARGE      
210&&-2                   1 ABC          1   N   GHIABC       0     CHARGE      
210&&-2                   2 ABC          1   N   JKLABC       0     CHARGE      
210&&-2                   3 ABC          1   N   MNOABC       0     CHARGE      
210&&-2                   4 ABC          1   N   PQRABC       0     CHARGE      
2130&&-4&-6&&-9           0 ABC          1   N   DEFABC       0     CHARGE      
2130&&-4&-6&&-9           1 ABC          1   N   GHIABC       0     CHARGE      
2130&&-4&-6&&-9           2 ABC          1   N   JKLABC       0     CHARGE      
2130&&-4&-6&&-9           3 ABC          1   N   MNOABC       0     CHARGE      
2130&&-4&-6&&-9           4 ABC          1   N   PQRABC       0     CHARGE      
查看更多
姐就是有狂的资本
5楼-- · 2020-04-11 14:45

You can use this:

awk 'NF==8{m=$1}NF!=8{$0=m$0}1' the.file

However it will break the output formatting.

查看更多
登录 后发表回答