Sort lines in a file on 2 keys with variable text
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I am a DBA and have the output from a reorg generation process that gives me DML in the following format :
REORG TABLE SCHEMA.A some other options
REORG TABLE SCHEMA.B some other options
REORG TABLE SCHEMA.C some other options
REORG TABLE SCHEMA.D some other options
REORG INDEXES ALL FOR TABLE SCHEMA.A some other options
REORG INDEXES ALL FOR TABLE SCHEMA.B some other options
REORG INDEXES ALL FOR TABLE SCHEMA.C some other options
REORG INDEXES ALL FOR TABLE SCHEMA.D some other options
RUNSTATS ON TABLE SCHEMA.A some other options
RUNSTATS ON TABLE SCHEMA.B some other options
RUNSTATS ON TABLE SCHEMA.C some other options
RUNSTATS ON TABLE SCHEMA.D some other options
For reasons I won't go in to here, i need this file sorted into the following order (note there might be less or more than 4 tables, or less or more than 3 entries per table):
REORG TABLE SCHEMA.A
REORG INDEXES ALL FOR TABLE SCHEMA.A
RUNSTATS ON TABLE SCHEMA.A
REORG TABLE SCHEMA.B
REORG INDEXES ALL FOR TABLE SCHEMA.B
RUNSTATS ON TABLE SCHEMA.B
...etc
ie. REORG TABLES, REORG INDEX then RUNSTAT for each table in the file.
I have tried the following (plus variants):
sort -t. -k2 test.fil
sort -t. -k2.1,2.8 -k1.1,1.7 test.fil
but the issue seems to be getting the secondary sorting (REORG TABLE, REORG INDEX then RUNSTAT) working correctly.
The first sort command above gets objects in order of descending table name, then sorted by length of k2.
The second sort command doesn't account for the fact that TABLE comes after INDEX (T after I) in the alphabet, but that I need INDEX after Table.
Any gurus out there that can help, muchly appreciated. Maybe there's other awk's and linux commands to use in conjunction?
I've also been through the sort command options, maybe there's some i've missed.
linux sort
add a comment |Â
up vote
1
down vote
favorite
I am a DBA and have the output from a reorg generation process that gives me DML in the following format :
REORG TABLE SCHEMA.A some other options
REORG TABLE SCHEMA.B some other options
REORG TABLE SCHEMA.C some other options
REORG TABLE SCHEMA.D some other options
REORG INDEXES ALL FOR TABLE SCHEMA.A some other options
REORG INDEXES ALL FOR TABLE SCHEMA.B some other options
REORG INDEXES ALL FOR TABLE SCHEMA.C some other options
REORG INDEXES ALL FOR TABLE SCHEMA.D some other options
RUNSTATS ON TABLE SCHEMA.A some other options
RUNSTATS ON TABLE SCHEMA.B some other options
RUNSTATS ON TABLE SCHEMA.C some other options
RUNSTATS ON TABLE SCHEMA.D some other options
For reasons I won't go in to here, i need this file sorted into the following order (note there might be less or more than 4 tables, or less or more than 3 entries per table):
REORG TABLE SCHEMA.A
REORG INDEXES ALL FOR TABLE SCHEMA.A
RUNSTATS ON TABLE SCHEMA.A
REORG TABLE SCHEMA.B
REORG INDEXES ALL FOR TABLE SCHEMA.B
RUNSTATS ON TABLE SCHEMA.B
...etc
ie. REORG TABLES, REORG INDEX then RUNSTAT for each table in the file.
I have tried the following (plus variants):
sort -t. -k2 test.fil
sort -t. -k2.1,2.8 -k1.1,1.7 test.fil
but the issue seems to be getting the secondary sorting (REORG TABLE, REORG INDEX then RUNSTAT) working correctly.
The first sort command above gets objects in order of descending table name, then sorted by length of k2.
The second sort command doesn't account for the fact that TABLE comes after INDEX (T after I) in the alphabet, but that I need INDEX after Table.
Any gurus out there that can help, muchly appreciated. Maybe there's other awk's and linux commands to use in conjunction?
I've also been through the sort command options, maybe there's some i've missed.
linux sort
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am a DBA and have the output from a reorg generation process that gives me DML in the following format :
REORG TABLE SCHEMA.A some other options
REORG TABLE SCHEMA.B some other options
REORG TABLE SCHEMA.C some other options
REORG TABLE SCHEMA.D some other options
REORG INDEXES ALL FOR TABLE SCHEMA.A some other options
REORG INDEXES ALL FOR TABLE SCHEMA.B some other options
REORG INDEXES ALL FOR TABLE SCHEMA.C some other options
REORG INDEXES ALL FOR TABLE SCHEMA.D some other options
RUNSTATS ON TABLE SCHEMA.A some other options
RUNSTATS ON TABLE SCHEMA.B some other options
RUNSTATS ON TABLE SCHEMA.C some other options
RUNSTATS ON TABLE SCHEMA.D some other options
For reasons I won't go in to here, i need this file sorted into the following order (note there might be less or more than 4 tables, or less or more than 3 entries per table):
REORG TABLE SCHEMA.A
REORG INDEXES ALL FOR TABLE SCHEMA.A
RUNSTATS ON TABLE SCHEMA.A
REORG TABLE SCHEMA.B
REORG INDEXES ALL FOR TABLE SCHEMA.B
RUNSTATS ON TABLE SCHEMA.B
...etc
ie. REORG TABLES, REORG INDEX then RUNSTAT for each table in the file.
I have tried the following (plus variants):
sort -t. -k2 test.fil
sort -t. -k2.1,2.8 -k1.1,1.7 test.fil
but the issue seems to be getting the secondary sorting (REORG TABLE, REORG INDEX then RUNSTAT) working correctly.
The first sort command above gets objects in order of descending table name, then sorted by length of k2.
The second sort command doesn't account for the fact that TABLE comes after INDEX (T after I) in the alphabet, but that I need INDEX after Table.
Any gurus out there that can help, muchly appreciated. Maybe there's other awk's and linux commands to use in conjunction?
I've also been through the sort command options, maybe there's some i've missed.
linux sort
I am a DBA and have the output from a reorg generation process that gives me DML in the following format :
REORG TABLE SCHEMA.A some other options
REORG TABLE SCHEMA.B some other options
REORG TABLE SCHEMA.C some other options
REORG TABLE SCHEMA.D some other options
REORG INDEXES ALL FOR TABLE SCHEMA.A some other options
REORG INDEXES ALL FOR TABLE SCHEMA.B some other options
REORG INDEXES ALL FOR TABLE SCHEMA.C some other options
REORG INDEXES ALL FOR TABLE SCHEMA.D some other options
RUNSTATS ON TABLE SCHEMA.A some other options
RUNSTATS ON TABLE SCHEMA.B some other options
RUNSTATS ON TABLE SCHEMA.C some other options
RUNSTATS ON TABLE SCHEMA.D some other options
For reasons I won't go in to here, i need this file sorted into the following order (note there might be less or more than 4 tables, or less or more than 3 entries per table):
REORG TABLE SCHEMA.A
REORG INDEXES ALL FOR TABLE SCHEMA.A
RUNSTATS ON TABLE SCHEMA.A
REORG TABLE SCHEMA.B
REORG INDEXES ALL FOR TABLE SCHEMA.B
RUNSTATS ON TABLE SCHEMA.B
...etc
ie. REORG TABLES, REORG INDEX then RUNSTAT for each table in the file.
I have tried the following (plus variants):
sort -t. -k2 test.fil
sort -t. -k2.1,2.8 -k1.1,1.7 test.fil
but the issue seems to be getting the secondary sorting (REORG TABLE, REORG INDEX then RUNSTAT) working correctly.
The first sort command above gets objects in order of descending table name, then sorted by length of k2.
The second sort command doesn't account for the fact that TABLE comes after INDEX (T after I) in the alphabet, but that I need INDEX after Table.
Any gurus out there that can help, muchly appreciated. Maybe there's other awk's and linux commands to use in conjunction?
I've also been through the sort command options, maybe there's some i've missed.
linux sort
asked Jul 12 at 7:02
dergy
82
82
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Let's make an awk
script that modifies your data so that it's easily sortable the way you want it to be sorted:
BEGIN OFS = "t" # set output delimiter to a tab
/REORG TABLE/ $0 = 1 OFS $0 # prefix line with 1 for this op.
/REORG INDEX/ $0 = 2 OFS $0 # with 2
/RUNSTATS/ $0 = 3 OFS $0 # with 3
# prefix line with schema name (unconditionally)
match($0,"SCHEMA.[^ ]*"); $0 = substr($0,RSTART,RLENGTH) OFS $0
# output modified line
print
This awk
script would prepend each line with two tab-separated values:
- The schema name, e.g.
SCHEMA.A
. - The step that this operation happens in for this schema, which is an integer, 1, 2 or 3.
Running it on your data:
$ awk -f script.awk file
SCHEMA.A 1 REORG TABLE SCHEMA.A some other options
SCHEMA.B 1 REORG TABLE SCHEMA.B some other options
SCHEMA.C 1 REORG TABLE SCHEMA.C some other options
SCHEMA.D 1 REORG TABLE SCHEMA.D some other options
SCHEMA.A 2 REORG INDEXES ALL FOR TABLE SCHEMA.A some other options
SCHEMA.B 2 REORG INDEXES ALL FOR TABLE SCHEMA.B some other options
SCHEMA.C 2 REORG INDEXES ALL FOR TABLE SCHEMA.C some other options
SCHEMA.D 2 REORG INDEXES ALL FOR TABLE SCHEMA.D some other options
SCHEMA.A 3 RUNSTATS ON TABLE SCHEMA.A some other options
SCHEMA.B 3 RUNSTATS ON TABLE SCHEMA.B some other options
SCHEMA.C 3 RUNSTATS ON TABLE SCHEMA.C some other options
SCHEMA.D 3 RUNSTATS ON TABLE SCHEMA.D some other options
What this does is making the sorting keys easier to find for sort
.
This can then be sorted and the new columns may be deleted:
$ awk -f script.awk file | sort | cut -f 3-
REORG TABLE SCHEMA.A some other options
REORG INDEXES ALL FOR TABLE SCHEMA.A some other options
RUNSTATS ON TABLE SCHEMA.A some other options
REORG TABLE SCHEMA.B some other options
REORG INDEXES ALL FOR TABLE SCHEMA.B some other options
RUNSTATS ON TABLE SCHEMA.B some other options
REORG TABLE SCHEMA.C some other options
REORG INDEXES ALL FOR TABLE SCHEMA.C some other options
RUNSTATS ON TABLE SCHEMA.C some other options
REORG TABLE SCHEMA.D some other options
REORG INDEXES ALL FOR TABLE SCHEMA.D some other options
RUNSTATS ON TABLE SCHEMA.D some other options
Works a treat! Thanks Kusalananda, you've earned a co-author's credit on the script i'm writing!
â dergy
Jul 12 at 23:30
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Let's make an awk
script that modifies your data so that it's easily sortable the way you want it to be sorted:
BEGIN OFS = "t" # set output delimiter to a tab
/REORG TABLE/ $0 = 1 OFS $0 # prefix line with 1 for this op.
/REORG INDEX/ $0 = 2 OFS $0 # with 2
/RUNSTATS/ $0 = 3 OFS $0 # with 3
# prefix line with schema name (unconditionally)
match($0,"SCHEMA.[^ ]*"); $0 = substr($0,RSTART,RLENGTH) OFS $0
# output modified line
print
This awk
script would prepend each line with two tab-separated values:
- The schema name, e.g.
SCHEMA.A
. - The step that this operation happens in for this schema, which is an integer, 1, 2 or 3.
Running it on your data:
$ awk -f script.awk file
SCHEMA.A 1 REORG TABLE SCHEMA.A some other options
SCHEMA.B 1 REORG TABLE SCHEMA.B some other options
SCHEMA.C 1 REORG TABLE SCHEMA.C some other options
SCHEMA.D 1 REORG TABLE SCHEMA.D some other options
SCHEMA.A 2 REORG INDEXES ALL FOR TABLE SCHEMA.A some other options
SCHEMA.B 2 REORG INDEXES ALL FOR TABLE SCHEMA.B some other options
SCHEMA.C 2 REORG INDEXES ALL FOR TABLE SCHEMA.C some other options
SCHEMA.D 2 REORG INDEXES ALL FOR TABLE SCHEMA.D some other options
SCHEMA.A 3 RUNSTATS ON TABLE SCHEMA.A some other options
SCHEMA.B 3 RUNSTATS ON TABLE SCHEMA.B some other options
SCHEMA.C 3 RUNSTATS ON TABLE SCHEMA.C some other options
SCHEMA.D 3 RUNSTATS ON TABLE SCHEMA.D some other options
What this does is making the sorting keys easier to find for sort
.
This can then be sorted and the new columns may be deleted:
$ awk -f script.awk file | sort | cut -f 3-
REORG TABLE SCHEMA.A some other options
REORG INDEXES ALL FOR TABLE SCHEMA.A some other options
RUNSTATS ON TABLE SCHEMA.A some other options
REORG TABLE SCHEMA.B some other options
REORG INDEXES ALL FOR TABLE SCHEMA.B some other options
RUNSTATS ON TABLE SCHEMA.B some other options
REORG TABLE SCHEMA.C some other options
REORG INDEXES ALL FOR TABLE SCHEMA.C some other options
RUNSTATS ON TABLE SCHEMA.C some other options
REORG TABLE SCHEMA.D some other options
REORG INDEXES ALL FOR TABLE SCHEMA.D some other options
RUNSTATS ON TABLE SCHEMA.D some other options
Works a treat! Thanks Kusalananda, you've earned a co-author's credit on the script i'm writing!
â dergy
Jul 12 at 23:30
add a comment |Â
up vote
2
down vote
accepted
Let's make an awk
script that modifies your data so that it's easily sortable the way you want it to be sorted:
BEGIN OFS = "t" # set output delimiter to a tab
/REORG TABLE/ $0 = 1 OFS $0 # prefix line with 1 for this op.
/REORG INDEX/ $0 = 2 OFS $0 # with 2
/RUNSTATS/ $0 = 3 OFS $0 # with 3
# prefix line with schema name (unconditionally)
match($0,"SCHEMA.[^ ]*"); $0 = substr($0,RSTART,RLENGTH) OFS $0
# output modified line
print
This awk
script would prepend each line with two tab-separated values:
- The schema name, e.g.
SCHEMA.A
. - The step that this operation happens in for this schema, which is an integer, 1, 2 or 3.
Running it on your data:
$ awk -f script.awk file
SCHEMA.A 1 REORG TABLE SCHEMA.A some other options
SCHEMA.B 1 REORG TABLE SCHEMA.B some other options
SCHEMA.C 1 REORG TABLE SCHEMA.C some other options
SCHEMA.D 1 REORG TABLE SCHEMA.D some other options
SCHEMA.A 2 REORG INDEXES ALL FOR TABLE SCHEMA.A some other options
SCHEMA.B 2 REORG INDEXES ALL FOR TABLE SCHEMA.B some other options
SCHEMA.C 2 REORG INDEXES ALL FOR TABLE SCHEMA.C some other options
SCHEMA.D 2 REORG INDEXES ALL FOR TABLE SCHEMA.D some other options
SCHEMA.A 3 RUNSTATS ON TABLE SCHEMA.A some other options
SCHEMA.B 3 RUNSTATS ON TABLE SCHEMA.B some other options
SCHEMA.C 3 RUNSTATS ON TABLE SCHEMA.C some other options
SCHEMA.D 3 RUNSTATS ON TABLE SCHEMA.D some other options
What this does is making the sorting keys easier to find for sort
.
This can then be sorted and the new columns may be deleted:
$ awk -f script.awk file | sort | cut -f 3-
REORG TABLE SCHEMA.A some other options
REORG INDEXES ALL FOR TABLE SCHEMA.A some other options
RUNSTATS ON TABLE SCHEMA.A some other options
REORG TABLE SCHEMA.B some other options
REORG INDEXES ALL FOR TABLE SCHEMA.B some other options
RUNSTATS ON TABLE SCHEMA.B some other options
REORG TABLE SCHEMA.C some other options
REORG INDEXES ALL FOR TABLE SCHEMA.C some other options
RUNSTATS ON TABLE SCHEMA.C some other options
REORG TABLE SCHEMA.D some other options
REORG INDEXES ALL FOR TABLE SCHEMA.D some other options
RUNSTATS ON TABLE SCHEMA.D some other options
Works a treat! Thanks Kusalananda, you've earned a co-author's credit on the script i'm writing!
â dergy
Jul 12 at 23:30
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Let's make an awk
script that modifies your data so that it's easily sortable the way you want it to be sorted:
BEGIN OFS = "t" # set output delimiter to a tab
/REORG TABLE/ $0 = 1 OFS $0 # prefix line with 1 for this op.
/REORG INDEX/ $0 = 2 OFS $0 # with 2
/RUNSTATS/ $0 = 3 OFS $0 # with 3
# prefix line with schema name (unconditionally)
match($0,"SCHEMA.[^ ]*"); $0 = substr($0,RSTART,RLENGTH) OFS $0
# output modified line
print
This awk
script would prepend each line with two tab-separated values:
- The schema name, e.g.
SCHEMA.A
. - The step that this operation happens in for this schema, which is an integer, 1, 2 or 3.
Running it on your data:
$ awk -f script.awk file
SCHEMA.A 1 REORG TABLE SCHEMA.A some other options
SCHEMA.B 1 REORG TABLE SCHEMA.B some other options
SCHEMA.C 1 REORG TABLE SCHEMA.C some other options
SCHEMA.D 1 REORG TABLE SCHEMA.D some other options
SCHEMA.A 2 REORG INDEXES ALL FOR TABLE SCHEMA.A some other options
SCHEMA.B 2 REORG INDEXES ALL FOR TABLE SCHEMA.B some other options
SCHEMA.C 2 REORG INDEXES ALL FOR TABLE SCHEMA.C some other options
SCHEMA.D 2 REORG INDEXES ALL FOR TABLE SCHEMA.D some other options
SCHEMA.A 3 RUNSTATS ON TABLE SCHEMA.A some other options
SCHEMA.B 3 RUNSTATS ON TABLE SCHEMA.B some other options
SCHEMA.C 3 RUNSTATS ON TABLE SCHEMA.C some other options
SCHEMA.D 3 RUNSTATS ON TABLE SCHEMA.D some other options
What this does is making the sorting keys easier to find for sort
.
This can then be sorted and the new columns may be deleted:
$ awk -f script.awk file | sort | cut -f 3-
REORG TABLE SCHEMA.A some other options
REORG INDEXES ALL FOR TABLE SCHEMA.A some other options
RUNSTATS ON TABLE SCHEMA.A some other options
REORG TABLE SCHEMA.B some other options
REORG INDEXES ALL FOR TABLE SCHEMA.B some other options
RUNSTATS ON TABLE SCHEMA.B some other options
REORG TABLE SCHEMA.C some other options
REORG INDEXES ALL FOR TABLE SCHEMA.C some other options
RUNSTATS ON TABLE SCHEMA.C some other options
REORG TABLE SCHEMA.D some other options
REORG INDEXES ALL FOR TABLE SCHEMA.D some other options
RUNSTATS ON TABLE SCHEMA.D some other options
Let's make an awk
script that modifies your data so that it's easily sortable the way you want it to be sorted:
BEGIN OFS = "t" # set output delimiter to a tab
/REORG TABLE/ $0 = 1 OFS $0 # prefix line with 1 for this op.
/REORG INDEX/ $0 = 2 OFS $0 # with 2
/RUNSTATS/ $0 = 3 OFS $0 # with 3
# prefix line with schema name (unconditionally)
match($0,"SCHEMA.[^ ]*"); $0 = substr($0,RSTART,RLENGTH) OFS $0
# output modified line
print
This awk
script would prepend each line with two tab-separated values:
- The schema name, e.g.
SCHEMA.A
. - The step that this operation happens in for this schema, which is an integer, 1, 2 or 3.
Running it on your data:
$ awk -f script.awk file
SCHEMA.A 1 REORG TABLE SCHEMA.A some other options
SCHEMA.B 1 REORG TABLE SCHEMA.B some other options
SCHEMA.C 1 REORG TABLE SCHEMA.C some other options
SCHEMA.D 1 REORG TABLE SCHEMA.D some other options
SCHEMA.A 2 REORG INDEXES ALL FOR TABLE SCHEMA.A some other options
SCHEMA.B 2 REORG INDEXES ALL FOR TABLE SCHEMA.B some other options
SCHEMA.C 2 REORG INDEXES ALL FOR TABLE SCHEMA.C some other options
SCHEMA.D 2 REORG INDEXES ALL FOR TABLE SCHEMA.D some other options
SCHEMA.A 3 RUNSTATS ON TABLE SCHEMA.A some other options
SCHEMA.B 3 RUNSTATS ON TABLE SCHEMA.B some other options
SCHEMA.C 3 RUNSTATS ON TABLE SCHEMA.C some other options
SCHEMA.D 3 RUNSTATS ON TABLE SCHEMA.D some other options
What this does is making the sorting keys easier to find for sort
.
This can then be sorted and the new columns may be deleted:
$ awk -f script.awk file | sort | cut -f 3-
REORG TABLE SCHEMA.A some other options
REORG INDEXES ALL FOR TABLE SCHEMA.A some other options
RUNSTATS ON TABLE SCHEMA.A some other options
REORG TABLE SCHEMA.B some other options
REORG INDEXES ALL FOR TABLE SCHEMA.B some other options
RUNSTATS ON TABLE SCHEMA.B some other options
REORG TABLE SCHEMA.C some other options
REORG INDEXES ALL FOR TABLE SCHEMA.C some other options
RUNSTATS ON TABLE SCHEMA.C some other options
REORG TABLE SCHEMA.D some other options
REORG INDEXES ALL FOR TABLE SCHEMA.D some other options
RUNSTATS ON TABLE SCHEMA.D some other options
edited Jul 12 at 7:21
answered Jul 12 at 7:16
Kusalananda
101k13199312
101k13199312
Works a treat! Thanks Kusalananda, you've earned a co-author's credit on the script i'm writing!
â dergy
Jul 12 at 23:30
add a comment |Â
Works a treat! Thanks Kusalananda, you've earned a co-author's credit on the script i'm writing!
â dergy
Jul 12 at 23:30
Works a treat! Thanks Kusalananda, you've earned a co-author's credit on the script i'm writing!
â dergy
Jul 12 at 23:30
Works a treat! Thanks Kusalananda, you've earned a co-author's credit on the script i'm writing!
â dergy
Jul 12 at 23:30
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f454841%2fsort-lines-in-a-file-on-2-keys-with-variable-text%23new-answer', 'question_page');
);
Post as a guest
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
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
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