0

I can't believe I'm here for such a trivial issue, but I honestly have no idea what's going on. I've been writing shell scripts for 20 years now and never encountered an issue like this.

I've got a section of code where I'm looking for a match, forming some variables, and then spitting them out to a CSV. The overall script works, but the output to CSV isn't acting as it should. I've added a debug statement or two, and those are acting weird as well.

    else
        # We assume there was no direct match, let's see if we can remove the CID from the asset name
        NewDevName=`echo $DEVNAME | perl -pe "s/^(\d{4})\-//"`
        echo -ne "Trying $NewDevName..."

        if grep -q $NewDevName sl1_registry.csv;
        then
            echo "Found match with modified name"
            IP=`grep $NewDevName sl1_registry.csv | cut -f3 -d\, | head -1`
            CID=`grep $NewDevName snow_cmdb_asset_ci.csv | cut -f2 -d\, | head -1`
            COMPANY=`grep $NewDevName sl1_registry.csv | cut -f7 -d\, | head -1`
            REGION=`grep $NewDevName sl1_registry.csv | cut -f11 -d\, | head -1`
            echo "Debug Values: Name:$NewDevName, IP:$IP, Company:$COMPANY"
            echo "Test ${NewDevName},${IP},${COMPANY},${CID},${REGION},${DEVNAME} Foobar "
            echo "$NewDevName,$IP,$COMPANY,$CID,$REGION,$DEVNAME" >> tempdb.csv
        else
            echo "$DEVNAME,NULL,NULL,NULL,NULL " >> tempdb.csv
            echo "No Match Found"
        fi
    fi

I've had to scrub a lot of this, but I tried to do it in such a way that you can still see what's going on. The output is as follows:

Searching 1059-XYZ-123...Trying XYZ-123...Found match with modified name
Debug Values: Name:XYZ-123, IP:100.X.35.252, Company:ACME
,1059-XYZ-123 Foobar 5.252,ACME,1059,US

I don't seem to be able to format in color, but please note the word "Foobar" and where it appears in the output vs where it appears in the code statement.

Also note, the word "Test" (beginning of the second echo statement) is missing completely.

Lastly, you can see the last bit of the IP (100.X.35.252) is included as part of the device name in the output (5.252)

As far as I can guess, there has to be some sort of weird unicode/escape thing going on that is messing up 'something', but I have no idea what that could be. Nothing makes sense with this. I thought maybe it was an issue with how my Mac is running the code, but I also tried it on Ubuntu with the exact same results.

  • 1
    `$REGION` ends with a carriage return, likely because your CSV file uses DOS line endings. As a result, the cursor moves to the beginning of the line and `$DEVNAME, Foobar` overwrites what was previously written. – chepner Aug 11 '21 at 15:39
  • Look at the output in `od -c` or similar – jhnc Aug 11 '21 at 15:43
  • Please try to write question titles that _uniquely identify the specific behavior_ that you're asking for help with, so someone can tell if they have the same problem (and will benefit from your question's answers) just by reading the title. – Charles Duffy Aug 11 '21 at 16:55
  • BTW, it's a lot more efficient to do something like `IFS=, read -r field ip company rest < <(grep -e "$NewDevName" sl1_registry.csv)` -- that you you only run `grep` _once_ (instead of starting a separate copy for each field you want to extract), and don't need to spawn copies of `cut` or `head` at all. Remember that `grep`, `cut`, `head`, etc. are all separate programs -- starting a program is a lot slower than using shell-builtin primitives, even if that program is faster than the shell's builtins after it's running. – Charles Duffy Aug 11 '21 at 16:56
  • @CharlesDuffy isn't `< <(...)` a bash-ism? – jhnc Aug 11 '21 at 18:29
  • @jhnc, yes, it is. I'm only now noticing that this is flagged `sh`. – Charles Duffy Aug 11 '21 at 18:31
  • @CharlesDuffy could fix with `< – jhnc Aug 11 '21 at 18:34

1 Answers1

0

Ran my files through dos2unix and that seems to have cleared things up.