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 way with formatting preserved
And another way without needing the length of fields(very similar to tom feneches answer)
Output of both
This works for fixed width:
If there is something in the first column, save the value to
f
. Either way, substitute the value into the line. The1
at the end ensures that the line is printed.Testing it out:
Output:
You can use this:
However it will break the output formatting.