Extract key info from CLI

If you are frustrated waiting for the API then this little snippet may help in a very small way
I am quite certain there will be more elegant ways to bash the required bytes but this seems to work OK. Thanks to @chriso for inspiration

willie@gagarin:~$ safe keys create > keys.txt
willie@gagarin:~$ cat keys.txt
New SafeKey created: "safe://hyryyyyyywnpdjttecrh4mdcekm14e48um98ynzj4amy7fnjewzf5q5mpdh4y"
Key pair generated:
Public Key = a09a34c6286139a58d8852e5a468f35fce015d3ac2c1d28928a5cbb76d6d1f34
Secret Key = 2487e7cf02e98d65ba52fd1e74432cb8523528cffe67b32e2e9ff5635ee5cca4
willie@gagarin:~$ MY_SAFE_KEY_URL=$(cat keys.txt |head -1|cut -b 23-90)
willie@gagarin:~$ echo $MY_SAFE_KEY_URL 
safe://hyryyyyyywnpdjttecrh4mdcekm14e48um98ynzj4amy7fnjewzf5q5mpdh4y
willie@gagarin:~$ MY_SAFE_PUBLIC_KEY=$(cat keys.txt |tail -2|cut -b 14-77|head -1)
willie@gagarin:~$ echo $MY_SAFE_PUBLIC_KEY 
a09a34c6286139a58d8852e5a468f35fce015d3ac2c1d28928a5cbb76d6d1f34
willie@gagarin:~$ MY_SAFE_SECRET_KEY=$(cat keys.txt |tail -2|cut -b 14-77|tail -1)
willie@gagarin:~$ echo $MY_SAFE_SECRET_KEY 
2487e7cf02e98d65ba52fd1e74432cb8523528cffe67b32e2e9ff5635ee5cca4
3 Likes

Just a wee tip: you could parse those quite nicely using awk rather than cut :). Use -F to specify the separator, which would be : then `=’.

4 Likes

I did man awk once and my head still hurts.
@davidpbrown once kindly tried to explain sed and awk to me. Sadly it was a waste of his time.
I must sit down with a fresh brain some time and try again. This present brain recoils in horror and shutsdown when it sees more than 3 backslashes and brackets in the one line

3 Likes

Well, awk is a very powerful little language in its own right, but quite frankly, I don’t understand 99% of it, and I think that’s the case for most people. However, the basic use case is just for parsing out text you need from a particular line. A common example is if there’s output with some columns, and you’re only interested in one of the columns. awk will split by default on whitespace, then make each of the columns available.

In your case, you would do awk -F ':' '{ print $2 }'. You’re telling it to split on the : character. So $1 will be “New SafeKey created” and $2 will be the URL.

Actually, it would include the quotes in the URL string, so you’d need to strip off the quotes with a sed, but that’s easy: sed 's/"//'. The single quotes for the expression there is important. If you used double quotes, you’d have to escape the double quote character. The expression just says “substitute (that’s the s) the double quotes character with nothing”. If you’d wanted to substitute another character or string, you’d put that between the last 2 slashes.

5 Likes

I’m running through Learning Awk Is Essential For Linux Users - YouTube right now

I can already see

willie@gagarin:~$ cat keys.txt | awk '{print $4}'|head -1
"safe://hyryyyyyywnpdjttecrh4mdcekm14e48um98ynzj4amy7fnjewzf5q5mpdh4y"

is indeed a lot cleaner

5 Likes

Ah yeah, it will just be splitting on the whitespace by default.

4 Likes

cut can be useful too, but it just feels :sunglasses: to use awk :joy:.

The guy who invented it was one of the OG Unix gurus, Brian Kernighan, a really awesome guy.

3 Likes

I bow to your uber-geekness :sunglasses:

7 Likes

I believe sed is no good for substituting line breaks, though. I’ve used bits of perl for that.

I think there’s a cli flag to get sed to ignore line breaks so you can substitute them.

I’m a big Python guy. If things are getting too awkward or unwieldy in the shell I drop out to Python.

I tend to go with the rules of thumb in the When to use Shell section of Google’s ‘Shell Style Guide’.

2 Likes

Oh god now you are triggering me…

How can you tell when one bit of perl stops and the other one starts? Its like someone took all the ugly chars on the keyboard and flung them at the screen.

Guy I used to know was always entering “Obfuscated Perl” competitions - which I thought was a tautology as Perl by definition is obfuscated anyway. One winner wrote a program to calculateprimes up to some huge number and won because when printed out, the listing was in the shape of a camel.

1 Like

Did someone mention swine?

2 Likes

Oink oink!!!

Perl is for those more concerned about their self-assumed geek credentials than actual utility.

I’ve never seen a python swallow a camel but I bet it could.

2 Likes

I can’t tell diddly. I just used to copy stuff and pipe it together on the command line. I really suck at any kind of programming. I think I should learn some Python. Do you know of any good guides for complete n00bs, which is what I really am?

1 Like

There are many Python from scratch tutorials. The best I found was a paid course fro CodeWithMosh which I got as part of his All-Access subscription.

But there are zillions of free ones

1 Like

You may want to actually try learning Typescript rather than Python. It looks like this may possibly surpass Python in the long term. I’m definitely going to learn it at some point. I still love Python though.

2 Likes

Thanks! I can see it was developed by Microsoft, though, and I don’t touch Windows. Is Typescript equally good for Linux? No weird patents or other restrictions? I would want to just go apt-get install and have it working.

1 Like

I’ve actually never written a line of code in it myself yet, I’ve just heard so much about how loved it is. A lot of people see it as Python or JS but with static/strong typing, and love it for that reason. I personally just don’t get too caught up on the typing aspect and just see the static/dynamic typing thing as different approaches with benefits and drawbacks.

I wouldn’t worry too much about the Microsoft aspect of it. Over the last 5 years or so Microsoft have actually been very good when it comes to cross platform support. I can see from just a brief Google that it’s on all platforms, as I would expect.

2 Likes

OK its official awk wins for convenience and simplicity.
thanks @chriso


willie@gagarin:~$ safe keys create > keys.txt
willie@gagarin:~$ MY_SAFE_KEY_URL=$(awk '{if(NR==1) print $4}' keys.txt|sed 's/"//')
willie@gagarin:~$ MY_SAFE_PUBLIC_KEY=$(awk '{if(NR==3) print $4}' keys.txt)
willie@gagarin:~$ MY_SAFE_SECRET_KEY=$(awk '{if(NR==4) print $4}' keys.txt)
willie@gagarin:~$ cat keys.txt
New SafeKey created: "safe://hyryyyyyyk7xrd6yqzcpd8a69g1zxp85pdfzimiudfnkxyigcmi7hsa38seuy"
Key pair generated:
Public Key = 575e41f80ebb1a33e3df34aef69f6d196f55d6632894f054cc5d7bcb6327b226
Secret Key = 5a1039034c4b71358543965a08eaed42ac554ab15dfefb7e7d81d13a8a631b60
willie@gagarin:~$ echo $MY_SAFE_KEY_URL 
safe://hyryyyyyyk7xrd6yqzcpd8a69g1zxp85pdfzimiudfnkxyigcmi7hsa38seuy"
willie@gagarin:~$ echo $MY_SAFE_PUBLIC_KEY 
575e41f80ebb1a33e3df34aef69f6d196f55d6632894f054cc5d7bcb6327b226
willie@gagarin:~$ echo $MY_SAFE_SECRET_KEY 
5a1039034c4b71358543965a08eaed42ac554ab15dfefb7e7d81d13a8a631b60
3 Likes