shell - AWK replace null column with the column value of next row -


i have following sample file missing column values replace next available value same column.

cat test.txt 11.2.0.1,ora1,oracle 11.2.0.4,ora2,oracle 11.2.0.3,ora3,oracle 12.2.0.1,ora4,oracle ,ora5,oracle ,ora6,oracle 12.2.0.2,ora7,oracle ,mys1,mysql 5.1,mys2,mysql 

here trying do:

 cat test.txt |awk '{printf("%s,%s,%s\n", $11,$3,$1);}'|awk -f',' 'begin{ofs=","}    {         (i=1; i<=nf; i++)         if ($i=="")             --read next column value             --if next column null, read futhur next             -- assign next available value $i         print     }' 

expected output:

11.2.0.1,ora1,oracle 11.2.0.4,ora2,oracle 11.2.0.3,ora3,oracle 12.2.0.1,ora4,oracle 12.2.0.2,ora5,oracle 12.2.0.2,ora6,oracle 12.2.0.2,ora7,oracle 5.1,mys1,mysql 5.1,mys2,mysql 

thanks

you can this:

awk -f, '$1==""{a[n++]=$0;next} n{for (i=0;i<n;i++) print $1 a[i]; n=0} 1' file 

details:

$1=="" {          # if first field empty     a[n++]=$0     # store whole line @ index n , increment n     next          # jump next line }  n {                    # if n isn't 0     (i=0;i<n;i++)  # loop on stored lines indexes         print $1 a[i]  # , print lines starting current first field     n=0                # set n 0 }  1  # true, print current line 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -