-1

To sort various paragraphs (abc order) I tried:

awk 'BEGIN { RS="" } { a[FNR]=$0 } END { PROCINFO["sorted_in"]="@val_str_asc" for (i in a) print a[i] ORS } ' myrecords.txt

But it won't sort. Sample records:

Ham  
this is good  
(mind the mail)

Cheese  
I'm fine

Turkey
(empty)

Blocks of text might got one or more lines, seperated by one or more blank lines or even a date instead of a blank. The latter can be solved by replacing the date with a blank line.

Desired result:

Cheese
I'm fine

Ham 
this is good 
(mind the mail)

Turkey 
(empty)
Chris
  • 3
  • 3
  • 1
    Works just fine over here, except: 1. Add the missing `;` in `END` before the `for`. 2. Are you sure you are using GNU awk as other awks won't sort the data out-of-the-box? – James Brown Feb 25 '22 at 11:43
  • 2
    See answers to [your last question](https://stackoverflow.com/q/71213089/3776858). – Cyrus Feb 25 '22 at 11:50
  • What version of AWK are you using? Please check that using `awk --version` and add it to your question – Daweo Feb 25 '22 at 11:50
  • 1
    Assuming you've made sure you're using GNU awk, are you sure those blank lines are really empty and don't have a space or `CR` (aka `\r`, aka `^M`) in them? [edit] your question to show the output of running `cat -Ev file` on your input file. – Ed Morton Feb 25 '22 at 12:48
  • @Daweo GNU Awk 4.1.0 and busybox v1.22.1 based gawk as well – Chris Feb 25 '22 at 16:45
  • @EdMorton Sample `^M$ ^M$ List of groups ^M$ ???? Swiss ???? ^M$ ^M$ t.me/unarkt_sa^M$ ^M$ t.me/unkot^M$ ^M$ ^M$ Stutt^M$ t.me/versStutt^M$ ^M$ ^M$ TM-|bin^M$ Zu son tier: @fule^M$ ^M$ ^M$ FM-|h^M$ t.me/fuehSIC^M$` – Chris Feb 25 '22 at 17:12

1 Answers1

1

From the output shown in your comment your lines all end in control-Ms (Carriage Returns) so the ones that look empty actually aren't so your whole file is a single record when RS is null. Run dos2unix or sed 's/\r$//' on your input file to remove those CRs and then run the awk command again. See the difference below before and after I run sed on the input to remove the CRs:

$ cat -Ev file
Ham  ^M$
this is good  ^M$
(mind the mail)^M$
^M$
Cheese  ^M$
I'm fine^M$
^M$
Turkey^M$
(empty)^M$

$ awk -v RS= '{print NR, "<" $0 ">"}' file | cat -Ev
1 <Ham  ^M$
this is good  ^M$
(mind the mail)^M$
^M$
Cheese  ^M$
I'm fine^M$
^M$
Turkey^M$
(empty)^M>$

$ sed 's/\r$//' file > tmp && mv tmp file

$ cat -Ev file
Ham  $
this is good  $
(mind the mail)$
$
Cheese  $
I'm fine$
$
Turkey$
(empty)$

$ awk -v RS= '{print NR, "<" $0 ">"}' file | cat -Ev
1 <Ham  $
this is good  $
(mind the mail)>$
2 <Cheese  $
I'm fine>$
3 <Turkey$
(empty)>$

See Why does my tool output overwrite itself and how do I fix it? for more information on those DOS line endings.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185