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

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Posting a File and Associated Data to a RESTful WebService preferably as JSON2019 Community Moderator ElectionREST URI convention - Singular or plural name of resource while creating itUploading both data and files in one form using Ajax?How do I upload a file with metadata using a REST web service?REST API - file (ie images) processing - best practicesSpring: JSON data and file in the same requestREST API Put large dataPosting a file and JSON data to Spring rest serviceRESTful Web Service Upload/Download Large Data With JSONREST API Design sending JSON data and a file to the api in same requestPost JSONArray to REST servicePUT vs. POST in RESTParsing values from a JSON file?JavaScript/jQuery to download file via POST with JSON dataHow do I upload a file with metadata using a REST web service?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Separate REST JSON API server and client?what's the correct way to send a file from REST web service to client?How do I write JSON data to a file?What is “406-Not Acceptable Response” in HTTP?REST API - file (ie images) processing - best practices

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page