0

My input may be any of the given two types.
First one
Each field is of width 20

                2019              Autumn              CS 753                   6 Department elective                  AB
                2019              Autumn              CS 490                   6         Honors core                  CC
                2019              Autumn              CS 492                   6         Honors core                  FR
                2019              Autumn              CS 747                   6 Department elective                  FF
                2019              Autumn              ES 200                   3      Basic Sciences                  BC

Second one

^[[40m^[[33m                2019              Autumn              CS 753                   6 Department elective                  AB^[(B^[[m
^[[40m^[[37m                2019              Autumn              CS 490                   6         Honors core                  CC^[(B^[[m
^[[40m^[[37m                2019              Autumn              CS 492                   6         Honors core                  FR^[(B^[[m
^[[40m^[[33m                2019              Autumn              CS 747                   6 Department elective                  FF^[(B^[[m
^[[40m^[[36m                2019              Autumn              ES 200                   3      Basic Sciences                  BC^[(B^[[m

first file with color
I want to sort them with respect to third field that is field containing CS 490 etc.
Coloured inputs should give coloured outputs.
Expected output for input 1

                2019              Autumn              CS 490                   6         Honors core                  CC
                2019              Autumn              CS 492                   6         Honors core                  FR
                2019              Autumn              CS 747                   6 Department elective                  FF
                2019              Autumn              CS 753                   6 Department elective                  AB
                2019              Autumn              ES 200                   3      Basic Sciences                  BC

Likewise for input 2

^[[40m^[[37m                2019              Autumn              CS 490                   6         Honors core                  CC^[(B^[[m
^[[40m^[[37m                2019              Autumn              CS 492                   6         Honors core                  FR^[(B^[[m
^[[40m^[[33m                2019              Autumn              CS 747                   6 Department elective                  FF^[(B^[[m
^[[40m^[[33m                2019              Autumn              CS 753                   6 Department elective                  AB^[(B^[[m
^[[40m^[[36m                2019              Autumn              ES 200                   3      Basic Sciences                  BC^[(B^[[m

How can i get this in bash.
I can sort first input by doing sort -k3,4
And the second one by sort -k4,5
But the problem arises when it can be any of the above.

7
  • 1
    what have you tried so far? have you tried using the sort? command? here are some samples: sample1, sample2; sample3 Commented Sep 27, 2020 at 13:01
  • I have edited question please look at it. Commented Sep 27, 2020 at 14:40
  • what do you get as output when you run your sort commands? what do you mean by 'problem arises when it can be any of the above'? what problem are you referring to? Commented Sep 27, 2020 at 14:44
  • My input can be any of the two for same script, then first command will not work for second one and vice versa Commented Sep 27, 2020 at 14:45
  • 1
    I'd do your first sort by position, not field: sort -k41,60 <file> Commented Sep 27, 2020 at 14:49

1 Answer 1

1

Provisos and assumptions:

  • input files can be one of two formats: 1) without color codes, 2) with color codes
  • a file will not contain both formats
  • for files with color formats the first character in the line is the single character Ctrl-[ (ie, it is not the 2x characters '^' + '[')

For the sake of this answer we'll name the files:

  • file1 - contains no color characters
  • file2 - contains color characters

One solution using sort to order by fields 3,4 (non-color) or fields 4,5 (color)

for f in file1 file2
do
    echo "+++++++++++++++++ ${f}"
    pat='-k3,4'                                                  # define `sort` pattern for non-color file
    [[ $(head -1 ${f} | cut -c1) =~ $'\c[' ]] && pat='-k4,5'     # if first character of first line is `Ctrl-[` (ie, $'\c[') then define `sort` pattern for color file
    sort ${pat} ${f}                                             # sort the file
done

Running the above generates:

+++++++++++++++++ file1
            2019              Autumn              CS 490                   6         Honors core                  CC
            2019              Autumn              CS 492                   6         Honors core                  FR
            2019              Autumn              CS 747                   6 Department elective                  FF
            2019              Autumn              CS 753                   6 Department elective                  AB
            2019              Autumn              ES 200                   3      Basic Sciences                  BC                 AB
+++++++++++++++++ file2
            2019              Autumn              CS 490                   6         Honors core                  CC
            2019              Autumn              CS 492                   6         Honors core                  FR
            2019              Autumn              CS 747                   6 Department elective                  FF
            2019              Autumn              CS 753                   6 Department elective                  AB
            2019              Autumn              ES 200                   3      Basic Sciences                  BC

NOTE: The last 3 lines of output for file2 show with color in my console.

To show color formatting codes: sort ${pat} ${f} | cat -v

+++++++++++++++++ file1
                2019              Autumn              CS 490                   6         Honors core                  CC
                2019              Autumn              CS 492                   6         Honors core                  FR
                2019              Autumn              CS 747                   6 Department elective                  FF
                2019              Autumn              CS 753                   6 Department elective                  AB
                2019              Autumn              ES 200                   3      Basic Sciences                  BC
+++++++++++++++++ file2
^[[40m^[[37m                2019              Autumn              CS 490                   6         Honors core                  CC^[(B^[[m
^[[40m^[[37m                2019              Autumn              CS 492                   6         Honors core                  FR^[(B^[[m
^[[40m^[[33m                2019              Autumn              CS 747                   6 Department elective                  FF^[(B^[[m
^[[40m^[[33m                2019              Autumn              CS 753                   6 Department elective                  AB^[(B^[[m
^[[40m^[[36m                2019              Autumn              ES 200                   3      Basic Sciences                  BC^[(B^[[m
Sign up to request clarification or add additional context in comments.

2 Comments

It is working. Only thing I can't understand is your third assumption. I don't know about control character.
head -1 file2 | cut -c1-2 | od -c will output '033' if the first character in the file is the control character Ctrl-[, or it will output ^ and [ if it's not a single control character

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.