Printing ten values from each array alternating between them until both are emptyDeleting Column Selection in AWKawk - counting content of filesPassing parameters from shell to awk as an arrayPiping output from awk to perlUpdate last field with value from line count (awk?)awk print something if column is emptywhat happened when delete array element in awk?Awk comparing 3 values, 2nd file value between 1st file values with multiple column printout between both files to a 3rdIs preprocessing file with awk needed or it can be done directly in R?how to print/store duplicate values in array in awk

Most elegant way to write a one shot IF

How do I tell the reader that my character is autistic in Fantasy?

Why do changes to /etc/hosts take effect immediately?

Step into the Octagram

Can a function nowhere continuous have a connected graph?

Is it okay to fade a human face just to create some space to place important content over it?

How would an order of Monks that renounce their names communicate effectively?

Does flying two boosters close together affect efficiency?

Ordered list of OR journals

What does the phrase "building hopping chop" mean here?

What game is this character in the Pixels movie from?

Balanced parentheses using STL C++

How did Lefschetz do mathematics without hands?

Who are these Discworld wizards from this picture?

Can Aziraphale and Crowley actually become native?

Security Patch SUPEE-11155 - Possible issues?

Golf the smallest circle!

How can my story take place on Earth without referring to our existing cities and countries?

Should I report a leak of confidential HR information?

Why would anyone even use a Portkey?

Can an editor review manuscript without sending to reviewers?

Is it legal to call shared_future::get() multiple times on the same instance in the same thread?

Was it really unprofessional of me to leave without asking for a raise first?

Can I travel from Germany to England alone as an unaccompanied minor?



Printing ten values from each array alternating between them until both are empty


Deleting Column Selection in AWKawk - counting content of filesPassing parameters from shell to awk as an arrayPiping output from awk to perlUpdate last field with value from line count (awk?)awk print something if column is emptywhat happened when delete array element in awk?Awk comparing 3 values, 2nd file value between 1st file values with multiple column printout between both files to a 3rdIs preprocessing file with awk needed or it can be done directly in R?how to print/store duplicate values in array in awk






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















Suppose I have the following script and it is iterating over two arrays and prints the the values alternating between them. I feed the Script the following file:



1 2
1 2
1 2
1 2
1 2
1 2


Awk Script:



BEGIN FS = "t" 
a[o++]=$1; b[c++]=$2
END while (++co <= 5 )
for ( k = 1+count; k <= 2+count; k++ ) if (length(a) >= 0) print a[k]; delete a[k]
for ( ki = 1+count; ki <= 2+count; ki++ ) if (length(b) >= 0) print b[ki]; delete b[ki]
count = k




And I expect the output to be



1
1
2
2
1
1
2
2
1
1
2
2


But what I get is:



1
1
2
2
1
1
2
2

(followed by blank lines but they are not my issue)


So how to make it run until both arrays are empty and everything is printed. And how to make it work even if the arrays are of different size e.g. one caontains for example 20 more values than the other. So then just these values should be printed until the array is empty.










share|improve this question
























  • If you adhere to the MCVE principle, you will receive better answers. My guess is that your code is failing because you delete elements 1-10 and then continue referring to them

    – Thor
    Mar 25 at 11:25











  • Soory that this is not fully complete but it is part of a real big script. But your suggestion makes sense to me and I see the error now. Let me try something and then I either rewrite the question or I close it. Thank you.

    – JFS31
    Mar 25 at 11:30











  • Hope its better now :)

    – JFS31
    Mar 25 at 11:56






  • 2





    awk arrays are one-indexed. Where you collect data into a and b, you write the first data to a[0] and b[0] which are never printed

    – Thor
    Mar 25 at 12:25












  • @Thor Very good point. I overlooked that. Thank you :)

    – JFS31
    Mar 25 at 13:25

















1















Suppose I have the following script and it is iterating over two arrays and prints the the values alternating between them. I feed the Script the following file:



1 2
1 2
1 2
1 2
1 2
1 2


Awk Script:



BEGIN FS = "t" 
a[o++]=$1; b[c++]=$2
END while (++co <= 5 )
for ( k = 1+count; k <= 2+count; k++ ) if (length(a) >= 0) print a[k]; delete a[k]
for ( ki = 1+count; ki <= 2+count; ki++ ) if (length(b) >= 0) print b[ki]; delete b[ki]
count = k




And I expect the output to be



1
1
2
2
1
1
2
2
1
1
2
2


But what I get is:



1
1
2
2
1
1
2
2

(followed by blank lines but they are not my issue)


So how to make it run until both arrays are empty and everything is printed. And how to make it work even if the arrays are of different size e.g. one caontains for example 20 more values than the other. So then just these values should be printed until the array is empty.










share|improve this question
























  • If you adhere to the MCVE principle, you will receive better answers. My guess is that your code is failing because you delete elements 1-10 and then continue referring to them

    – Thor
    Mar 25 at 11:25











  • Soory that this is not fully complete but it is part of a real big script. But your suggestion makes sense to me and I see the error now. Let me try something and then I either rewrite the question or I close it. Thank you.

    – JFS31
    Mar 25 at 11:30











  • Hope its better now :)

    – JFS31
    Mar 25 at 11:56






  • 2





    awk arrays are one-indexed. Where you collect data into a and b, you write the first data to a[0] and b[0] which are never printed

    – Thor
    Mar 25 at 12:25












  • @Thor Very good point. I overlooked that. Thank you :)

    – JFS31
    Mar 25 at 13:25













1












1








1








Suppose I have the following script and it is iterating over two arrays and prints the the values alternating between them. I feed the Script the following file:



1 2
1 2
1 2
1 2
1 2
1 2


Awk Script:



BEGIN FS = "t" 
a[o++]=$1; b[c++]=$2
END while (++co <= 5 )
for ( k = 1+count; k <= 2+count; k++ ) if (length(a) >= 0) print a[k]; delete a[k]
for ( ki = 1+count; ki <= 2+count; ki++ ) if (length(b) >= 0) print b[ki]; delete b[ki]
count = k




And I expect the output to be



1
1
2
2
1
1
2
2
1
1
2
2


But what I get is:



1
1
2
2
1
1
2
2

(followed by blank lines but they are not my issue)


So how to make it run until both arrays are empty and everything is printed. And how to make it work even if the arrays are of different size e.g. one caontains for example 20 more values than the other. So then just these values should be printed until the array is empty.










share|improve this question
















Suppose I have the following script and it is iterating over two arrays and prints the the values alternating between them. I feed the Script the following file:



1 2
1 2
1 2
1 2
1 2
1 2


Awk Script:



BEGIN FS = "t" 
a[o++]=$1; b[c++]=$2
END while (++co <= 5 )
for ( k = 1+count; k <= 2+count; k++ ) if (length(a) >= 0) print a[k]; delete a[k]
for ( ki = 1+count; ki <= 2+count; ki++ ) if (length(b) >= 0) print b[ki]; delete b[ki]
count = k




And I expect the output to be



1
1
2
2
1
1
2
2
1
1
2
2


But what I get is:



1
1
2
2
1
1
2
2

(followed by blank lines but they are not my issue)


So how to make it run until both arrays are empty and everything is printed. And how to make it work even if the arrays are of different size e.g. one caontains for example 20 more values than the other. So then just these values should be printed until the array is empty.







linux awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 12:20









Thor

32k7 gold badges85 silver badges98 bronze badges




32k7 gold badges85 silver badges98 bronze badges










asked Mar 25 at 11:10









JFS31JFS31

4533 silver badges13 bronze badges




4533 silver badges13 bronze badges












  • If you adhere to the MCVE principle, you will receive better answers. My guess is that your code is failing because you delete elements 1-10 and then continue referring to them

    – Thor
    Mar 25 at 11:25











  • Soory that this is not fully complete but it is part of a real big script. But your suggestion makes sense to me and I see the error now. Let me try something and then I either rewrite the question or I close it. Thank you.

    – JFS31
    Mar 25 at 11:30











  • Hope its better now :)

    – JFS31
    Mar 25 at 11:56






  • 2





    awk arrays are one-indexed. Where you collect data into a and b, you write the first data to a[0] and b[0] which are never printed

    – Thor
    Mar 25 at 12:25












  • @Thor Very good point. I overlooked that. Thank you :)

    – JFS31
    Mar 25 at 13:25

















  • If you adhere to the MCVE principle, you will receive better answers. My guess is that your code is failing because you delete elements 1-10 and then continue referring to them

    – Thor
    Mar 25 at 11:25











  • Soory that this is not fully complete but it is part of a real big script. But your suggestion makes sense to me and I see the error now. Let me try something and then I either rewrite the question or I close it. Thank you.

    – JFS31
    Mar 25 at 11:30











  • Hope its better now :)

    – JFS31
    Mar 25 at 11:56






  • 2





    awk arrays are one-indexed. Where you collect data into a and b, you write the first data to a[0] and b[0] which are never printed

    – Thor
    Mar 25 at 12:25












  • @Thor Very good point. I overlooked that. Thank you :)

    – JFS31
    Mar 25 at 13:25
















If you adhere to the MCVE principle, you will receive better answers. My guess is that your code is failing because you delete elements 1-10 and then continue referring to them

– Thor
Mar 25 at 11:25





If you adhere to the MCVE principle, you will receive better answers. My guess is that your code is failing because you delete elements 1-10 and then continue referring to them

– Thor
Mar 25 at 11:25













Soory that this is not fully complete but it is part of a real big script. But your suggestion makes sense to me and I see the error now. Let me try something and then I either rewrite the question or I close it. Thank you.

– JFS31
Mar 25 at 11:30





Soory that this is not fully complete but it is part of a real big script. But your suggestion makes sense to me and I see the error now. Let me try something and then I either rewrite the question or I close it. Thank you.

– JFS31
Mar 25 at 11:30













Hope its better now :)

– JFS31
Mar 25 at 11:56





Hope its better now :)

– JFS31
Mar 25 at 11:56




2




2





awk arrays are one-indexed. Where you collect data into a and b, you write the first data to a[0] and b[0] which are never printed

– Thor
Mar 25 at 12:25






awk arrays are one-indexed. Where you collect data into a and b, you write the first data to a[0] and b[0] which are never printed

– Thor
Mar 25 at 12:25














@Thor Very good point. I overlooked that. Thank you :)

– JFS31
Mar 25 at 13:25





@Thor Very good point. I overlooked that. Thank you :)

– JFS31
Mar 25 at 13:25












1 Answer
1






active

oldest

votes


















1














wrt how to make it run until both arrays are empty - There's no need to delete the array contents.



wrt just these values should be printed until the array is empty - "the array is empty" could mean print just the values at the indices present in both arrays ("the first array is empty") or it could mean print all the values from both arrays ("both arrays are empty"). The script below assumes the latter but is easily tweaked to do the former (change || to &&).



$ cat tst.awk
a[++maxA]=$1; b[++maxB]=$2
END (prevEnd<maxB) )
prt(a)
prevEnd = prt(b)



function prt(arr, idx)
for (idx=prevEnd+1; idx<=prevEnd+2; idx++)
if (idx in arr)
print arr[idx]


return (idx-1)


$ awk -f tst.awk file
1
1
2
2
1
1
2
2
1
1
2
2





share|improve this answer

























  • Yeah, you are right with not deleting the content.

    – JFS31
    Mar 25 at 13:27










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%2f55336470%2fprinting-ten-values-from-each-array-alternating-between-them-until-both-are-empt%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









1














wrt how to make it run until both arrays are empty - There's no need to delete the array contents.



wrt just these values should be printed until the array is empty - "the array is empty" could mean print just the values at the indices present in both arrays ("the first array is empty") or it could mean print all the values from both arrays ("both arrays are empty"). The script below assumes the latter but is easily tweaked to do the former (change || to &&).



$ cat tst.awk
a[++maxA]=$1; b[++maxB]=$2
END (prevEnd<maxB) )
prt(a)
prevEnd = prt(b)



function prt(arr, idx)
for (idx=prevEnd+1; idx<=prevEnd+2; idx++)
if (idx in arr)
print arr[idx]


return (idx-1)


$ awk -f tst.awk file
1
1
2
2
1
1
2
2
1
1
2
2





share|improve this answer

























  • Yeah, you are right with not deleting the content.

    – JFS31
    Mar 25 at 13:27















1














wrt how to make it run until both arrays are empty - There's no need to delete the array contents.



wrt just these values should be printed until the array is empty - "the array is empty" could mean print just the values at the indices present in both arrays ("the first array is empty") or it could mean print all the values from both arrays ("both arrays are empty"). The script below assumes the latter but is easily tweaked to do the former (change || to &&).



$ cat tst.awk
a[++maxA]=$1; b[++maxB]=$2
END (prevEnd<maxB) )
prt(a)
prevEnd = prt(b)



function prt(arr, idx)
for (idx=prevEnd+1; idx<=prevEnd+2; idx++)
if (idx in arr)
print arr[idx]


return (idx-1)


$ awk -f tst.awk file
1
1
2
2
1
1
2
2
1
1
2
2





share|improve this answer

























  • Yeah, you are right with not deleting the content.

    – JFS31
    Mar 25 at 13:27













1












1








1







wrt how to make it run until both arrays are empty - There's no need to delete the array contents.



wrt just these values should be printed until the array is empty - "the array is empty" could mean print just the values at the indices present in both arrays ("the first array is empty") or it could mean print all the values from both arrays ("both arrays are empty"). The script below assumes the latter but is easily tweaked to do the former (change || to &&).



$ cat tst.awk
a[++maxA]=$1; b[++maxB]=$2
END (prevEnd<maxB) )
prt(a)
prevEnd = prt(b)



function prt(arr, idx)
for (idx=prevEnd+1; idx<=prevEnd+2; idx++)
if (idx in arr)
print arr[idx]


return (idx-1)


$ awk -f tst.awk file
1
1
2
2
1
1
2
2
1
1
2
2





share|improve this answer















wrt how to make it run until both arrays are empty - There's no need to delete the array contents.



wrt just these values should be printed until the array is empty - "the array is empty" could mean print just the values at the indices present in both arrays ("the first array is empty") or it could mean print all the values from both arrays ("both arrays are empty"). The script below assumes the latter but is easily tweaked to do the former (change || to &&).



$ cat tst.awk
a[++maxA]=$1; b[++maxB]=$2
END (prevEnd<maxB) )
prt(a)
prevEnd = prt(b)



function prt(arr, idx)
for (idx=prevEnd+1; idx<=prevEnd+2; idx++)
if (idx in arr)
print arr[idx]


return (idx-1)


$ awk -f tst.awk file
1
1
2
2
1
1
2
2
1
1
2
2






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 25 at 13:28

























answered Mar 25 at 13:19









Ed MortonEd Morton

120k13 gold badges46 silver badges106 bronze badges




120k13 gold badges46 silver badges106 bronze badges












  • Yeah, you are right with not deleting the content.

    – JFS31
    Mar 25 at 13:27

















  • Yeah, you are right with not deleting the content.

    – JFS31
    Mar 25 at 13:27
















Yeah, you are right with not deleting the content.

– JFS31
Mar 25 at 13:27





Yeah, you are right with not deleting the content.

– JFS31
Mar 25 at 13:27








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















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%2f55336470%2fprinting-ten-values-from-each-array-alternating-between-them-until-both-are-empt%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

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript