I am trying to modify a tab delimited file by replacing the value of 2 columns separated by ":" where there is a null value in one column:
Start with this file:
id1 id2 pos1 pos2
749 2847443 21 13517135
862 2821796 21 13571669
997 21 13636494
1095 2821826 21 13661335
1131 21 13678797
I would like to modify column 2 to get this:
id1 id2 pos1 pos2
749 2847443 21 13517135
862 2821796 21 13571669
997 21:13636494 21 13636494
1095 2821826 21 13661335
1131 21:13678797 21 13678797
The problem is also that there is no blank space (" ") indicating a null value. For now I have not even been able to substitute the value of column 2 with column 3 when it is null...
I have tried using sed:
sed -e 's/\t\t/\t$3\t/g' input.txt > output
Which works but it just substitutes the text '$3' and I can't find how to substitute the values of $3:$4 instead.
I have also tried awk:
awk 'BEGIN {
IFS = OFS = "\t"
}
{
for (column = 2; column <= NF; ++column) {
if ($column == "") {
$column = $3
}
}
print
}
'
input.txt > output
But this does not work either (it does nothing actually neither using "" nor " "...)
Can you please help me? Thank you.