Extracting data from extension attribute cleanup

akamenev47
Contributor II

Hello,

Not sure if anybody has seen such issue before, it should be an easy one, but I can't get it accomplished.

So I extract data from one of the extension attributes into a variable and pass it to the clipboard. All the data is passed fine, but it also includes an extra blank string or a string with non-visible characters.

I am unable to remove that blank/mysterious string.... Have tried sed, awk, grep, xargs... it either does not do anything or removes the string with the data - leaving the blank string...
Adding the things I have tried:
sed -i '/^$/d' ~/temp1
awk NF ~/temp > ~/temp1
grep "[!-~]" ~/temp > ~/temp1
cleanedvar="$uncleanedvar" | xargs

When I ran: od -c temp
I got:
0000000 N a t F B 0 2 j g = = 0000014

0000014 per octal is '8', yet it can't be seen in the file, it's just blank string, I feel like I am going crazy here, any assistance appreciated.

PS. I will share the script once it is fully running :)

Ahoy!
6 REPLIES 6

mm2270
Legendary Contributor III

Can you post the commands you've already tried, just so people don't recommend something you already attempted?

akamenev47
Contributor II

Sure,

Tried these:

sed -i '/^$/d' ~/temp1

awk NF ~/temp > ~/temp1

grep "[!-~]" ~/temp > ~/temp1

cleanedvar="$uncleanedvar" | xargs

Ahoy!

mm2270
Legendary Contributor III

Let me ask - how are you detecting the blank string you mentioned? What is it that's clueing you in to the fact that it's there? Is it possible what you're seeing is actually a new line or carriage return? If it's a Windows carriage return, those tend to show up as a new blank line along with the rest of the data. I have run into those in some curl commands, but never with a call from the API. Usually when curling down data from a website. But it's possible since these are Extension Attributes that the data is being pulled in from whatever source it came from with those carriage returns.

I believe I used something like the following to remove them in one of my old scripts. I can't seem to find it now, but I know I had to do it at times.

sed -i "" 's/^M//g' <file>

I may have also used tr to remove them, but used as the target character.

tr -d '
' <file>

See if one of those helps maybe?

akamenev47
Contributor II

@mm2270 ,

Thanks for the suggestions. The first one did not work :( For second one I get a typical "usage" response indicating Bash is not understanding something with the tr command.

To elaborate more on what's going on, the indication that there is a space/empty line is that after I paste the data passed to clipboard - the cursor is on the lower line. Also if I pass the data into the file and then open the file - it shows 2 lines (I attached a screenshot). If the data is copy/pasted into Terminal it is ok, but in TextEdit it creates extra line and if it is copy/pasted into the password field when trying to unlock something in System Preferences it is pasted with extra space because of this.

I am basically working on a script which will grab the current LAPS local admin password from JSS over API and then copy/paste it into clipboard.
812a18bb2d1541cf8e34353be677ed81

Ahoy!

akamenev47
Contributor II

It seems like it is not the issue with the extension attribute. If is probably an issue with the pbcopy command as if I simply echo a file, after it is copied into Clipboard using pbcopy - it does the same thing with an extra line. What it is interesting - it works fine if you paste it into Terminal, without an extra "space", yet when pasting it into TextEdit - it creates and extra line and when pasting it into System Preferences User Name or Password - for User Name it adds extra line (see attachment) and for Password it adds an extra space or extra character, which if deleted with backspace - the pasted password works.... weird
44105c3cbc094e858304d65e557ab150

Ahoy!

akamenev47
Contributor II

Nevermind... I started at the wrong "end" I guess. There was no issue with JSS/API at all, but with pbcopy command, which creates an extra line if used by default. In order for it to not do so, you just have to slightly modify the command such as:

echo $variable | tr -d '
' | pbcopy

If anybody is interested, the script grabs data from specific extension attribute on the JSS via API, passes it to the variable and then the variable passes it to Clipboard

#Grab extension attribute from JSS and pass it into variable
currentmachinelapspass=`/usr/bin/curl -k -u $user:$pass "$jssurl/JSSResource/computers/id/$id/subset/extension_attributes" -H "Accept: text/xml" -X GET | xpath '//extension_attribute[id=67]/value/text()' | cut -f2 -d ">" | cut -f1 -d "<"`

#Pass variable to clipboard
echo $currentmachinelapspass | tr -d '
' | pbcopy
Ahoy!