Help sorting a file using sorting

I have this file:

100: pattern1
++++++++++++++++++++
1:pattern2
9:pattern2
+++++++++++++++++++
79: pattern1
61: pattern1
+++++++++++++++++++

and I want to sort it as follows:

++++++++++++++++++++
1:pattern2
9:pattern2
+++++++++++++++++++
61:pattern1
79:pattern1
100:pattern1
+++++++++++++++++++

Is it possible to use only the Linux sort command?

If I had:

4:pat1 
3:pat2
2:pat2
1:pat1

O / p should be:

1:pat1
++++++++++++ 
2:pat2
3:pat2
++++++++++++
4:pat1

So, you want to sort the first group, but the "group" according to the template of the second group. Note that the thing after: is a regular expression pattern, not a literal.

+3
source share
4 answers

I do not believe that sortcan do what you need.

Create a new shell script and put it in its contents (i.e. mysort.sh):

#!/bin/sh
IFS=$'\n' # This makes the for loop below split on newline instead of whitespace.
delim=+++++++++++++++++++
for l in `grep -v ^+| sort -g`      # Ignore all + lines and sort by number
do
    current=`echo $l | sed s/^[0-9]*://g` # Get what comes after the number
    if [ ! -z "$prev" ] && [ "$prev" != "$current" ] # If it has changed...
    then                                  #  then output a ++++ delimiter line.
        echo $delim
    fi
    prev=$current
    echo $l                               # Output this line.
done

To use it, write the contents of your file as follows:

cat input | sh mysort.sh
0
source

. "+".

$ sort -n input
+++++++++++++++++++
+++++++++++++++++++
++++++++++++++++++++
1:wow
9:wow
61: this is it
79: this is it
100: this is it
+1

, - (1). , , . - , , AWK Perl Python script.

0

, ':' :

sort  -rk2 | uniq -D -f1

;

  • , "" ( , sort(1) . , , sort -k2,1n, ).
  • use --all-repeated=separateinstead -Dto get empty dividers between groups. Look man uniqfor more ideas!

However, since your input is colon-separated, you need to hack:

sed 's/\([0123456789]\+\):/\1 /' t | sort  -rk2 | uniq -D -f1

NTN

0
source

All Articles