7 Replies Latest reply on Jul 29, 2013 11:48 AM by Alexander Szostek

    Using CSV as input file

      Right now I have a script that uses a CSV file as an input file like seen below:

       

      /Depot/Engineering,test 1,

      /Depot/Engineering,test 2,

      /Depot/Engineering,test 3,

      /Depot/Engineering,test a,

      /Depot/Engineering,test b,

      /Depot/Engineering,test c,

      end,end

       

      The CSV file has two columns, the first one is the package locations, and the second column is the package name.  My script looks something like  below:

       

      while read Line

        do

        Group=`echo $Line | cut -d ',' -f1`

        Name=`echo $Line | cut -d ',' -f2`

       

        groupName=`echo $Group | sed -r 's/^.{6}//'`

        packageName="$Name"

       

        echo "Group path: " "$groupName"

        echo "Package name: " "$packageName"

       

      Right now the script only works if I put a comma at the end of each row.  I was wonder if there is some way I could change the script so the comma is not need at the end of each row.

        • 1. Re: Using CSV as input file
          Bill Robinson

          is this on windows - maybe there are some control characters here?  this works fine:

           

          while read line

          do

          GROUP="$(echo ${line} | cut -f1 -d,)"

          NAME="$(echo ${line} | cut -f2 -d,)"

          echo "${GROUP##*/}"

          echo "${NAME}"

          done < file.csv

          • 2. Re: Using CSV as input file
            Sean Berry

            Alex:

             

            When you do this:

             

            Name=`echo $Line | cut -d ',' -f2`


            And the result of that echo statement has a space in it, but there's no double quotes around the back ticks, it won't be passed properly into the $Name variable.

            • 3. Re: Using CSV as input file

              I'm still unable to get it to work. I tried:

               

              while read line

              do

              GROUP="$(echo ${line} | cut -f1 -d,)"

              NAME="$(echo ${line} | cut -f2 -d,)"

              echo "${GROUP##*/}"

              echo "${NAME}"

              done < file.csv

               

              but that didn't work either.  Is there a way to have the script look for a newline break? like:

               

              NAME="$(echo ${line} | cut -f2 -newline,)"

              • 4. Re: Using CSV as input file
                Bill Robinson

                So what about that didn’t work ?  what does that output ?

                • 5. Re: Using CSV as input file

                  It says "cannot find depot object by name : test 1" , and there is package by that name.

                   

                  I can get it to work without the comma at the end if I just echo the string into the prompt, but when I try to use in the argument of the blcli command, that is were I am having the problem.

                  • 6. Re: Using CSV as input file
                    Bill Robinson

                    can you include the full script you are using as well as the csv file ?

                    • 7. Re: Using CSV as input file

                      Yes, I am using a csv file.  I am pasting the full script below:

                      ________________________________________________________________________

                      rm D:/BLEngineering/Interns/Alex/PROJECTS/output.csv
                      touch D:/BLEngineering/Interns/Alex/PROJECTS/output.csv
                      rm D:/BLEngineering/Interns/Alex/PROJECTS/FailedOutput.csv
                      touch D:/BLEngineering/Interns/Alex/PROJECTS/FailedOutput.csv


                      while read Line
                      do

                        Group="$(echo ${Line} | cut -f1 -d,)"
                        Name="$(echo ${Line} | cut -f2 -d,)"

                        groupName=`echo $Group | sed -r 's/^.{6}//'`
                        packageName="$Name"


                      blcli_execute BlPackage getDBKeyByGroupAndName "$groupName" "$packageName"
                      blcli_storeenv PackageDB
                      blcli_execute DepotObject addPermission "$PackageDB" "BLAdmins" "BLPackage.*"

                       

                      PACKAGE_DBKEY=`blcli BlPackage getDBKeyByGroupAndName "$groupName" "$packageName"`
                      ERROR=`echo "$PACKAGE_DBKEY" | grep failed`

                      if [ "$ERROR" = "" ]
                            then

                      echo "#### Deleting a BlPackage by group and Name ####"
                      blcli_execute BlPackage deleteBlPackageByGroupAndName "$groupName" "$packageName"

                           if test $? -ne 1
                                        then
                                             echo "$groupName","$packageName" >> output.csv
                               echo -e \\r >> output.csv
                        else
                         echo "$groupName","$packageName" >> FailedOutput.csv
                                echo -e \\r >> FailedOutput.csv
                         echo "#### Error : Unable to delete package ####"
                                  fi

                      fi   


                      done < D:/BLEngineering/Interns/Alex/Test/PackageListTest.csv

                      echo " "

                      ________________________________________________________________