Greetings!
I have a text file that I am trying to process to get the desired output but looks like I will need the community help.
Input File:
a|x|london|consumer|consumer1|country||D|consumer|consumer1|country||1
a|x|paris|consumer|consumer2|country||D|consumer1|consumer2|country||2
a|x|paris|consumer3|consumer2|country||D|consumer1|consumer2|country||2
a|x|spain|id|sys|id|country|U|consumer|sys|id|country|3
b|x|spain|hash|sys|id|country|U|consumer|sys|id|country|3
b|x|spain|tkn|sys|id|country|U|consumer|sys|id|country|3
b|x|spain|txt|sys|id|country|U|consumer|sys|id|country|3
I need to read a file similar to the above example and get the following output. the output needs to be broken down by the number we see at the end of each line. Same numbered lines needs to be grouped.
Output:
a|x|london|consumer=consumer|consumer1=consumer1 and country=country|1
a|x|paris|consumer=consumer1, consumer3=consumer1|consumer2 = consumer2 and country=country|2
a|x|spain|id=consumer, hash=consumer,tkn=consumer,txt=consumer|sys=sys and id=id and country=country|3
In other terms.
for each number grouping in the file we do this
$1|$2|$3|$4=$9[,$4=$9 if we have more than one line]|$5=$10[and $6=$11 and $7=$12 if they have some value in their fields]| $13
Here is what I tried..
key=$(head -1 filename.out | awk -F'|' '{ print $1"|"$2"|"$3 }')
value1=""
value2=""
while read p; do
anon1=$( echo $p | awk -F'|' '{ print $4"="$9 }')
if [ "$value1" != "" ]; then value1="${value1}, ${anon1}"; else value1="${anon1}"; fi
anon2=$( echo $p | awk -F'|' '{ print $5"="$10" and "$6"="$11" and "$7"="$12 }')
done < filename.out
if [ "$value2" != "" ]; then value2="${value2} and ${anon2}"; else value2="${anon2}"; fi
echo $key"|"$value1"|"$value2
the above code works but i want to loop this through the rest of the file where we have lines broken down by numbered values like the input shown above in the example. if i run the above code to the above input it combines all the lines which is not the desired output. it should only combine the lines that share the same numbered value at the end of each line.
Thank you very much.