# awk command

**URL:** <https://community.unix.com/t/awk-command/288013>\
**Category:** Shell Programming and Scripting\
**Created:** [May 13, 2011, 10:25am UTC](https://community.unix.com/t/awk-command/288013 "2011-05-13T10:25:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dvah](https://community.unix.com/letter_avatar/dvah/32/6_449c533f586939b6e5f246632a46421f.png) [@dvah](https://community.unix.com/u/dvah)\
**Post date:** [May 13, 2011, 10:25am UTC](https://community.unix.com/t/awk-command/288013/1 "2011-05-13T10:25:06Z")

</div>

Hi,

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

infile-

> [@](#):
>
> CWN|1110353|M|0001|AVC1|AV06  
> CWN|1110353|M|0001|AVC2|AV07  
> CWN|1110353|M|0001|AVC1|AV07  
> CWN|1110360|M|0001|AVC3|AV07  
> CWN|1110360|M|0001|AVC1|AV07  
> CWN|1110360|M|0001|AVC2|AV07  
> CWN|1110360|M|0001|AVC5|AV07

o/p

> [@](#):
>
> 1110353|AVC1|AVC2|AVC3  
> 1110360|AVC3|AVC1|AVC2|AVC5

Thanks.

---

<div class="post-metadata">

**Author:** ![Shell\_Life](https://community.unix.com/user_avatar/community.unix.com/shell_life/32/521_2.png) [@Shell\_Life](https://community.unix.com/u/Shell_Life)\
**Post date:** [May 13, 2011, 10:43am UTC](https://community.unix.com/t/awk-command/288013/2 "2011-05-13T10:43:37Z")

</div>

If you are looking for a solution:

```nohighlight
#!/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

```

---

<div class="post-metadata">

**Author:** ![Peasant](https://community.unix.com/user_avatar/community.unix.com/peasant/32/2251_2.png) [@Peasant](https://community.unix.com/u/Peasant)\
**Post date:** [May 13, 2011, 10:52am UTC](https://community.unix.com/t/awk-command/288013/3 "2011-05-13T10:52:14Z")

</div>

Try this

```nohighlight
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.

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

```

---

<div class="post-metadata">

**Author:** ![dvah](https://community.unix.com/letter_avatar/dvah/32/6_449c533f586939b6e5f246632a46421f.png) [@dvah](https://community.unix.com/u/dvah)\
**Post date:** [May 13, 2011, 11:03am UTC](https://community.unix.com/t/awk-command/288013/4 "2011-05-13T11:03:51Z")

</div>

> [@peasant](#):
>
> Try this
> 
> ```plaintext
> 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.
> 
> ```plaintext
> 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!

---

<div class="post-metadata">

**Author:** ![Peasant](https://community.unix.com/user_avatar/community.unix.com/peasant/32/2251_2.png) [@Peasant](https://community.unix.com/u/Peasant)\
**Post date:** [May 13, 2011, 11:08am UTC](https://community.unix.com/t/awk-command/288013/5 "2011-05-13T11:08:39Z")

</div>

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

> **[AWK formatting help. | Post 302516290](https://www.unix.com/302516290-post6.html)**
>
> AWK formatting help. | Post 302516290 by Franklin52 on Friday 22nd of April 2011 11:43:42 AM

---

<div class="post-metadata">

**Author:** ![kumaran\_5555](https://community.unix.com/letter_avatar/kumaran_5555/32/6_449c533f586939b6e5f246632a46421f.png) [@kumaran\_5555](https://community.unix.com/u/kumaran_5555)\
**Post date:** [May 13, 2011, 11:13am UTC](https://community.unix.com/t/awk-command/288013/6 "2011-05-13T11:13:42Z")

</div>

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

```nohighlight
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
