i have a text file as belows, it includes 2 columns, 1st is the column name, 2nd is the file_name
data_file.txt
column_name file_name
col1 file1
col2 file2
col3 file1
col4 file1
col5 file2
now, i would like to output below result to a text file, which returns all the column name by file name as below.
file_name column_name
file1 col1,col3,col4
file2 col2,col5
how can i write a shell script to read the data_file.txt line by line and output the result as above by file name.
Aia
November 5, 2017, 12:05pm
2
perl -lane 'if($. == 1){ print "$F[1] $F[0]" and next } push @{$f{$F[1]}}, $F[0]; END{ $"=","; for(sort keys %f){print "$_ @{$f{$_}}"}}' data_file.txt
file_name column_name
file1 col1,col3,col4
file2 col2,col5
aia:
perl -lane 'if($. == 1){ print "$F[1] $F[0]" and next } push @{$f{$F[1]}}, $F[0]; END{ $"=","; for(sort keys %f){print "$_ @{$f{$_}}"}}' data_file.txt
file_name column_name
file1 col1,col3,col4
file2 col2,col5
my data_file is changing.. how can I transform to return file_name per line and concat all the column_name to that file, so that each line has 1 file name.
Aia
November 5, 2017, 12:12pm
4
I am sorry for not understanding. Is not that what it does, already? Please, post another, more exact, example if that's not what you want.
RudiC
November 5, 2017, 12:59pm
5
How about
{ head -1 ; tail -n+1 | sort -k2 -k1.1; } < file2 | awk '
$2 != L2 {printf "%c%s ", LF, $2
LF = "\n"
DL = ""
L2 = $2
}
{printf "%c%s", DL, $1
DL = ","
}
END {print ""
}
'
file_name column_name
file1 col1,col3,col4
file2 col2,col5