# AWK formatting help.

**URL:** <https://community.unix.com/t/awk-formatting-help/286800>\
**Category:** Shell Programming and Scripting\
**Created:** [April 22, 2011, 9:41am UTC](https://community.unix.com/t/awk-formatting-help/286800 "2011-04-22T09:41:50Z")\
**Posts on this page:** 6\
**Page:** 1

<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:** [April 22, 2011, 9:41am UTC](https://community.unix.com/t/awk-formatting-help/286800/1 "2011-04-22T09:41:50Z")

</div>

Dear all

I require help with AWK regarding this situation

Input is :

```nohighlight
fn1 12345
fn1 23456
fn3 231513
fn1 22325
fn3 123125

```

Desired output is

```nohighlight
fn1 12345 23456 22325
fn3 231513 123125

```

Of course to be taken in mind that fn(n) can be anywhere but always $1

I have some messy shell code to achieve this, but it's not worth pasting.

Thank you for your assistance.  
Best regards  
Peasant.

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [April 22, 2011, 9:46am UTC](https://community.unix.com/t/awk-formatting-help/286800/2 "2011-04-22T09:46:57Z")

</div>

```nohighlight
awk '{a[$1]=a[$1]?a[$1] FS $2:$2}END {for(i in a)print i, a}' file

```

---

<div class="post-metadata">

**Author:** ![kevintse](https://community.unix.com/user_avatar/community.unix.com/kevintse/32/1921_2.png) [@kevintse](https://community.unix.com/u/kevintse)\
**Post date:** [April 22, 2011, 9:50am UTC](https://community.unix.com/t/awk-formatting-help/286800/3 "2011-04-22T09:50:40Z")

</div>

```nohighlight
awk '$1 in a{a[$1]=a[$1] FS $2; next} {a[$1]=$2} END{for(e in a) print e, a[e]}' infile

```

---

<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:** [April 22, 2011, 11:06am UTC](https://community.unix.com/t/awk-formatting-help/286800/4 "2011-04-22T11:06:32Z")

</div>

Thanks folks.

Since i'm still in process of mastering AWK, some hints would be very useful (explanation of code execution)  
Documentation online is ok, but it really lacks real life examples elaborated (like this).

Regards  
Peasant.

---

<div class="post-metadata">

**Author:** ![kurumi](https://community.unix.com/letter_avatar/kurumi/32/5_5575768a8748004e209b776fc1b2916d.png) [@kurumi](https://community.unix.com/u/kurumi)\
**Post date:** [April 22, 2011, 11:18am UTC](https://community.unix.com/t/awk-formatting-help/286800/5 "2011-04-22T11:18:54Z")

</div>

```nohighlight
$ ruby -ane 'BEGIN{h={}};(h[$F[0]]||=[])<<$F[1];END{h.each{|x,y|puts "#{x} #{y.join(" ")}"}}' file
fn1 12345 23456 22325
fn3 231513 123125

```

Create a hash, store first field as key and second field as value if key is found, otherwise, initialize the value an empty array. At the end, print the hash.

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [April 22, 2011, 11:43am UTC](https://community.unix.com/t/awk-formatting-help/286800/6 "2011-04-22T11:43:42Z")

</div>

> [@peasant](#):
>
> Thanks folks.
> 
> Since i'm still in process of mastering AWK, some hints would be very useful (explanation of code execution)  
> Documentation online is ok, but it really lacks real life examples elaborated (like this).
> 
> Regards  
> Peasant.

```plaintext
awk '{a[$1]=a[$1]?a[$1] FS $2:$2}END {for(i in a)print i, a}' file

```

Here a value is assigned to the array element a[$1] with a condition expression operator:

```plaintext
a[$1]=a[$1]?a[$1] FS $2:$2

```

Explanation:

```plaintext
a[$1]? -> if a[$1] has a value then 
  a[$1]=a[$1] FS $2
else 
  a[$1]=$2

```
