Shell script sort
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
Im trying to sort a small file with some entries containing two words but i want to sort this as one entry.
for example consider this small list
peter barker painter
carl baker cook
joshua carpenter
These are all names and occupations. now say i want to use sort to sort these entries.
Problem is sort uses white spaces as fields
so if i sort -k 1n i'll sort by first name
But i want to sort by full name and then have the option to sort by occupation aswell. As you can see some entires don't have a full name, joshua only have his first name and his occupation. So for him i want to sort only by first name but for the others full name.
Can this be achieved?
linux text-processing sort
add a comment |
up vote
0
down vote
favorite
Im trying to sort a small file with some entries containing two words but i want to sort this as one entry.
for example consider this small list
peter barker painter
carl baker cook
joshua carpenter
These are all names and occupations. now say i want to use sort to sort these entries.
Problem is sort uses white spaces as fields
so if i sort -k 1n i'll sort by first name
But i want to sort by full name and then have the option to sort by occupation aswell. As you can see some entires don't have a full name, joshua only have his first name and his occupation. So for him i want to sort only by first name but for the others full name.
Can this be achieved?
linux text-processing sort
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Im trying to sort a small file with some entries containing two words but i want to sort this as one entry.
for example consider this small list
peter barker painter
carl baker cook
joshua carpenter
These are all names and occupations. now say i want to use sort to sort these entries.
Problem is sort uses white spaces as fields
so if i sort -k 1n i'll sort by first name
But i want to sort by full name and then have the option to sort by occupation aswell. As you can see some entires don't have a full name, joshua only have his first name and his occupation. So for him i want to sort only by first name but for the others full name.
Can this be achieved?
linux text-processing sort
Im trying to sort a small file with some entries containing two words but i want to sort this as one entry.
for example consider this small list
peter barker painter
carl baker cook
joshua carpenter
These are all names and occupations. now say i want to use sort to sort these entries.
Problem is sort uses white spaces as fields
so if i sort -k 1n i'll sort by first name
But i want to sort by full name and then have the option to sort by occupation aswell. As you can see some entires don't have a full name, joshua only have his first name and his occupation. So for him i want to sort only by first name but for the others full name.
Can this be achieved?
linux text-processing sort
linux text-processing sort
edited Nov 23 at 15:58
Kusalananda
118k16222360
118k16222360
asked Nov 23 at 15:42
mrmagin
122
122
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Assuming that it is only ever going to be the surname that is missing (and not the first name) and that the words in the file does not include spaces (which would make it extremely difficult), first get the data into tab-delimited format with the missing surnames replaced by empty fields:
$ awk -v OFS='t' 'NF == 3 $1 = $1 NF == 2 $3 = $2; $2 = "" print ' <file
peter barker painter
carl baker cook
joshua carpenter
The awk
script will detect lines that contain two or three fields. It will simply reformat the lines that already has three fields into three tab-delimited fields while moving the second field into the third field for the lines that originally only contained two fields.
Then sort the data with tabs as delimiters:
$ awk -v OFS='t' 'NF == 3 $1 = $1 NF == 2 $3 = $2; $2 = "" print ' <file | sort -t $'t' -k1,2 -k3
carl baker cook
joshua carpenter
peter barker painter
The sorting done here is on the full name (fields one and two) and then by occupation. It is assumed that you are using a shell like bash
that understands $'t'
as a tab character.
Instead of tabs, you may use any other character that does not interfere with the data (here :
):
$ awk -v OFS=':' 'NF == 3 $1 = $1 NF == 2 $3 = $2; $2 = "" print ' <file | sort -t ':' -k1,2 -k3
carl:baker:cook
joshua::carpenter
peter:barker:painter
Then replace the chosen delimiter character by passing the result through tr
(here replacing with tabs, because it looks nice):
$ awk -v OFS=':' 'NF == 3 $1 = $1 NF == 2 $3 = $2; $2 = "" print ' <file | sort -t ':' -k1,2 -k3 | tr ':' 't'
carl baker cook
joshua carpenter
peter barker painter
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Assuming that it is only ever going to be the surname that is missing (and not the first name) and that the words in the file does not include spaces (which would make it extremely difficult), first get the data into tab-delimited format with the missing surnames replaced by empty fields:
$ awk -v OFS='t' 'NF == 3 $1 = $1 NF == 2 $3 = $2; $2 = "" print ' <file
peter barker painter
carl baker cook
joshua carpenter
The awk
script will detect lines that contain two or three fields. It will simply reformat the lines that already has three fields into three tab-delimited fields while moving the second field into the third field for the lines that originally only contained two fields.
Then sort the data with tabs as delimiters:
$ awk -v OFS='t' 'NF == 3 $1 = $1 NF == 2 $3 = $2; $2 = "" print ' <file | sort -t $'t' -k1,2 -k3
carl baker cook
joshua carpenter
peter barker painter
The sorting done here is on the full name (fields one and two) and then by occupation. It is assumed that you are using a shell like bash
that understands $'t'
as a tab character.
Instead of tabs, you may use any other character that does not interfere with the data (here :
):
$ awk -v OFS=':' 'NF == 3 $1 = $1 NF == 2 $3 = $2; $2 = "" print ' <file | sort -t ':' -k1,2 -k3
carl:baker:cook
joshua::carpenter
peter:barker:painter
Then replace the chosen delimiter character by passing the result through tr
(here replacing with tabs, because it looks nice):
$ awk -v OFS=':' 'NF == 3 $1 = $1 NF == 2 $3 = $2; $2 = "" print ' <file | sort -t ':' -k1,2 -k3 | tr ':' 't'
carl baker cook
joshua carpenter
peter barker painter
add a comment |
up vote
1
down vote
accepted
Assuming that it is only ever going to be the surname that is missing (and not the first name) and that the words in the file does not include spaces (which would make it extremely difficult), first get the data into tab-delimited format with the missing surnames replaced by empty fields:
$ awk -v OFS='t' 'NF == 3 $1 = $1 NF == 2 $3 = $2; $2 = "" print ' <file
peter barker painter
carl baker cook
joshua carpenter
The awk
script will detect lines that contain two or three fields. It will simply reformat the lines that already has three fields into three tab-delimited fields while moving the second field into the third field for the lines that originally only contained two fields.
Then sort the data with tabs as delimiters:
$ awk -v OFS='t' 'NF == 3 $1 = $1 NF == 2 $3 = $2; $2 = "" print ' <file | sort -t $'t' -k1,2 -k3
carl baker cook
joshua carpenter
peter barker painter
The sorting done here is on the full name (fields one and two) and then by occupation. It is assumed that you are using a shell like bash
that understands $'t'
as a tab character.
Instead of tabs, you may use any other character that does not interfere with the data (here :
):
$ awk -v OFS=':' 'NF == 3 $1 = $1 NF == 2 $3 = $2; $2 = "" print ' <file | sort -t ':' -k1,2 -k3
carl:baker:cook
joshua::carpenter
peter:barker:painter
Then replace the chosen delimiter character by passing the result through tr
(here replacing with tabs, because it looks nice):
$ awk -v OFS=':' 'NF == 3 $1 = $1 NF == 2 $3 = $2; $2 = "" print ' <file | sort -t ':' -k1,2 -k3 | tr ':' 't'
carl baker cook
joshua carpenter
peter barker painter
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Assuming that it is only ever going to be the surname that is missing (and not the first name) and that the words in the file does not include spaces (which would make it extremely difficult), first get the data into tab-delimited format with the missing surnames replaced by empty fields:
$ awk -v OFS='t' 'NF == 3 $1 = $1 NF == 2 $3 = $2; $2 = "" print ' <file
peter barker painter
carl baker cook
joshua carpenter
The awk
script will detect lines that contain two or three fields. It will simply reformat the lines that already has three fields into three tab-delimited fields while moving the second field into the third field for the lines that originally only contained two fields.
Then sort the data with tabs as delimiters:
$ awk -v OFS='t' 'NF == 3 $1 = $1 NF == 2 $3 = $2; $2 = "" print ' <file | sort -t $'t' -k1,2 -k3
carl baker cook
joshua carpenter
peter barker painter
The sorting done here is on the full name (fields one and two) and then by occupation. It is assumed that you are using a shell like bash
that understands $'t'
as a tab character.
Instead of tabs, you may use any other character that does not interfere with the data (here :
):
$ awk -v OFS=':' 'NF == 3 $1 = $1 NF == 2 $3 = $2; $2 = "" print ' <file | sort -t ':' -k1,2 -k3
carl:baker:cook
joshua::carpenter
peter:barker:painter
Then replace the chosen delimiter character by passing the result through tr
(here replacing with tabs, because it looks nice):
$ awk -v OFS=':' 'NF == 3 $1 = $1 NF == 2 $3 = $2; $2 = "" print ' <file | sort -t ':' -k1,2 -k3 | tr ':' 't'
carl baker cook
joshua carpenter
peter barker painter
Assuming that it is only ever going to be the surname that is missing (and not the first name) and that the words in the file does not include spaces (which would make it extremely difficult), first get the data into tab-delimited format with the missing surnames replaced by empty fields:
$ awk -v OFS='t' 'NF == 3 $1 = $1 NF == 2 $3 = $2; $2 = "" print ' <file
peter barker painter
carl baker cook
joshua carpenter
The awk
script will detect lines that contain two or three fields. It will simply reformat the lines that already has three fields into three tab-delimited fields while moving the second field into the third field for the lines that originally only contained two fields.
Then sort the data with tabs as delimiters:
$ awk -v OFS='t' 'NF == 3 $1 = $1 NF == 2 $3 = $2; $2 = "" print ' <file | sort -t $'t' -k1,2 -k3
carl baker cook
joshua carpenter
peter barker painter
The sorting done here is on the full name (fields one and two) and then by occupation. It is assumed that you are using a shell like bash
that understands $'t'
as a tab character.
Instead of tabs, you may use any other character that does not interfere with the data (here :
):
$ awk -v OFS=':' 'NF == 3 $1 = $1 NF == 2 $3 = $2; $2 = "" print ' <file | sort -t ':' -k1,2 -k3
carl:baker:cook
joshua::carpenter
peter:barker:painter
Then replace the chosen delimiter character by passing the result through tr
(here replacing with tabs, because it looks nice):
$ awk -v OFS=':' 'NF == 3 $1 = $1 NF == 2 $3 = $2; $2 = "" print ' <file | sort -t ':' -k1,2 -k3 | tr ':' 't'
carl baker cook
joshua carpenter
peter barker painter
edited Nov 23 at 15:57
answered Nov 23 at 15:50
Kusalananda
118k16222360
118k16222360
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f483710%2fshell-script-sort%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