Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7959539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:35:23+00:00 2026-06-04T04:35:23+00:00

I am currently writing an awk script within a bash script. One of my

  • 0

I am currently writing an awk script within a bash script. One of my arguments needs to be split and cycled through. Eg: for an argument of 1234 I need to cycle through each number in the order written. So, 2413 is not equivalent.

I used split to create an array and then a for-in loop to cycle through the array. I assumed it would cycle through in order, but it is not.

My code is as follows:

split(cols,toShow,"")
for (c in toShow)
    printf "%s\n",c

cols is passed to the awk command using the -v option and gives the following output:

4
1
2
3

After testing this a few times, with varying lengths of arguments and using both numbers and letters, it appears that the for loop starts at element 4 of the array, cycles through to the end in order, then cycles through elements 1 to 3, instead of the expected starting at element 1 and cycling to the end.

Is there anyway to change the behaviour or am I doing something wrong?

EDIT For clarification, I am using gawk in xubuntu 11.10

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:35:23+00:00Added an answer on June 4, 2026 at 4:35 am

    arrays in awk aren’t (necessarily) stored in the order from the original source.

    Also, using numbers as your input is confusing the issue

    Here is a solution that will illustrate the problem

    awk 'END{cols="ABCD";n=split(cols,toShow,"")
    for (i=1;i<=n;i++)
        printf "%s\n",toShow[i] }' /dev/null
    

    ** output**

    A
    B
    C
    D
    

    To see what I mean, edit your code to ABCD and you’ll still the your numeric output as for c in toShow is printing the keys and not the values of the assoc array.

    If you edit ABCD in my sample, to 1234, you’ll get the output you’re looking for.

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently writing an app in Python and need to provide localization for it.
I'm currently writing a small script using pygame, but I don't believe this question
I'm currently writing an application that allows one to store images, and then tag
I'm currently writing a application that needs to capture some images. I start the
I'm currently writing a script that exports data from a list of tables and
I'm currently writing a script that processes batches of quicktimes, and its my first
I am currently writing a networking assignment for one of my college classes and
I'm on OS X and writing a bash script to edit text in a
I'm currently writing a little toy assembler in c# (going through the elements of
I'm currently writing unit tests for my Ruby on Rails application, and one of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.