awk command

Hi,

I just need a awk command to get the below task.

infile-

o/p

Thanks.

If you are looking for a solution:

#!/usr/bin/ksh
mPrev2='First'
while read mInpLine
do
  mFld2=$(echo ${mInpLine} | cut -d'|' -f2)
  mFld5=$(echo ${mInpLine} | cut -d'|' -f5)
  if [[ "${mFld2}" != "${mPrev2}" ]]; then
    if [[ "${mPrev2}" != "First" ]]; then
      echo ${mOutLine}
    fi
    mPrev2=${mFld2}
    mOutLine=${mFld2}
  fi
  mOutLine=${mOutLine}'|'${mFld5}
done < input_file
if [[ "${mPrev2}" != "First" ]]; then
  echo ${mOutLine}
fi

Try this

awk -F"|" ' { a[$2]=a[$2]?a[$2] FS $5:$5 }  END { for ( i in a) print i"|" a }'

or if $2 field always exist we don't need to test that as in example above.

awk -F"|" ' { a[$2]=a[$2]FS$5 }  END { for ( i in a) print i a }'

Thanks. This works perfect.

But if you dont mind, can you pls give a brief explanation of ths command. It'll be really helpful to improve my awk knowledge.

Thanks once again!

Please refer to this post, where Franklin explain me and i have learned (Thx Franklin :slight_smile: )

I am explaining the second logic.

First we are creating array 'a' which will have index as your second field. And each time we process a row we will append the content for fifth field with corresponfing content of the array

a[$2]=a[$2]FS$5

the FS will have the field seperator '|'

and once all rows are processed we are using a for loop to print each array index and its value