rearranging rows in a table using awk [closed]
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a table with several hundred lines:
a1
a2
a3
a4
b1
b2
b3
b4
c1
c2
c3
c4
... etc.
I want to return it in the following order:
a1
b1
c1
d1
a2
b2
c2
d2
a3
b3
c3
The script below works to select the first block of lines:
$ awk 'NR%4==1)print'
But how can I loop it to do it for the whole file?
awk
closed as unclear what you're asking by slm⦠Aug 24 at 2:09
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |Â
up vote
0
down vote
favorite
I have a table with several hundred lines:
a1
a2
a3
a4
b1
b2
b3
b4
c1
c2
c3
c4
... etc.
I want to return it in the following order:
a1
b1
c1
d1
a2
b2
c2
d2
a3
b3
c3
The script below works to select the first block of lines:
$ awk 'NR%4==1)print'
But how can I loop it to do it for the whole file?
awk
closed as unclear what you're asking by slm⦠Aug 24 at 2:09
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
Is it always two characters on each line?
â unxnut
Aug 23 at 23:20
no, actually it is a table with numerous columns. the first column is like shown in my example.
â kate
Aug 23 at 23:29
So, we can assume that the first column is two characters?
â unxnut
Aug 24 at 3:19
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a table with several hundred lines:
a1
a2
a3
a4
b1
b2
b3
b4
c1
c2
c3
c4
... etc.
I want to return it in the following order:
a1
b1
c1
d1
a2
b2
c2
d2
a3
b3
c3
The script below works to select the first block of lines:
$ awk 'NR%4==1)print'
But how can I loop it to do it for the whole file?
awk
I have a table with several hundred lines:
a1
a2
a3
a4
b1
b2
b3
b4
c1
c2
c3
c4
... etc.
I want to return it in the following order:
a1
b1
c1
d1
a2
b2
c2
d2
a3
b3
c3
The script below works to select the first block of lines:
$ awk 'NR%4==1)print'
But how can I loop it to do it for the whole file?
awk
awk
edited Aug 24 at 0:59
Rui F Ribeiro
36.7k1271116
36.7k1271116
asked Aug 23 at 23:14
kate
1
1
closed as unclear what you're asking by slm⦠Aug 24 at 2:09
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by slm⦠Aug 24 at 2:09
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
Is it always two characters on each line?
â unxnut
Aug 23 at 23:20
no, actually it is a table with numerous columns. the first column is like shown in my example.
â kate
Aug 23 at 23:29
So, we can assume that the first column is two characters?
â unxnut
Aug 24 at 3:19
add a comment |Â
1
Is it always two characters on each line?
â unxnut
Aug 23 at 23:20
no, actually it is a table with numerous columns. the first column is like shown in my example.
â kate
Aug 23 at 23:29
So, we can assume that the first column is two characters?
â unxnut
Aug 24 at 3:19
1
1
Is it always two characters on each line?
â unxnut
Aug 23 at 23:20
Is it always two characters on each line?
â unxnut
Aug 23 at 23:20
no, actually it is a table with numerous columns. the first column is like shown in my example.
â kate
Aug 23 at 23:29
no, actually it is a table with numerous columns. the first column is like shown in my example.
â kate
Aug 23 at 23:29
So, we can assume that the first column is two characters?
â unxnut
Aug 24 at 3:19
So, we can assume that the first column is two characters?
â unxnut
Aug 24 at 3:19
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
You can use sort
to do the sorting instead. Specifically you can tell sort
to do a general sort, g
which handles the sorting of letters and numbers. We can control which character in the string we want to do the sorting on by telling sort
using the X.Y
notation instead of the more typical X,Y
notation.
For example:
$ sort -k1.2g file
a1
b1
c1
a2
b2
c2
a3
b3
c3
a4
b4
c4
Sort options:
-k, --key=KEYDEF
sort via a key; KEYDEF gives location and type
-g, --general-numeric-sort
compare according to general numerical value
KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is
a field number and C a character position in the field; both are origin 1,
and the stop position defaults to the line's end. If neither -t nor -b is
in effect, characters in a field are counted from the beginning of the
preceding whitespace. OPTS is one or more single-letter ordering options
[bdfgiMhnRrV], which override global ordering options for that key. If
no key is given, use the entire line as the key.
thank you for attention to my problem, but it does not work for my case, because in several lines the letter-number combinations repeat and I get something like a1,a1,a1,b1,b1,b1... I think it's because my description of the task is too generic.
â kate
Aug 23 at 23:45
2
@kate - then please update your Q with actual real data, otherwise we cannot help further.
â slmâ¦
Aug 23 at 23:48
add a comment |Â
up vote
0
down vote
If the "step" is always small (in your case, 4) then a quick'n'dirty way might be to simply read the file that many times and pick out the records at each offset - e.g.
awk 'FNR==1 k++ !((FNR-k)%4)' file file file file
a1
b1
c1
a2
b2
c2
a3
b3
c3
a4
b4
c4
or, equivalently, using GNU Awk (with its BEGINFILE
rule):
gawk 'BEGINFILEk++ !((FNR-k)%4)' file file file file
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You can use sort
to do the sorting instead. Specifically you can tell sort
to do a general sort, g
which handles the sorting of letters and numbers. We can control which character in the string we want to do the sorting on by telling sort
using the X.Y
notation instead of the more typical X,Y
notation.
For example:
$ sort -k1.2g file
a1
b1
c1
a2
b2
c2
a3
b3
c3
a4
b4
c4
Sort options:
-k, --key=KEYDEF
sort via a key; KEYDEF gives location and type
-g, --general-numeric-sort
compare according to general numerical value
KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is
a field number and C a character position in the field; both are origin 1,
and the stop position defaults to the line's end. If neither -t nor -b is
in effect, characters in a field are counted from the beginning of the
preceding whitespace. OPTS is one or more single-letter ordering options
[bdfgiMhnRrV], which override global ordering options for that key. If
no key is given, use the entire line as the key.
thank you for attention to my problem, but it does not work for my case, because in several lines the letter-number combinations repeat and I get something like a1,a1,a1,b1,b1,b1... I think it's because my description of the task is too generic.
â kate
Aug 23 at 23:45
2
@kate - then please update your Q with actual real data, otherwise we cannot help further.
â slmâ¦
Aug 23 at 23:48
add a comment |Â
up vote
1
down vote
You can use sort
to do the sorting instead. Specifically you can tell sort
to do a general sort, g
which handles the sorting of letters and numbers. We can control which character in the string we want to do the sorting on by telling sort
using the X.Y
notation instead of the more typical X,Y
notation.
For example:
$ sort -k1.2g file
a1
b1
c1
a2
b2
c2
a3
b3
c3
a4
b4
c4
Sort options:
-k, --key=KEYDEF
sort via a key; KEYDEF gives location and type
-g, --general-numeric-sort
compare according to general numerical value
KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is
a field number and C a character position in the field; both are origin 1,
and the stop position defaults to the line's end. If neither -t nor -b is
in effect, characters in a field are counted from the beginning of the
preceding whitespace. OPTS is one or more single-letter ordering options
[bdfgiMhnRrV], which override global ordering options for that key. If
no key is given, use the entire line as the key.
thank you for attention to my problem, but it does not work for my case, because in several lines the letter-number combinations repeat and I get something like a1,a1,a1,b1,b1,b1... I think it's because my description of the task is too generic.
â kate
Aug 23 at 23:45
2
@kate - then please update your Q with actual real data, otherwise we cannot help further.
â slmâ¦
Aug 23 at 23:48
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You can use sort
to do the sorting instead. Specifically you can tell sort
to do a general sort, g
which handles the sorting of letters and numbers. We can control which character in the string we want to do the sorting on by telling sort
using the X.Y
notation instead of the more typical X,Y
notation.
For example:
$ sort -k1.2g file
a1
b1
c1
a2
b2
c2
a3
b3
c3
a4
b4
c4
Sort options:
-k, --key=KEYDEF
sort via a key; KEYDEF gives location and type
-g, --general-numeric-sort
compare according to general numerical value
KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is
a field number and C a character position in the field; both are origin 1,
and the stop position defaults to the line's end. If neither -t nor -b is
in effect, characters in a field are counted from the beginning of the
preceding whitespace. OPTS is one or more single-letter ordering options
[bdfgiMhnRrV], which override global ordering options for that key. If
no key is given, use the entire line as the key.
You can use sort
to do the sorting instead. Specifically you can tell sort
to do a general sort, g
which handles the sorting of letters and numbers. We can control which character in the string we want to do the sorting on by telling sort
using the X.Y
notation instead of the more typical X,Y
notation.
For example:
$ sort -k1.2g file
a1
b1
c1
a2
b2
c2
a3
b3
c3
a4
b4
c4
Sort options:
-k, --key=KEYDEF
sort via a key; KEYDEF gives location and type
-g, --general-numeric-sort
compare according to general numerical value
KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is
a field number and C a character position in the field; both are origin 1,
and the stop position defaults to the line's end. If neither -t nor -b is
in effect, characters in a field are counted from the beginning of the
preceding whitespace. OPTS is one or more single-letter ordering options
[bdfgiMhnRrV], which override global ordering options for that key. If
no key is given, use the entire line as the key.
answered Aug 23 at 23:28
slmâ¦
238k65494664
238k65494664
thank you for attention to my problem, but it does not work for my case, because in several lines the letter-number combinations repeat and I get something like a1,a1,a1,b1,b1,b1... I think it's because my description of the task is too generic.
â kate
Aug 23 at 23:45
2
@kate - then please update your Q with actual real data, otherwise we cannot help further.
â slmâ¦
Aug 23 at 23:48
add a comment |Â
thank you for attention to my problem, but it does not work for my case, because in several lines the letter-number combinations repeat and I get something like a1,a1,a1,b1,b1,b1... I think it's because my description of the task is too generic.
â kate
Aug 23 at 23:45
2
@kate - then please update your Q with actual real data, otherwise we cannot help further.
â slmâ¦
Aug 23 at 23:48
thank you for attention to my problem, but it does not work for my case, because in several lines the letter-number combinations repeat and I get something like a1,a1,a1,b1,b1,b1... I think it's because my description of the task is too generic.
â kate
Aug 23 at 23:45
thank you for attention to my problem, but it does not work for my case, because in several lines the letter-number combinations repeat and I get something like a1,a1,a1,b1,b1,b1... I think it's because my description of the task is too generic.
â kate
Aug 23 at 23:45
2
2
@kate - then please update your Q with actual real data, otherwise we cannot help further.
â slmâ¦
Aug 23 at 23:48
@kate - then please update your Q with actual real data, otherwise we cannot help further.
â slmâ¦
Aug 23 at 23:48
add a comment |Â
up vote
0
down vote
If the "step" is always small (in your case, 4) then a quick'n'dirty way might be to simply read the file that many times and pick out the records at each offset - e.g.
awk 'FNR==1 k++ !((FNR-k)%4)' file file file file
a1
b1
c1
a2
b2
c2
a3
b3
c3
a4
b4
c4
or, equivalently, using GNU Awk (with its BEGINFILE
rule):
gawk 'BEGINFILEk++ !((FNR-k)%4)' file file file file
add a comment |Â
up vote
0
down vote
If the "step" is always small (in your case, 4) then a quick'n'dirty way might be to simply read the file that many times and pick out the records at each offset - e.g.
awk 'FNR==1 k++ !((FNR-k)%4)' file file file file
a1
b1
c1
a2
b2
c2
a3
b3
c3
a4
b4
c4
or, equivalently, using GNU Awk (with its BEGINFILE
rule):
gawk 'BEGINFILEk++ !((FNR-k)%4)' file file file file
add a comment |Â
up vote
0
down vote
up vote
0
down vote
If the "step" is always small (in your case, 4) then a quick'n'dirty way might be to simply read the file that many times and pick out the records at each offset - e.g.
awk 'FNR==1 k++ !((FNR-k)%4)' file file file file
a1
b1
c1
a2
b2
c2
a3
b3
c3
a4
b4
c4
or, equivalently, using GNU Awk (with its BEGINFILE
rule):
gawk 'BEGINFILEk++ !((FNR-k)%4)' file file file file
If the "step" is always small (in your case, 4) then a quick'n'dirty way might be to simply read the file that many times and pick out the records at each offset - e.g.
awk 'FNR==1 k++ !((FNR-k)%4)' file file file file
a1
b1
c1
a2
b2
c2
a3
b3
c3
a4
b4
c4
or, equivalently, using GNU Awk (with its BEGINFILE
rule):
gawk 'BEGINFILEk++ !((FNR-k)%4)' file file file file
edited Aug 24 at 1:40
answered Aug 23 at 23:45
steeldriver
32.2k34979
32.2k34979
add a comment |Â
add a comment |Â
1
Is it always two characters on each line?
â unxnut
Aug 23 at 23:20
no, actually it is a table with numerous columns. the first column is like shown in my example.
â kate
Aug 23 at 23:29
So, we can assume that the first column is two characters?
â unxnut
Aug 24 at 3:19