# Sort data in text file in particular format

**URL:** <https://community.unix.com/t/sort-data-in-text-file-in-particular-format/367978>\
**Category:** Shell Programming and Scripting\
**Created:** [August 21, 2017, 8:44am UTC](https://community.unix.com/t/sort-data-in-text-file-in-particular-format/367978 "2017-08-21T08:44:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adfire](https://community.unix.com/letter_avatar/adfire/32/6_449c533f586939b6e5f246632a46421f.png) [@Adfire](https://community.unix.com/u/Adfire)\
**Post date:** [August 21, 2017, 8:44am UTC](https://community.unix.com/t/sort-data-in-text-file-in-particular-format/367978/1 "2017-08-21T08:44:18Z")

</div>

I have to sort below output in text file in unix bash

```nohighlight
20170308
DA,I,113
20170308
PM,I,123
20170308
DA,U,22
20170308
PM,U,123
20170309
DA,I,11
20170309
PM,I,23
20170309
DA,U,123
20170309
PM,U,233

```

New format should be like below values may change sequence of name will remain same

```nohighlight
20170308
PM,I,123
PM,U,123
DA,I,113
DA,U,22
20170309
PM,I,23
PM,U,233
DA,I,11
DA,U,123

```

---

<div class="post-metadata">

**Author:** ![rdrtx1](https://community.unix.com/user_avatar/community.unix.com/rdrtx1/32/2721_2.png) [@rdrtx1](https://community.unix.com/u/rdrtx1)\
**Post date:** [August 21, 2017, 10:39am UTC](https://community.unix.com/t/sort-data-in-text-file-in-particular-format/367978/2 "2017-08-21T10:39:16Z")

</div>

maybe:

```nohighlight
awk '/,/ || !a[$0]++' infile

```

---

<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:** [August 21, 2017, 1:44pm UTC](https://community.unix.com/t/sort-data-in-text-file-in-particular-format/367978/3 "2017-08-21T13:44:50Z")

</div>

Wouldn't it be nice to tell people the sort criteria?

---

<div class="post-metadata">

**Author:** ![RavinderSingh13](https://community.unix.com/letter_avatar/ravindersingh13/32/6_449c533f586939b6e5f246632a46421f.png) [@RavinderSingh13](https://community.unix.com/u/RavinderSingh13)\
**Post date:** [August 21, 2017, 4:16pm UTC](https://community.unix.com/t/sort-data-in-text-file-in-particular-format/367978/4 "2017-08-21T16:16:35Z")

</div>

Hello Adfire,

As RudiC already mentioned criteria is not mentioned of sorting, so as per your shown first part of output for `20170308 ` I am considering that we have to sort by 3rd field of digits for each digit ids(or `yyyymmdd` ). If your Input\_file is same as sample shown then following may help you in same.

```nohighlight
awk '/^[0-9]/{val=$0;next} {a[val]=a[val]?a[val] ORS $0:$0} END{for(i in a){print i;system("echo " s1 a s1 " | sort -t, -k3nr")}}' s1="\"" Input_file

```

Output will be as follows.

```nohighlight
20170308
PM,I,123
PM,U,123
DA,I,113
DA,U,22
20170309
PM,U,233
DA,U,123
PM,I,23
DA,I,11

```

EDIT: Adding a non-one liner form of solution too here.

```nohighlight
awk '/^[0-9]/{
val=$0;
next
}
{
  a[val]=a[val]?a[val] ORS $0:$0
}
END{
  for(i in a){
    print i;system("echo " s1 a s1 " | sort -t, -k3nr")
}
}
' s1="\"" Input_file

```

Thanks,  
R. Singh

---

<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:** [August 21, 2017, 4:44pm UTC](https://community.unix.com/t/sort-data-in-text-file-in-particular-format/367978/5 "2017-08-21T16:44:32Z")

</div>

Just guessing what might be the `sort` criteria, this gets you quite near your desired output:

```nohighlight
awk '
!T[$0]++ && !/,/ {FN = $0
                         if (CMD) close (CMD)
                         CMD = "sort -t, -k1,1r -k2,2 - > " FN
                        }
$0 != FN {print | CMD}
' file ; for x in 20*; do echo $x; cat $x; done
20170308
PM,I,123
PM,U,123
DA,I,113
DA,U,22
20170309
PM,I,23
PM,U,233
DA,I,11
DA,U,123

```

---

<div class="post-metadata">

**Author:** ![Adfire](https://community.unix.com/letter_avatar/adfire/32/6_449c533f586939b6e5f246632a46421f.png) [@Adfire](https://community.unix.com/u/Adfire)\
**Post date:** [August 23, 2017, 7:53am UTC](https://community.unix.com/t/sort-data-in-text-file-in-particular-format/367978/6 "2017-08-23T07:53:45Z")

</div>

> [@ravindersingh13](#):
>
> Hello Adfire,
> 
> As RudiC already mentioned criteria is not mentioned of sorting, so as per your shown first part of output for `20170308 ` I am considering that we have to sort by 3rd field of digits for each digit ids(or `yyyymmdd` ). If your Input\_file is same as sample shown then following may help you in same.
> 
> ```plaintext
> awk '/^[0-9]/{val=$0;next} {a[val]=a[val]?a[val] ORS $0:$0} END{for(i in a){print i;system("echo " s1 a s1 " | sort -t, -k3nr")}}' s1="\"" Input_file
> 
> ```
> 
> Output will be as follows.
> 
> ```plaintext
> 20170308
> PM,I,123
> PM,U,123
> DA,I,113
> DA,U,22
> 20170309
> PM,U,233
> DA,U,123
> PM,I,23
> DA,I,11
> 
> ```
> 
> EDIT: Adding a non-one liner form of solution too here.
> 
> ```plaintext
> awk '/^[0-9]/{
> val=$0;
> next
> }
> {
> a[val]=a[val]?a[val] ORS $0:$0
> }
> END{
> for(i in a){
> print i;system("echo " s1 a s1 " | sort -t, -k3nr")
> }
> }
> ' s1="\"" Input_file
> 
> ```
> 
> Thanks,  
> R. Singh

Hi thanks for your reply it's working now.Now i need to add sum (Total).Example below

```plaintext
20170308
PM,Total,246
PM,I,123
PM,U,123
DA,Total,135
DA,I,113
DA,U,22
20170309
PM,Total,246
PM,I,23
PM,U,233
DA,Total,134
DA,I,11
DA,U,123

```

---

<div class="post-metadata">

**Author:** ![RavinderSingh13](https://community.unix.com/letter_avatar/ravindersingh13/32/6_449c533f586939b6e5f246632a46421f.png) [@RavinderSingh13](https://community.unix.com/u/RavinderSingh13)\
**Post date:** [August 23, 2017, 11:20pm UTC](https://community.unix.com/t/sort-data-in-text-file-in-particular-format/367978/7 "2017-08-23T23:20:59Z")

</div>

> [@adfire](#):
>
> Hi thanks for your reply it's working now.Now i need to add sum (Total).Example below
> 
> ```plaintext
> 20170308
> PM,Total,246
> PM,I,123
> PM,U,123
> DA,Total,135
> DA,I,113
> DA,U,22
> 20170309
> PM,Total,246
> PM,I,23
> PM,U,233
> DA,Total,134
> DA,I,11
> DA,U,123
> 
> ```

Hello Adfire,

Please always use code tags as per forum rules for your commands/codes/Input\_files, could you please try following and let me know if this helps you.

```plaintext
awk -F, '
val && /^[0-9]+/ && val !~ $0{
    print val;
    for(i in b){
       if(a){
         print c,a;
         delete a
};
       print b
};
    delete b;
    delete c
}
/^[0-9]+/{
    val=$0;
    next
}
{
    c[$1]=$1 FS $2;
    a[$1]+=$3;
    b[$1]=b[$1]?b[$1] ORS $0:$0
}
END{
    print val;
    for(i in b){
       if(a){
         print c,a;
         delete a
};
    print b
}
}
' Input_file
 

```

Output will be as follows.

```plaintext
20170308
PM,U 246
PM,I,123
PM,U,123
DA,U 135
DA,I,113
DA,U,22
20170309
PM,U 256
PM,I,23
PM,U,233
DA,U 134
DA,I,11
DA,U,123
 

```

Thanks,  
R. Singh

---

<div class="post-metadata">

**Author:** ![Adfire](https://community.unix.com/letter_avatar/adfire/32/6_449c533f586939b6e5f246632a46421f.png) [@Adfire](https://community.unix.com/u/Adfire)\
**Post date:** [August 29, 2017, 12:44pm UTC](https://community.unix.com/t/sort-data-in-text-file-in-particular-format/367978/8 "2017-08-29T12:44:46Z")

</div>

> [@ravindersingh13](#):
>
> Hello Adfire,
> 
> Please always use code tags as per forum rules for your commands/codes/Input\_files, could you please try following and let me know if this helps you.
> 
> ```plaintext
> awk -F, '
> val && /^[0-9]+/ && val !~ $0{
> print val;
> for(i in b){
> if(a){
> print c,a;
> delete a
> };
> print b
> };
> delete b;
> delete c
> }
> /^[0-9]+/{
> val=$0;
> next
> }
> {
> c[$1]=$1 FS $2;
> a[$1]+=$3;
> b[$1]=b[$1]?b[$1] ORS $0:$0
> }
> END{
> print val;
> for(i in b){
> if(a){
> print c,a;
> delete a
> };
> print b
> }
> }
> ' Input_file
>  
> 
> ```
> 
> Output will be as follows.
> 
> ```plaintext
> 20170308
> PM,U 246
> PM,I,123
> PM,U,123
> DA,U 135
> DA,I,113
> DA,U,22
> 20170309
> PM,U 256
> PM,I,23
> PM,U,233
> DA,U 134
> DA,I,11
> DA,U,123
>  
> 
> ```
> 
> Thanks,  
> R. Singh

> [@](#):
>
> Hi Ravinder it works .In case we have multiple rows for single line its should display consolidated count for that row.in below example we hv two rows for PM and U .It should display consolidated count.
> 
> ```plaintext
> 20170308
> PM,U 246
> PM,U, 113
> PM,I,123
> PM,U,123
> DA,U 135
> DA,I,113
> DA,U,22
> 
> ```

---

<div class="post-metadata">

**Author:** ![Adfire](https://community.unix.com/letter_avatar/adfire/32/6_449c533f586939b6e5f246632a46421f.png) [@Adfire](https://community.unix.com/u/Adfire)\
**Post date:** [August 30, 2017, 2:18pm UTC](https://community.unix.com/t/sort-data-in-text-file-in-particular-format/367978/9 "2017-08-30T14:18:46Z")

</div>

> [@adfire](#):
>

Hi Ravinder  
Could you please explain the code if possible? Thanks in advance.
