LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   AWK fill column from previuos line column (https://www.linuxquestions.org/questions/programming-9/awk-fill-column-from-previuos-line-column-4175447799/)

akeka 01-30-2013 01:51 AM

AWK fill column from previuos line column
 
Dear guru's

I've this input file

Code:

        1        3284510.7        2393630.5                0    1551.65893555
                                                            0    1551.65893555
        2 3284510.71875000 2392907.50000000                0    1551.65893555
                                                            0    1551.65893555
                                                  0.11904644    1551.65893555
                                                  0.16666490    1551.65893555
                                                  0.21428336    1551.65893555
                                                  0.26190186    1551.65893555
                                                  0.30952030    1551.65893555
                                                  0.35713875    1551.65893555
                                                  0.40475720    1551.65893555
        3 3284510.71875000 2392184.50000000                0    1551.65893555
                                                            0    1551.65893555
                                                  0.11904646    1551.65893555
                                                  0.16666491    1551.65893555
                                                  0.21428338    1551.65893555
                                                  0.26190186    1551.65893555

As you may aware of, I want to fill null column 1,2 and 3 from previous line that has value

Like this

Code:

        1        3284510.7        2393630.5                0    1551.65893555
        1        3284510.7        2393630.5                0    1551.65893555
        2 3284510.71875000 2392907.50000000                0    1551.65893555
        2 3284510.71875000 2392907.50000000                0    1551.65893555
        2 3284510.71875000 2392907.50000000                0.11904644    1551.65893555
        2 3284510.71875000 2392907.50000000                          0.16666490    1551.65893555
        2 3284510.71875000 2392907.50000000                            0.21428336    1551.65893555
        2 3284510.71875000 2392907.50000000                            0.26190186    1551.65893555
        2 3284510.71875000 2392907.50000000                            0.30952030    1551.65893555
        2 3284510.71875000 2392907.50000000                            0.35713875    1551.65893555
        2 3284510.71875000 2392907.50000000                                  0.40475720    1551.65893555
        3 3284510.71875000 2392184.50000000                0    1551.65893555
        3 3284510.71875000 2392184.50000000                    0    1551.65893555
        3 3284510.71875000 2392184.50000000                            0.11904646    1551.65893555
        3 3284510.71875000 2392184.50000000                            0.16666491    1551.65893555
        3 3284510.71875000 2392184.50000000                                  0.21428338    1551.65893555
        3 3284510.71875000 2392184.50000000                                  0.26190186    1551.65893555

Thank you for any help

mina86 01-30-2013 04:54 AM

Code:

NF == 5 {
        store_1 = $1
        store_2 = $2
        store_3 = $3
        print
}
NF == 2 {
        print store_1, store_2, store_3, $1, $2
}

You can pipe it through “columns -t” to get nicely aligned columns.

colucix 01-30-2013 05:18 AM

A slight variation to the code suggested by mina86 in order to preserve the original (fixed-width) format:
Code:

BEGIN {
  FIELDWIDTHS = "10 17 17 17 17"
}

$1 ~ /^ +$/ {
  print store_1 store_2 store_3 $4 $5
}

$1 ~ /[0-9]/ {
  store_1 = $1
  store_2 = $2
  store_3 = $3
  print
}


grail 01-30-2013 07:43 AM

Same fish, different fillet:
Code:

awk 'BEGIN{FIELDWIDTHS = "10 17 17 17 17"}$0 = ($1 !~ /^ +$/ && a = $1 $2 $3)?$0:a $4 $5' file

akeka 01-30-2013 07:16 PM

Thanks guru for all your help

I've solved the problem with your help

Thanks again


All times are GMT -5. The time now is 05:19 AM.