Eu sugiro o awk para este caso. Crie este arquivo (nome: filter-au.awk
):
#!/usr/bin/awk
BEGIN {
# first field on output list. Ex: FrontEnd
last_reg="xxxxxxxxxxxx";
# second field on output list. Ex: Dev,DoubleClick,master
parts="";
# number of parts in second field.
# Ex: from previous case. num_parts_on_reg=3
num_parts_on_reg=0;
# flag to signalize a new part has to be included in
# string parts
flag_part=0;
}
{
# not first line?
if ( NR > 1 ) {
# $1 field was changed? If yes, print register
if ( $1 != last_reg ) {
print last_reg ":" parts;
num_parts_on_reg=0;
parts="";
}
# fill string parts
if ( num_parts_on_reg == 0 ) {
# first part
parts=$2;
num_parts_on_reg++;
}
else {
# verify if $2 field has included in parts before
split(parts, arr_values, ",", arr_seps)
flag_part=0;
for (p in arr_values) {
if ( p == $2 ) {
flag_part=1;
break;
}
}
# if not, include $2 in parts string
if ( flag_part == 0 ) {
parts=parts "," $2
num_parts_on_reg++;
}
}
} else {
# first register
parts=$2;
num_parts_on_reg++;
}
last_reg=$1;
}
END {
# print last register
if ( num_parts_on_reg > 0 ) {
print last_reg ":" parts;
}
}
Em seguida, execute estes comandos:
grep -v 'HEAD' input.txt | awk -F "/" -f filter-au.awk
input.txt é um arquivo com esse conteúdo que você publica. Parece que funciona:
Core:Dev,master
FrontEnd:Dev,DoubleClick,master
Infrastructure:Dev,master
Software:Dev,Control,master
Se FrontEnd/HEAD -> FrontEnd/master
for interpretado como duas linhas, crie outro arquivo (nome: filter-au2.awk
):
#!/usr/bin/awk
{
if ( NF = 1 ) { print $0; }
else {
# split "->" lines in two
print $1;
print $2;
}
}
e execute estes comandos canalizados:
cat input-awk.txt | awk -F "->" -f filter-au2.awk | awk -F "/" -f filter-au.awk
a saída é:
Core:Dev,master
FrontEnd:Dev,DoubleClick,HEAD ,master
Infrastructure:Dev,master
Software:Dev,Control,master