I've run into some problems with editing a text file I've received due to some missing values in a specific column for some rows. For example, here is what the file looks like (mostly):

ID10    dad    prbM    ATGD
ID11    mom    prbF    CDSF
ID12    mom        CGDS
ID13    dad    prbF    FSDD
ID14    mom        FDSF

As you can see, sometimes the prb is not provided for every entry. I've found that when an entry is not present, there are two tab separations. Is there a way to insert a string between the tabs? For example, something like this:

ID10    dad    prbM    ATGD
ID11    mom    prbF    CDSF
ID12    mom    none    CGDS
ID13    dad    prbF    FSDD
ID14    mom    none    FDSF

With awk, assuming your fields are actually separated by tabs:

awk -F '\t' -v OFS='\t' '$3 == ""{$3 = "none"};1' input.txt


  1. Sets the input field separator to a single tab (-F '\t')
  2. Sets the output field separator to a tab (-v OFS='\t')
  3. Checks if the third field is empty ($3 == ""), and if so, sets it to the string "none"
  4. And always prints the line (1).
  • Is there a reason 'BEGIN {OFS = "\t"} wouldn't work to handle the tabs? – cosmictypist Feb 14 '16 at 23:00
  • @stellar01 is it not working? – muru Feb 14 '16 at 23:05
  • @Stephane Can you have a look at Trap ERR only works once? on SO? Thanks for the edit! – muru Feb 14 '16 at 23:07
  • It worked perfectly! – cosmictypist Feb 14 '16 at 23:07
  • @stellar01 the answer, or BEGIN {OFS="\t"}? (both should) – muru Feb 14 '16 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.