Sorting by two columns
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
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...
text-processing awk sort
add a comment |
up vote
3
down vote
favorite
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...
text-processing awk sort
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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...
text-processing awk sort
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...
text-processing awk sort
text-processing awk sort
edited Nov 17 at 0:33
Rui F Ribeiro
38.2k1475123
38.2k1475123
asked Mar 21 '16 at 19:24
Han
184
184
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
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 questionlinux
? 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
add a comment |
up vote
0
down vote
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
__________
__________________
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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 questionlinux
? 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
add a comment |
up vote
2
down vote
accepted
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 questionlinux
? 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
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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
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
edited Mar 21 '16 at 21:27
community wiki
3 revs
don_crissti
Is this possible to do with nawk instead of sed?
– Han
Mar 21 '16 at 20:28
nawk
? why did you tag your questionlinux
? 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
add a comment |
Is this possible to do with nawk instead of sed?
– Han
Mar 21 '16 at 20:28
nawk
? why did you tag your questionlinux
? 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
Is this possible to do with nawk instead of sed?
– Han
Mar 21 '16 at 20:28
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
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
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
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
@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
Thank you for the information. Sorry I edited the question accordingly.
– Han
Mar 21 '16 at 20:54
add a comment |
up vote
0
down vote
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
__________
__________________
add a comment |
up vote
0
down vote
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
__________
__________________
add a comment |
up vote
0
down vote
up vote
0
down vote
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
__________
__________________
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
__________
__________________
edited Mar 21 '16 at 20:51
answered Mar 21 '16 at 19:52
Sergei Kurenkov
2,0241016
2,0241016
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f271311%2fsorting-by-two-columns%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown