# Getting max value of specific fields with awk

**URL:** <https://community.unix.com/t/getting-max-value-of-specific-fields-with-awk/365193>\
**Category:** Shell Programming and Scripting\
**Created:** [February 23, 2017, 1:04am UTC](https://community.unix.com/t/getting-max-value-of-specific-fields-with-awk/365193 "2017-02-23T01:04:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sayami00](https://community.unix.com/letter_avatar/sayami00/32/6_449c533f586939b6e5f246632a46421f.png) [@sayami00](https://community.unix.com/u/sayami00)\
**Post date:** [February 23, 2017, 1:04am UTC](https://community.unix.com/t/getting-max-value-of-specific-fields-with-awk/365193/1 "2017-02-23T01:04:43Z")

</div>

Hello All,

Here is am trying to get maximum value of third field depending on  
first,second and fourth fields with awk command . delimeter is pipe(|) .

```nohighlight
input

0221|09|14.25|aaa
0221|09|44.27|aaa
0221|09|44.33|aaa
0221|09|44.53|bbb
0221|09|34.32|bbb
0221|09|37.13|bbb
0221|09|33.68|ccc
0221|09|32.38|ccc
0221|09|33.48|ccc

```

```nohighlight
output

0221|09|44.33|aaa
0221|09|44.53|bbb
0221|09|33.68|ccc

```

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [February 23, 2017, 2:29am UTC](https://community.unix.com/t/getting-max-value-of-specific-fields-with-awk/365193/2 "2017-02-23T02:29:09Z")

</div>

What have you tried to solve this problem?

Are all of the records that have common values for fields 1, 2, & 4 always adjacent as in your sample input file?

If the records with common values for fields 1, 2, & 4 are always adjacent, does the order of lines in the output matter?

---

<div class="post-metadata">

**Author:** ![sayami00](https://community.unix.com/letter_avatar/sayami00/32/6_449c533f586939b6e5f246632a46421f.png) [@sayami00](https://community.unix.com/u/sayami00)\
**Post date:** [February 23, 2017, 3:56am UTC](https://community.unix.com/t/getting-max-value-of-specific-fields-with-awk/365193/3 "2017-02-23T03:56:44Z")

</div>

Dear Don,  
Please find my answer :  
1) I have tried like this

```nohighlight
egrep "0221|09|ccc" test.unl | awk -F "|" 'maxvalue=="" || $3 > maxvalue { maxvalue=$3 } END { print "ccc|"maxvalue }'

```

With this I am providing unique value for fields 1,2 and 4 .

2 ) Yes , all the fields have common value .

3 ) Output order does not matter . In output , line having max third field have to be displayed .

```nohighlight
0221|09|aaa| ,44.33 

```

---

<div class="post-metadata">

**Author:** ![raj\_saini20](https://community.unix.com/letter_avatar/raj_saini20/32/6_449c533f586939b6e5f246632a46421f.png) [@raj\_saini20](https://community.unix.com/u/raj_saini20)\
**Post date:** [February 23, 2017, 5:02am UTC](https://community.unix.com/t/getting-max-value-of-specific-fields-with-awk/365193/4 "2017-02-23T05:02:08Z")

</div>

Try this Third field move to last, if required in order can be done

```nohighlight
awk -F"|" '(!a[$1"|"$2"|"$4]){a[$1"|"$2"|"$4]=$3;next}{if(a[$1"|"$2"|"$4] < $3){a[$1"|"$2"|"$4]=$3}}END{for( i in a) { print i"|"a}}' file_name

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [February 23, 2017, 5:06am UTC](https://community.unix.com/t/getting-max-value-of-specific-fields-with-awk/365193/5 "2017-02-23T05:06:15Z")

</div>

Try also

```nohighlight
awk -F"|" '{if ($3 > MX[$1 FS $2 FS $4]) MX[$1 FS $2 FS $4] = $3} END {for (m in MX) print m FS MX[m]}' file
0221|09|ccc|33.68
0221|09|aaa|44.33
0221|09|bbb|44.53

```

OR, if you want to keep the field order in the output:

```nohighlight
awk -F"|" '{if ($3 > MX[$1 FS $2 FS FS $4]) MX[$1 FS $2 FS FS $4] = $3} END {for (m in MX) {sub ("\|\|", FS MX[m] FS, m); print m}}' file
0221|09|33.68|ccc
0221|09|44.33|aaa
0221|09|44.53|bbb

```

---

<div class="post-metadata">

**Author:** ![sayami00](https://community.unix.com/letter_avatar/sayami00/32/6_449c533f586939b6e5f246632a46421f.png) [@sayami00](https://community.unix.com/u/sayami00)\
**Post date:** [February 23, 2017, 5:43am UTC](https://community.unix.com/t/getting-max-value-of-specific-fields-with-awk/365193/6 "2017-02-23T05:43:05Z")

</div>

Dear RudiC and Raj ,

Thanks a lot . Both code are working for me .
