home.

tagged: unix

Upload to an image to imgur from the command line with curl

First, go to https://api.imgur.com/oauth2/addclient and register as a client. Create a new client and you'll eventually get a Client ID and a Client secret.
 
Now take the client ID, and use this curl command:

curl --request POST --url https://api.imgur.com/3/image --header 'authorization: Client-ID YOUR_CLIENTID_HERE'   
--header 'content-type: multipart/form-data;' -F "image=@/LOCATION/OF/THE/IMAGE.png"

You should get some JSON back like this:

{"data":{"id":"SOME_ID","title":null,"description":null,"datetime":1511088894,"type":"image\/png",
"width":256,"height":256,"size":7541,...,"deletehash":"SOME_DELETE_HASH","name":"",
"link":"https:\/\/i.imgur.com\/THE_URL.png"},"success":true,"status":200}

unix imgur curl


Parsing XML from the command line

First apt-get install xmlstarlet. And let's say we have this XML file:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <name>Yes</name>
  </Document>
  <Document>
    <name>No</name>
  </Document>
  <Document>
    <name>Maybe</name>
  </Document>  
</kml>

We can use xmlstarlet sel to select elements. But first we need to tell it about the name space, so xmlstarlet sel -N x="http://www.opengis.net/kml/2.2".
 
Now let's use -t -v to start using some XPath. In our case /x:kml/x:Document/x:name. So in full xmlstarlet sel -N x="http://www.opengis.net/kml/2.2" -t -v "/x:kml/x:Document/x:name" the_xml_file.xml. This will print:

Yes
No
Maybe

xml unix


Unix: Replace newlines with sed

Imagine we have this file

Line one


Line two

And we want to remove the double new line.
 
This ugly looking command will replace all the newlines with nothing.

sed ':a;N;$!ba;s/\n\n//' YOURFILE

The :a says create a label - we'll need this in a moment.
 
The N says append the next line onto the current pattern - we need this since we're matching two lines. So say we're trying to match the double newline this will give us '\n\n' in our pattern space.
 
The $ matches the last line, i.e. in '\n\n' we'll be right at the end. And the ! inverts that. So here's we're matching the first '\n' since this is not the last line.
 
Then the ba means go back to our label that we just created. So if we're not on the last line, go back and do the match on the next part. This seems to be so the newline doesn't halt the match.

unix sed


Using sed to alter the first and last line of a file

Sed normally alters every line of a file. You can change this by either prepending the expression with a number or a dollar sign for the last line.

cat some_file | sed '1 s/^\(.\)/START OF FILE\1/' | sed '$ s/\(.\)$/\1END OF FILE/'

This takes the first character on the first line, and adds 'START OF FILE' before it.
 
The second command takes the last character on the last line, and adds 'END OF FILE' after it.

sed unix


Unix: Basics of Gawk

The basic syntax of this command is usually:
 
gawk '<gawk command>' filename

Within the gawk command, the format is:
 
BEGIN { <operations> } /<pattern match>/ { <operations> } /<another pattern>/ { <another op> } END { <operations> }

The BEGIN and END parts are optional. As is the pattern match.
 
Within the main operations block, you can use the NF variable to find many fields are on this line.
 
This gawk program will only print non-blank lines, since blank lines have a NF of 0:
 
gawk '{ if(NF!=0) print $0 }' filename

The 'print $0' will print the whole line. The $1 will print the first field and so on. Fields are separated by spaces and/or tabs.
 
Pattern matching will allow you to only print lines with, in this case, the word 'hello' in them:
 
gawk '/hello/ { print $0 }' filename

If you place a '~' before the pattern match, it will be inverted.
 
There are many other things gawk can do, including variables, addition of fields and many more.

unix awk

Page 2 of 14
prev next