Question

if got a file, with lines in the following format:

SOME_ATTRIBUTE_1 XYZ; IMPORTANT_ATTRIBUTE_1 1234; SOME_ATTRIBUTE_2 XYZ; IMPORTANT_ATTRIBUTE_2 AB;

Now I want to transform this into the following form, that the two important attribute values produce a new attribute:

JOIN_IMPORTANT_ATTRIBUTE AB1234; SOME_ATTRIBUTE_1 XYZ; IMPORTANT_ATTRIBUTE_1 1234; SOME_ATTRIBUTE_2 XYZ; IMPORTANT_ATTRIBUTE_2 AB;

Can this be done with some one-liner with awk or similar? I've got no idea how to tacle this, without grabbing into the java trickbox.

Was it helpful?

Solution

awk -F'[; ]+' '{print "JOIN_IMPORTANT_ATTRIBUTE", $8 $4 "; " $0}' file

OTHER TIPS

With awk you can split the input at semi-colon + any number of spaces and further split the important fields like this:

awk -F'; *' '{ split($2, a1, / +/); split($4, a2, / +/); print "JOIN_IMPORTANT_ATTRIBUTE", a2[2] a1[2] ";", $0 }' infile 

Output:

JOIN_IMPORTANT_ATTRIBUTE AB1234; SOME_ATTRIBUTE_1 XYZ; IMPORTANT_ATTRIBUTE_1 1234; SOME_ATTRIBUTE_2 XYZ; IMPORTANT_ATTRIBUTE_2 AB;

This assumes you know which columns the important attributes are in.

Perl solution:

perl -lane 'print join " ", "JOIN_IMPORTANT_ATTRIBUTE", substr($F[7], 0, -1) . $F[3], @F' 

This is my bash+awk alternative.

cat attrs.awk
# Awk script to get joined attributes for one line of attributes

BEGIN {
RS=";";     
PROCINFO["sorted_in"]="@ind_num_asc"; #gawk only: sort attributes on their attr id (so that IMPORTANT_ATTRIBUTE_n comes before IMPORTANT_ATTRIBUTE_n+1
}

$1 ~ /^IMPORTANT_ATTRIBUTE_/ {
            attrId=substr($1, 1 + length("IMPORTANT_ATTRIBUTE_"));
    if ($2 ~ /^[0-9]/) 
            impAttrsNum[attrId]=$2;
    else
            impAttrsAlpha[attrId]=$2;
}

END {
    #alpha attribs come before num attribs
    for(i in impAttrsAlpha)
            alphaVals = alphaVals impAttrsAlpha[i];
    for(i in impAttrsNum)
            numVals = numVals impAttrsNum[i];

    printf("JOIN_IMPORTANT_ATTRIBUTE %s%s%s", alphaVals, numVals, RS);
}

cat joinattrs
#!/bin/bash
#
# Applies joined attributes for each input line

while read l
do
    if [[ -n "$l" ]]
    then   
            joinAttrs=$(echo "$l" | awk -f attrs.awk)
            echo "$joinAttrs $l"
    fi
done  

How to use it: ./joinattrs < datafile

Not a one-liner :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top