Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
743 views
in Technique[技术] by (71.8m points)

awk to Fill Empty Column value with Previous Non-Empty Column value:

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 ="" 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 ...!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...