How to manipulate a 2D array into a multi-dimensional array in ‘awk’?Create ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?How to insert an item into an array at a specific index (JavaScript)?How do I empty an array in JavaScript?How to Sort Multi-dimensional Array by Value?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
Multiplicative persistence
What is the difference between lands and mana?
What is the English pronunciation of "pain au chocolat"?
What features enable the Su-25 Frogfoot to operate with such a wide variety of fuels?
How to preserve electronics (computers, iPads and phones) for hundreds of years
The IT department bottlenecks progress, how should I handle this?
Are Captain Marvel's powers affected by Thanos breaking the Tesseract and claiming the stone?
Has the laser at Magurele, Romania reached a tenth of the Sun's power?
C++ check if statement can be evaluated constexpr
Are cause and effect the same as in our Universe in a non-relativistic, Newtonian Universe in which the speed of light is infinite?
Will number of steps recorded on FitBit/any fitness tracker add up distance in PokemonGo?
How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?
How would you translate "more" for use as an interface button?
Does the Linux kernel need a file system to run?
Why is it that I can sometimes guess the next note?
C++ copy constructor called at return
Has any country ever had 2 former presidents in jail simultaneously?
How do you make your own symbol when Detexify fails?
Review your own paper in Mathematics
It grows, but water kills it
PTIJ: Why is Haman obsessed with Bose?
How can I write humor as character trait?
Mimic lecturing on blackboard, facing audience
Is this part of the description of the Archfey warlock's Misty Escape feature redundant?
How to manipulate a 2D array into a multi-dimensional array in ‘awk’?
Create ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?How to insert an item into an array at a specific index (JavaScript)?How do I empty an array in JavaScript?How to Sort Multi-dimensional Array by Value?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
I would like to transform a 2D array into a multi-dimensional array with ‘awk’ such as
1 2 3 4 5 6 ...
2 4 5 3 6 7 ...
5 3 2 4 5 1 ...
3 5 2 5 5 1 ...
.
.
into
Array[1]
3 6 ...
2 5 ...
1 4 ...
Array[2]
5 7 ...
4 6 ...
2 3 ...
.
.
Array[n]
I have tried different ways, but I am not even close to a solution. Please advise on how to achieve such endeavour...
Further, I was starting with 2D array and transform it to my needs... as
'BEGIN
for (x = 0; ++x <= 5;)
for (y = 0; ++y <= 5;)
A[x][y] = "element:" FS x FS y
for (i in A)
split(i, t, SUBSEP)
print A[t[1], t[2]]
'
But it is only for 2D... However, I was able to improve my answer since this posting with
awk '
for (i=1; i<=NF; i++)
a[NR,i] = $i
NF>p p = NF
END
for(j=1; j<=p; j++)
str=a[1,j]
for(i=2; i<=NR; i++)
str=str" "a[i,j];
print str
' f3a.dat > f4.dat ## Transpose
# Loop for all columns
awk 'print $1' f4.dat > f5.dat ## take out a column prior transformation into array
awk 'ORS=NR%3?FS:RS' f5.dat > f6.dat ## putting column into array::3=number of elements in row
awk '
for (i=1; i<=NF; i++)
a[NR,i] = $i
NF>p p = NF
END
for(j=1; j<=p; j++)
str=a[1,j]
for(i=2; i<=NR; i++)
str=str" "a[i,j];
print str
' f6.dat > f7.dat ## transpose
tac f7.dat > f8.dat ## reverse line order [UNIX command]
I reproduce the array[1] but it is not efficient as my real array to transform is [72x100000].
How can transform the above into a major loop AND each pseudo file f8.dat goes directly into a multidimensionnel array?
arrays multidimensional-array awk 2d
add a comment |
I would like to transform a 2D array into a multi-dimensional array with ‘awk’ such as
1 2 3 4 5 6 ...
2 4 5 3 6 7 ...
5 3 2 4 5 1 ...
3 5 2 5 5 1 ...
.
.
into
Array[1]
3 6 ...
2 5 ...
1 4 ...
Array[2]
5 7 ...
4 6 ...
2 3 ...
.
.
Array[n]
I have tried different ways, but I am not even close to a solution. Please advise on how to achieve such endeavour...
Further, I was starting with 2D array and transform it to my needs... as
'BEGIN
for (x = 0; ++x <= 5;)
for (y = 0; ++y <= 5;)
A[x][y] = "element:" FS x FS y
for (i in A)
split(i, t, SUBSEP)
print A[t[1], t[2]]
'
But it is only for 2D... However, I was able to improve my answer since this posting with
awk '
for (i=1; i<=NF; i++)
a[NR,i] = $i
NF>p p = NF
END
for(j=1; j<=p; j++)
str=a[1,j]
for(i=2; i<=NR; i++)
str=str" "a[i,j];
print str
' f3a.dat > f4.dat ## Transpose
# Loop for all columns
awk 'print $1' f4.dat > f5.dat ## take out a column prior transformation into array
awk 'ORS=NR%3?FS:RS' f5.dat > f6.dat ## putting column into array::3=number of elements in row
awk '
for (i=1; i<=NF; i++)
a[NR,i] = $i
NF>p p = NF
END
for(j=1; j<=p; j++)
str=a[1,j]
for(i=2; i<=NR; i++)
str=str" "a[i,j];
print str
' f6.dat > f7.dat ## transpose
tac f7.dat > f8.dat ## reverse line order [UNIX command]
I reproduce the array[1] but it is not efficient as my real array to transform is [72x100000].
How can transform the above into a major loop AND each pseudo file f8.dat goes directly into a multidimensionnel array?
arrays multidimensional-array awk 2d
Please do mention your efforts in your post too add let us know then.
– RavinderSingh13
Mar 7 at 23:48
RavinderSingh13, See above....
– Flapo
Mar 8 at 1:30
See above further improve
– Flapo
Mar 12 at 16:04
add a comment |
I would like to transform a 2D array into a multi-dimensional array with ‘awk’ such as
1 2 3 4 5 6 ...
2 4 5 3 6 7 ...
5 3 2 4 5 1 ...
3 5 2 5 5 1 ...
.
.
into
Array[1]
3 6 ...
2 5 ...
1 4 ...
Array[2]
5 7 ...
4 6 ...
2 3 ...
.
.
Array[n]
I have tried different ways, but I am not even close to a solution. Please advise on how to achieve such endeavour...
Further, I was starting with 2D array and transform it to my needs... as
'BEGIN
for (x = 0; ++x <= 5;)
for (y = 0; ++y <= 5;)
A[x][y] = "element:" FS x FS y
for (i in A)
split(i, t, SUBSEP)
print A[t[1], t[2]]
'
But it is only for 2D... However, I was able to improve my answer since this posting with
awk '
for (i=1; i<=NF; i++)
a[NR,i] = $i
NF>p p = NF
END
for(j=1; j<=p; j++)
str=a[1,j]
for(i=2; i<=NR; i++)
str=str" "a[i,j];
print str
' f3a.dat > f4.dat ## Transpose
# Loop for all columns
awk 'print $1' f4.dat > f5.dat ## take out a column prior transformation into array
awk 'ORS=NR%3?FS:RS' f5.dat > f6.dat ## putting column into array::3=number of elements in row
awk '
for (i=1; i<=NF; i++)
a[NR,i] = $i
NF>p p = NF
END
for(j=1; j<=p; j++)
str=a[1,j]
for(i=2; i<=NR; i++)
str=str" "a[i,j];
print str
' f6.dat > f7.dat ## transpose
tac f7.dat > f8.dat ## reverse line order [UNIX command]
I reproduce the array[1] but it is not efficient as my real array to transform is [72x100000].
How can transform the above into a major loop AND each pseudo file f8.dat goes directly into a multidimensionnel array?
arrays multidimensional-array awk 2d
I would like to transform a 2D array into a multi-dimensional array with ‘awk’ such as
1 2 3 4 5 6 ...
2 4 5 3 6 7 ...
5 3 2 4 5 1 ...
3 5 2 5 5 1 ...
.
.
into
Array[1]
3 6 ...
2 5 ...
1 4 ...
Array[2]
5 7 ...
4 6 ...
2 3 ...
.
.
Array[n]
I have tried different ways, but I am not even close to a solution. Please advise on how to achieve such endeavour...
Further, I was starting with 2D array and transform it to my needs... as
'BEGIN
for (x = 0; ++x <= 5;)
for (y = 0; ++y <= 5;)
A[x][y] = "element:" FS x FS y
for (i in A)
split(i, t, SUBSEP)
print A[t[1], t[2]]
'
But it is only for 2D... However, I was able to improve my answer since this posting with
awk '
for (i=1; i<=NF; i++)
a[NR,i] = $i
NF>p p = NF
END
for(j=1; j<=p; j++)
str=a[1,j]
for(i=2; i<=NR; i++)
str=str" "a[i,j];
print str
' f3a.dat > f4.dat ## Transpose
# Loop for all columns
awk 'print $1' f4.dat > f5.dat ## take out a column prior transformation into array
awk 'ORS=NR%3?FS:RS' f5.dat > f6.dat ## putting column into array::3=number of elements in row
awk '
for (i=1; i<=NF; i++)
a[NR,i] = $i
NF>p p = NF
END
for(j=1; j<=p; j++)
str=a[1,j]
for(i=2; i<=NR; i++)
str=str" "a[i,j];
print str
' f6.dat > f7.dat ## transpose
tac f7.dat > f8.dat ## reverse line order [UNIX command]
I reproduce the array[1] but it is not efficient as my real array to transform is [72x100000].
How can transform the above into a major loop AND each pseudo file f8.dat goes directly into a multidimensionnel array?
arrays multidimensional-array awk 2d
arrays multidimensional-array awk 2d
edited Mar 12 at 16:02
Flapo
asked Mar 7 at 23:46
FlapoFlapo
52
52
Please do mention your efforts in your post too add let us know then.
– RavinderSingh13
Mar 7 at 23:48
RavinderSingh13, See above....
– Flapo
Mar 8 at 1:30
See above further improve
– Flapo
Mar 12 at 16:04
add a comment |
Please do mention your efforts in your post too add let us know then.
– RavinderSingh13
Mar 7 at 23:48
RavinderSingh13, See above....
– Flapo
Mar 8 at 1:30
See above further improve
– Flapo
Mar 12 at 16:04
Please do mention your efforts in your post too add let us know then.
– RavinderSingh13
Mar 7 at 23:48
Please do mention your efforts in your post too add let us know then.
– RavinderSingh13
Mar 7 at 23:48
RavinderSingh13, See above....
– Flapo
Mar 8 at 1:30
RavinderSingh13, See above....
– Flapo
Mar 8 at 1:30
See above further improve
– Flapo
Mar 12 at 16:04
See above further improve
– Flapo
Mar 12 at 16:04
add a comment |
1 Answer
1
active
oldest
votes
Not quite sure what you are looking to do, but this awk script performs the transform specified:
awk -v STEP=3 '
for (i=0; i<NF; i++)
x = STEP - i%STEP;
a[x] = a[x] ? a[x] OFS $(i+1) : $(i+1);
printf "%sArray[%d]n", (FNR>1 ? "n" : ""), FNR;
for (i=1; i<=STEP; i++)
print a[i];
a[i]="";
' <<EOD
1 2 3 4 5 6
2 4 5 3 6 7
5 3 2 4 5 1
3 5 2 5 5 1
EOD
Wow... it works pretty well. Your solution is much shooter than mine and complete.
– Flapo
Mar 13 at 14:06
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2fstackoverflow.com%2fquestions%2f55054620%2fhow-to-manipulate-a-2d-array-into-a-multi-dimensional-array-in-awk%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Not quite sure what you are looking to do, but this awk script performs the transform specified:
awk -v STEP=3 '
for (i=0; i<NF; i++)
x = STEP - i%STEP;
a[x] = a[x] ? a[x] OFS $(i+1) : $(i+1);
printf "%sArray[%d]n", (FNR>1 ? "n" : ""), FNR;
for (i=1; i<=STEP; i++)
print a[i];
a[i]="";
' <<EOD
1 2 3 4 5 6
2 4 5 3 6 7
5 3 2 4 5 1
3 5 2 5 5 1
EOD
Wow... it works pretty well. Your solution is much shooter than mine and complete.
– Flapo
Mar 13 at 14:06
add a comment |
Not quite sure what you are looking to do, but this awk script performs the transform specified:
awk -v STEP=3 '
for (i=0; i<NF; i++)
x = STEP - i%STEP;
a[x] = a[x] ? a[x] OFS $(i+1) : $(i+1);
printf "%sArray[%d]n", (FNR>1 ? "n" : ""), FNR;
for (i=1; i<=STEP; i++)
print a[i];
a[i]="";
' <<EOD
1 2 3 4 5 6
2 4 5 3 6 7
5 3 2 4 5 1
3 5 2 5 5 1
EOD
Wow... it works pretty well. Your solution is much shooter than mine and complete.
– Flapo
Mar 13 at 14:06
add a comment |
Not quite sure what you are looking to do, but this awk script performs the transform specified:
awk -v STEP=3 '
for (i=0; i<NF; i++)
x = STEP - i%STEP;
a[x] = a[x] ? a[x] OFS $(i+1) : $(i+1);
printf "%sArray[%d]n", (FNR>1 ? "n" : ""), FNR;
for (i=1; i<=STEP; i++)
print a[i];
a[i]="";
' <<EOD
1 2 3 4 5 6
2 4 5 3 6 7
5 3 2 4 5 1
3 5 2 5 5 1
EOD
Not quite sure what you are looking to do, but this awk script performs the transform specified:
awk -v STEP=3 '
for (i=0; i<NF; i++)
x = STEP - i%STEP;
a[x] = a[x] ? a[x] OFS $(i+1) : $(i+1);
printf "%sArray[%d]n", (FNR>1 ? "n" : ""), FNR;
for (i=1; i<=STEP; i++)
print a[i];
a[i]="";
' <<EOD
1 2 3 4 5 6
2 4 5 3 6 7
5 3 2 4 5 1
3 5 2 5 5 1
EOD
answered Mar 13 at 0:02
jhncjhnc
2,235114
2,235114
Wow... it works pretty well. Your solution is much shooter than mine and complete.
– Flapo
Mar 13 at 14:06
add a comment |
Wow... it works pretty well. Your solution is much shooter than mine and complete.
– Flapo
Mar 13 at 14:06
Wow... it works pretty well. Your solution is much shooter than mine and complete.
– Flapo
Mar 13 at 14:06
Wow... it works pretty well. Your solution is much shooter than mine and complete.
– Flapo
Mar 13 at 14:06
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f55054620%2fhow-to-manipulate-a-2d-array-into-a-multi-dimensional-array-in-awk%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
Please do mention your efforts in your post too add let us know then.
– RavinderSingh13
Mar 7 at 23:48
RavinderSingh13, See above....
– Flapo
Mar 8 at 1:30
See above further improve
– Flapo
Mar 12 at 16:04