• bash script help splitting string

    Unsolved IT Discussion
    3
    0 Votes
    3 Posts
    632 Views
    JaredBuschJ

    @matteo-nunziati said in bash script help splitting string:

    try this:

    # initializing agi variables declare -a array while read -e ARG && [ "$ARG" ] ; do array=(` echo "$ARG" | sed -e 's/://'`) export ${array[0]}=${array[@]:1} #next added line for debug only. comment this out in prod. echo "${array[0]}=${array[@]:1}" done

    if you want to "slice" an array use the syntax: ${array[@]:from:to}, not providing the 'to' arg means go up to the end of array.
    source here.

    Almost right. Because of the export command parsing spaces, I needed to stick it in a variable first.

    # Variable to hold the details for the log file DUMPARG=" Begin Argument dump:\n" # Create an Array to hold the results of the loop declare -a array # Loop through the AGI variables while read -e ARG && [ "$ARG" ] ; do # Dump them into an array, after removing the : array=(` echo $ARG | sed -e 's/://'`) # take the array and create a variable from the first element put the rest as the value # value must be put into a holding variable to parse correctly by the export command val=${array[@]:1} export ${array[0]}="$val" # Dump them into a string for the log file DUMPARG="$DUMPARG $ARG\n" done
  • 0 Votes
    2 Posts
    1k Views
    lanceL

    I ended up finding that if I add another for statement and nested in the first for loop and then cout some blank spaces I was able to achieve what I was looking to do. Below is what I had to do.

    for(i = 0; i < num_rows; i++) { for(j = 0; j < num_cols_1; j++) { // print j'th element in i'th row of array 1 } // print some spaces for(j = 0; j < num_cols_2; j++) { // print j'th element in i'th row of array 2 } }