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?













0















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?










share|improve this question
























  • 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















0















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?










share|improve this question
























  • 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













0












0








0








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












1 Answer
1






active

oldest

votes


















0














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






share|improve this answer























  • Wow... it works pretty well. Your solution is much shooter than mine and complete.

    – Flapo
    Mar 13 at 14:06










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
);



);













draft saved

draft discarded


















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









0














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






share|improve this answer























  • Wow... it works pretty well. Your solution is much shooter than mine and complete.

    – Flapo
    Mar 13 at 14:06















0














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






share|improve this answer























  • Wow... it works pretty well. Your solution is much shooter than mine and complete.

    – Flapo
    Mar 13 at 14:06













0












0








0







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived