0

First let me say I followed questions on stackoverflow.com that relate to my question and it seems the rules are not applying. Let me show you.

The following script:

    #!/bin/bash
    OUTPUT_DIR=/share/es-ops/Build_Farm_Reports/WorkSpace_Reports
    TODAY=`date +"%m-%d-%y"`
    HOSTNAME=`hostname`
    WORKSPACES=( "bob" "mel" "sideshow-ws2" )
    if ! [ -f $OUTPUT_DIR/$HOSTNAME.csv ] && [ $HOSTNAME == "sideshow" ]; then
    echo "$TODAY","$HOSTNAME" >  $OUTPUT_DIR/$HOSTNAME.csv
    echo "${WORKSPACES[0]}," >> $OUTPUT_DIR/$HOSTNAME.csv
    sed -i "/^'"${WORKSPACES[0]}"'/$/'"${WORKSPACES[1]}"'/" $OUTPUT_DIR/$HOSTNAME.csv
    sed -i "/^'"${WORKSPACES[1]}"'/$/${WORKSPACES[2]}"'/" $OUTPUT_DIR/$HOSTNAME.csv
    fi

I want the output to look like:

09-20-14,sideshow
bob,mel,sideshow-ws2

the sed statements are supposed to append successive array elements to preceding ones on the same line. Now I know there's a simpler way to do this like:

echo "${WORKSPACES[0]},${WORKSPACES[1]},${WORKSPACES[2]}" >> $OUTPUT_DIR/$HOSTNAME.csv

But let's say I had 30 elements in the array and I wanted to appended them one after the other on the same line? Can you show me how to loop through the elements in an array and append them one after the other on the same line?

Also let's say I had the output of a command like:

df -m /export/ws/$ws | awk '{if (NR!=1) {print $3}}'

and I wanted to append that to the end of the same line.

But when I run it I get:

    + OUTPUT_DIR=/share/es-ops/Build_Farm_Reports/WorkSpace_Reports
    ++ date +%m-%d-%y
    + TODAY=09-20-14
    ++ hostname
    + HOSTNAME=sideshow
    + WORKSPACES=("bob" "mel" "sideshow-ws2")
    + '[' -f /share/es-ops/Build_Farm_Reports/WorkSpace_Reports/sideshow.csv ']'

And the file right now looks like:

09-20-14,sideshow
bob,

I am happy to report that user syme solved this (see below) but then I realized I need the date in the first column:

09-7-14,bob,mel,sideshow-ws2

Can I do this using syme's for loop?

Okay user syme solved this too he said "Just add $TODAY to the for loop" like this:

for v in "$TODAY" "${WORKSPACES[@]}"

Okay now the output looks like this I changed the elements in the array btw:

sideshow
09-20-14,bob_avail,bob_used,mel_avail,mel_used,sideshow-ws2_avail,sideshow-ws2_used

Now below that the next line will be populated by a , in the first column skipping the date and then:

df -m /export/ws/$v | awk '{if (NR!=1) {print $3}}

which equals the value of available space on bob in the first iteration

and then:

df -m /export/ws/$v | awk '{if (NR!=1) {print $2}}

which equals the value of used space on bob in the 2nd iteration

and then we just move on to the next value in ${WORKSPACE[@]}

which will be mel and do the available and used as we did with bob or $v above.

I know you geniuses on here will make child's play out of this.

I solved my own last question on this thread:

    WORKSPACES2=( "bob" "mel" "sideshow-ws2" )
    separator="," # defined empty for the first value
    for v in "${WORKSPACES2[@]}"
    do
    available=`df -m /export/ws/$v | awk '{if (NR!=1) {print $3}}'`
    used=`df -m /export/ws/$v | awk '{if (NR!=1) {print $2}}'`
      echo -n "$separator$available$separator$used" >> $OUTPUT_DIR/$HOSTNAME.csv # append, concatenated, the separator and the value to the file
    done

produces:

sideshow
09-20-14,bob_avail,bob_used,mel_avail,mel_used,sideshow-ws2_avail,sideshow-ws2_used
,470400,1032124,661826,1032124,43443,1032108
5
  • The back-tick on the 2 lines with sed are not closed. But I do not think this is your problem. I just do not understand what you are trying to do with those 2 lines: 1. sed -i modifies the file given, no output is expected (to be printed by echo). 2. Syntax error character 9 at first sed (as you can see...), what do you want sed to do ? 3. You should not use back-ticks (deprecated), use $(...) instead. 4. I think you should review your "s and 's, they are so weird. Commented Sep 20, 2014 at 21:21
  • I got rid of the echo and the >> and put the trailing back ticks but I still am getting the same kind of errors. What I want sed to do is output bob,mel,sideshow-ws2 on the second line. Thanks for your help. Commented Sep 20, 2014 at 21:31
  • Again: I do not understand what you are trying to do with sed ? If just want to print the values of WORKSPACES separated with ,, just used redirected echo "${WORKSPACES[0]},${WORKSPACES[1]},${WORKSPACES[2]}" ? No ? I do not understand. Commented Sep 20, 2014 at 21:43
  • Sure syme I hear ya that applies in this example, but for a larger list and for values that are derived from the output of a command there should be a way to pass variables into sed so you can append. This script is just the beginning of a much larger script. But I like your idea though. Okay let's say I had 15 elements in the array. Can you show me a for loop that would change the number in `[@]' each time the loop iterates? Commented Sep 20, 2014 at 21:46
  • Oh ok! I am going to post an answer. Commented Sep 20, 2014 at 22:06

1 Answer 1

1

echo -n permits to print text without the linebreak.

To loop over the values of the array, you can use a for-loop:

echo "$TODAY,$HOSTNAME" >  $OUTPUT_DIR/$HOSTNAME.csv # with a linebreak
separator="" # defined empty for the first value
for v in "${WORKSPACES[@]}"
do
  echo -n "$separator$v" >> $OUTPUT_DIR/$HOSTNAME.csv # append, concatenated, the separator and the value to the file
  separator="," # comma for the next values
done
echo >> $OUTPUT_DIR/$HOSTNAME.csv # add a linebreak (if you want it)
Sign up to request clarification or add additional context in comments.

3 Comments

what If I wanted to put $TODAY in the first column like: 09-7-14,bob,mel,sideshow-ws2
Just add it to the list of the for-loop: for v in "$TODAY" "${WORKSPACES[@]}"
You da man syme thanks. If I have another question should I add it to the same question or start another thread?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.