how to add a new column in an existing file

Hi guys,

Please help me if u have some solution.

I have a file with three columns separated by ':' -
INPUT_FILE
C416722_2 : calin Dirigent : Dirigent
AC4174_6 : Jac : cal_co
TC4260_5 : [no : lin kite
BC426302_1 : [no : calin Dirigent lin
JC426540_3 : lin Pymo_bin : calin
TC428_3 : no7 cal_co : no7 cal_co

I need the output such that It contain four columns and column4 should contain combined words of column2 and column3 but uniq.
OUTPUT_FILE
C416722_2 : calin Dirigent : Dirigent : calin Dirigent
AC4174_6 : Jac : cal_co : Jac cal_co
TC4260_5 : [no : lin kite : [no lin kite
BC426302_1 : lin : calin Dirigent lin : calin Dirigent lin
JC426540_3 : lin Pymo_bin : calin : lin Pymo_bin calin
TC428_3 : no7 cal_co : no7 cal_co : no7 cal_co

Thanks in advance :slight_smile:

Try this:

awk ' BEGIN { FS=" : "; OFS=" : " } { if (index($2, $3) == 0) if (index($3, $2) == 0) column4 = $2 " " $3; else column4 = $3; else column4 = $2; print $0 " : " column4 }' foo

Thanks angheloko :slight_smile: . Its working perfect.

below may help you a little, only issue is that the last column is not in original order, if not ok, you need to change it accordingly.

#!/usr/bin/perl
open FH,"<a.txt";
while(<FH>){
	chomp;
	my @tmp=split(" : ",$_);
	map {$hash{$_}++} split(" ",$tmp[1]);
	map {$hash{$_}++} split(" ",$tmp[2]);
	print $_," : ",join " ",keys %hash;
	print "\n";
	undef %hash;
}
close FH;

Thanks summer_cherry. Order is not an issue.