3

Write a shell script named sales1 using nawk and sort commands to do the following:
1. print the file in two sections, east and west, with lines alphabetized by last name within each section, showing per quarter and total sales per person.
2. print the names of sales people in three groups:
"The following people had total sales greater than $270:"
"The following people had total sales between $250 and $270:"
"The following people had total sales less than $250:"

east:Sam Smith:52:72:78:62  
east:Daniel Liebelt:83:78:84:61  
east:Shirley Couts:66:57:67:37  
east:James Peringer:50:62:56:94  
east:Lilly Batsuro:58:85:84:52  
west:Alex Opitz:79:68:57:93  
west:Carmen Gondar:62:58:85:56  
west:Derek Willard:63:69:74:43  
west:Kim Husak:89:91:63:64  
west:Lenny Taufa:53:61:82:52

I only need assistance with # 1...

I've tried

sort -k1,1 -k2,2

But it only sorts it by east then west and not by the last name. I'm trying to treat the first column as the direction and first name and the second column as the last name with the numbers...

2

With awk you could prepend : to the last field (that is, last name plus everything that follows) sort by 1st and 3rd fields (this time using : as a field delimiter) then again with awk remove the first : from the last field :

awk '$NF=":"$NF' infile | sort -t : -k1,1 -k3,3 | awk 'sub(/:/, "", $NF)'

end result:

east:Lilly Batsuro:58:85:84:52
east:Shirley Couts:66:57:67:37
east:Daniel Liebelt:83:78:84:61
east:James Peringer:50:62:56:94
east:Sam Smith:52:72:78:62
west:Carmen Gondar:62:58:85:56
west:Kim Husak:89:91:63:64
west:Alex Opitz:79:68:57:93
west:Lenny Taufa:53:61:82:52
west:Derek Willard:63:69:74:43
  • Is this possible to do with nawk instead of sed? – Han Mar 21 '16 at 20:28
  • nawk ? why did you tag your question linux ? what's your OS ? – don_crissti Mar 21 '16 at 20:32
  • Oh sorry that was a mistake. I'm currently using debian. The assignment requests that I use nawk and sorting commands. Which is why I'm asking – Han Mar 21 '16 at 20:44
  • 1
    @Han - for your information: debian is a linux distro. Also, if this is homework post the exact requirements and tell us what have you tried. – don_crissti Mar 21 '16 at 20:47
  • Thank you for the information. Sorry I edited the question accordingly. – Han Mar 21 '16 at 20:54
0
sed -r 's/^([^:]+):([^ ]+) ([^:]+):.*$/\1-\3 &/' your-file.txt  | \
    sort | sed -r 's/[^ ]+ (.*)/\1/'

Example:

$ sed -r 's/^([^:]+):([^ ]+) ([^:]+):.*$/\1-\3 &/' your-file.txt  | sort | sed -r 's/[^ ]+ (.*)/\1/'
east:Lilly Batsuro:58:85:84:52
east:Shirley Couts:66:57:67:37
east:Daniel Liebelt:83:78:84:61
east:James Peringer:50:62:56:94
east:Sam Smith:52:72:78:62
west:Carmen Gondar:62:58:85:56
west:Kim Husak:89:91:63:64
west:Alex Opitz:79:68:57:93
west:Lenny Taufa:53:61:82:52
west:Derek Willard:63:69:74:43

By the way, this is how sort selects the first field and the second field when you run it with sort -k1,1 -k2,2:

$ sort --debug -k1,1 -k2,2 ttt.txt
east:Daniel Liebelt:83:78:84:61
___________
           ____________________
_________________________________
east:James Peringer:50:62:56:94
__________
          _____________________
_________________________________
east:Lilly Batsuro:58:85:84:52
__________
          ____________________
________________________________
east:Sam Smith:52:72:78:62
________
        __________________
____________________________
east:Shirley Couts:66:57:67:37
____________
            __________________
________________________________
west:Alex Opitz:79:68:57:93
_________
         __________________
_____________________________
west:Carmen Gondar:62:58:85:56
___________
           ___________________
________________________________
west:Derek Willard:63:69:74:43
__________
          ____________________
________________________________
west:Kim Husak:89:91:63:64
________
        __________________
____________________________
west:Lenny Taufa:53:61:82:52
__________
          __________________

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.