Delete and insert columns in a tab delimited file

Hi all ,

I have a file having 12 columns tab delimited .

I need to read this file and remove the column 3 and column 4 and insert a word in column 3 as "AVIALABLE "

Is there a way to do this . I am trying like below

Thanks
DJ

 
cat $FILENAME|awk -F"\t" '{ print $1 "\t $2  "AVAILABLE""\t" $5 "\t" $6 "\t" $7 "\t" $8 "\t" $9 "\t" $10 "\t" $11 "\t" $12 "\t"}'>>$FILENAME.tmp

Hello Hypesslearner,

Could you please try following and let me know if this helps.

awk '{$3="AVIALABLE";$4="\b"} 1' OFS="\t" Input_file

Thanks,
R. Singh

Yes. There are several ways to delete two tab delimited fields from a file and modify the text in another field to contain the characters "AVIALABLE " (sic). Do you want to add the new text to the start of the old 5th field, to add the new text to the end of the old 5th field, or to replace the contents of the old 5th field with "AVIALABLE " ?

Why do you want to use something other than awk for this? Is this a homework assignment that does not allow use of awk ? (Oops, ignore that. I see that you have changed your requirements.)

Do you want to add AVAILABLE to the end of the 2nd field as in your code, or add "AVAILABLE " as requested in your description to what was in field 5 before you deleted fields 3 and 4?

Hi Ravinder,
Note that setting the 4th field to a backspace character does not remove the 4th field. Note also that if any of the fields contain spaces between words within the field, you need to set FS to tab as well as setting OFS to tab to get the proper output. For example, the command:

echo "1 2 3 4 5 6"|awk '{$3="AVIALABLE";$4="\b"} 1' OFS="\t" |od -bc

produces the output:

0000000   061 011 062 011 101 126 111 101 114 101 102 114 105 011 010 011
           1  \t   2  \t   A   V   I   A   L   A   B   L   E  \t  \b  \t
0000020   065 011 066 012                                                
           5  \t   6  \n                                                
0000024

My interpretation of the request to "remove the column 3 and column 4 and insert a word in column 3" is different than yours. I thought it meant to delete fields 3 and 4 and modify the new 3rd field. You thought it meant to replace field 3 and delete field 4. And the code provided by the OP, added text to the end of the 2nd field, deleted the 3rd and 4th fields, and added a new empty field to the end of each line.

Until we get an unambiguous description of the desired processing, I don't know which interpretation is correct.

1 Like