Fill data in column with previous value

Gents,

Kindly help me.

I have a file with empty values in selected column, I will like to fill the empty values with the previous value.

Example

Input file

X  4959     30010  66727.00  20457.001    1  1441  66512.00  20234.00  20520.001
X  4959     30010  66727.00  20457.001  145  2961  66524.00  20234.00  20536.001
X  4959     30010  66727.00  20457.001  297  4461  66536.00  20234.00  20532.001
X           30010  66727.00  20457.001  447  6021  66548.00  20234.00  20544.001
X           30010  66727.00  20457.001  603  7621  66560.00  20234.00  20552.001
X           30010  66727.00  20457.001  763  9261  66572.00  20234.00  20560.001
X  4959     30010  66727.00  20457.001  927 10911  66584.00  20234.00  20562.001
X  4959     30010  66727.00  20457.001 1092 12591  66596.00  20234.00  20568.001
X  4959     30010  66727.00  20457.001 1260 14271  66608.00  20234.00  20568.001
X  4959     30010  66727.00  20457.001 1428 15971  66620.00  20234.00  20572.001
X  4960     30010  66727.00  20457.001 1598 17561  66632.00  20234.00  20550.001
X  4960     30010  66727.00  20457.001 1757 19311  66644.00  20234.00  20582.001
X  4960     30010  66727.00  20457.001 1932 21071  66656.00  20234.00  20584.001
X           30010  66727.00  20457.001 2108 22861  66668.00  20234.00  20590.001
X           30010  66727.00  20457.001 2287 24661  66680.00  20234.00  20592.001
X           30010  66727.00  20457.001 2467 26421  66692.00  20234.00  20584.001
X  4970     30010  66727.00  20457.001 2643 28161  66704.00  20234.00  20580.001
X  4970     30010  66727.00  20457.001 2817 30001  66716.00  20234.00  20600.001
X  4970     30110  66511.00  20273.001    1  2241  66512.00  20050.00  20496.001
X  4970     30110  66511.00  20273.001  225  4481  66524.00  20050.00  20496.001
X           30110  66511.00  20273.001  449  6721  66536.00  20050.00  20496.001
X           30110  66511.00  20273.001  673  8961  66548.00  20050.00  20496.001

Output desired

X  4959     30010  66727.00  20457.001  145  2961  66524.00  20234.00  20536.001
X  4959     30010  66727.00  20457.001  297  4461  66536.00  20234.00  20532.001
X  4959     30010  66727.00  20457.001  447  6021  66548.00  20234.00  20544.001
X  4959     30010  66727.00  20457.001  603  7621  66560.00  20234.00  20552.001
X  4959     30010  66727.00  20457.001  763  9261  66572.00  20234.00  20560.001
X  4959     30010  66727.00  20457.001  927 10911  66584.00  20234.00  20562.001
X  4959     30010  66727.00  20457.001 1092 12591  66596.00  20234.00  20568.001
X  4959     30010  66727.00  20457.001 1260 14271  66608.00  20234.00  20568.001
X  4959     30010  66727.00  20457.001 1428 15971  66620.00  20234.00  20572.001
X  4960     30010  66727.00  20457.001 1598 17561  66632.00  20234.00  20550.001
X  4960     30010  66727.00  20457.001 1757 19311  66644.00  20234.00  20582.001
X  4960     30010  66727.00  20457.001 1932 21071  66656.00  20234.00  20584.001
X  4960     30010  66727.00  20457.001 2108 22861  66668.00  20234.00  20590.001
X  4960     30010  66727.00  20457.001 2287 24661  66680.00  20234.00  20592.001
X  4960     30010  66727.00  20457.001 2467 26421  66692.00  20234.00  20584.001
X  4970     30010  66727.00  20457.001 2643 28161  66704.00  20234.00  20580.001
X  4970     30010  66727.00  20457.001 2817 30001  66716.00  20234.00  20600.001
X  4970     30110  66511.00  20273.001    1  2241  66512.00  20050.00  20496.001
X  4970     30110  66511.00  20273.001  225  4481  66524.00  20050.00  20496.001
X  4970     30110  66511.00  20273.001  449  6721  66536.00  20050.00  20496.001
X  4970     30110  66511.00  20273.001  673  8961  66548.00  20050.00  20496.001

Thanks for your help.

Try:

perl -pe '$x=$1 if /^X  (\S+)/;s/^X {6}/X  $x/' file

Hello jiam912,

Following may also help but I didn't test it as don't have system now.:slight_smile:

awk '{if($2 == ""){$2=A}} {A=$2} 1' Input_file

Thanks,
R. Singh

Here is an awk solution:

$ awk '{if (substr($0,4,4)=="    ") {print substr($0,1,2),save,substr($0,9);} else {save=substr($0,4,4); print $0}}' file.txt
X  4959     30010  66727.00  20457.001    1  1441  66512.00  20234.00  20520.001
X  4959     30010  66727.00  20457.001  145  2961  66524.00  20234.00  20536.001
X  4959     30010  66727.00  20457.001  297  4461  66536.00  20234.00  20532.001
X  4959     30010  66727.00  20457.001  447  6021  66548.00  20234.00  20544.001
X  4959     30010  66727.00  20457.001  603  7621  66560.00  20234.00  20552.001
X  4959     30010  66727.00  20457.001  763  9261  66572.00  20234.00  20560.001
X  4959     30010  66727.00  20457.001  927 10911  66584.00  20234.00  20562.001
X  4959     30010  66727.00  20457.001 1092 12591  66596.00  20234.00  20568.001
X  4959     30010  66727.00  20457.001 1260 14271  66608.00  20234.00  20568.001
X  4959     30010  66727.00  20457.001 1428 15971  66620.00  20234.00  20572.001
X  4960     30010  66727.00  20457.001 1598 17561  66632.00  20234.00  20550.001
X  4960     30010  66727.00  20457.001 1757 19311  66644.00  20234.00  20582.001
X  4960     30010  66727.00  20457.001 1932 21071  66656.00  20234.00  20584.001
X  4960     30010  66727.00  20457.001 2108 22861  66668.00  20234.00  20590.001
X  4960     30010  66727.00  20457.001 2287 24661  66680.00  20234.00  20592.001
X  4960     30010  66727.00  20457.001 2467 26421  66692.00  20234.00  20584.001
X  4970     30010  66727.00  20457.001 2643 28161  66704.00  20234.00  20580.001
X  4970     30010  66727.00  20457.001 2817 30001  66716.00  20234.00  20600.001
X  4970     30110  66511.00  20273.001    1  2241  66512.00  20050.00  20496.001
X  4970     30110  66511.00  20273.001  225  4481  66524.00  20050.00  20496.001
X  4970     30110  66511.00  20273.001  449  6721  66536.00  20050.00  20496.001
X  4970     30110  66511.00  20273.001  673  8961  66548.00  20050.00  20496.001
awk '{if($2 == ""){$2=A}} {A=$2} 1' Input_file

RavinderSingh13's awk solution above does not appear to work as is since $2 will be valued from a different column (e.g 30010)when the expected field is missing (as file is column delimited).

GNU awk:

gawk '$2~/^ *$/{$2=p}{p=$2}1' OFS= FIELDWIDTHS="2 5 999" file
1 Like

Dear Scrutinizer

Can you explain me a litte about

OFS= FIELDWIDTHS="2 5 999"

Thanks

---------- Post updated at 03:47 AM ---------- Previous update was at 03:47 AM ----------

Dear Scrutinizer

Can you explain me a litte about

OFS= FIELDWIDTHS="2 5 999"

Thanks

OFS= output field separator, it determines which characters are put between fields in the output, in this case it is the empty string "" , so no characters. The OFS is inserted between fields, once a record is recomputed, which is the case when one of the fields is modified.

FIELDWIDTHS="2 5 999" is a GNU-only extension to awk, that determines that field 1 consists of the first two characters, field 2 the next 5 and field 3 the rest of the characters..

1 Like

Dear Scrutinizer,

Great, thanks for the explanation