Tag Archives: cli

newbie picking away at awk

I’ve been looking into awk off and on.
This is kind of a weird thing to do, coming from perl.
I’m not a perl genius, but, I’m intermediate. Which is saying a lot.
Perl is a beast. It’s a madman in the forest tripping on lsd- who commands power over small countries and speaks to aliens. If you can talk to perl, it can do everything from giving your wife an orgasm to putting the baby to sleep.

So, awk- it would seem silly to be interested in it. Since perl could do all of this already. Why learn it?

Well, I’ve been learning it. Because if there’s anything cooler than perl, it’s unix.
And awk is .. well.. unixy.

So.. awk. I’m a total awk noob, please.. keep in mind.

Awk seems to be cool for parsing line output, for one.
I often do ls -lha for listing sizes of things. And I may not ne interested in, you know.. permissions. Because maybe I just want to know the size of things..


Playing with ls output

Regular ls

$ ls -lh
total 76K
-rw-rw-r-- 1 leo leo 3.9K 2010-07-13 08:17 build-meta-refresh-substitutes.pl
-rw-rw-r-- 1 leo leo 138 2010-07-02 03:27 convert-old-new.urls-to-htaccess-entries.sh
-rw-r--r-- 1 leo leo 4.1K 2010-07-08 12:31 htaccess
-rw-r--r-- 1 leo leo 3.7K 2010-07-08 12:31 htaccess2
-rw-r--r-- 1 leo leo 3.5K 2010-07-08 12:30 htaccess3
-rw-r--r-- 1 leo leo 4.0K 2010-07-08 12:35 htaccess4
drwxrwxr-x 2 leo leo 4.0K 2010-07-13 08:18 meta-refresh-versions
-rw-rw-r-- 1 leo leo 5.0K 2010-07-02 02:44 old-new.urls
-rw-r--r-- 1 leo leo 20K 2010-07-13 08:18 refreshpages.zip
-rw-rw-r-- 1 leo leo 6.0K 2010-07-02 02:27 sitemap.new
-rw-rw-r-- 1 leo leo 5.0K 2010-07-02 02:43 sitemap.old

Yes, but what would jesus awk say?

$ ls -lh | awk '{ printf "s %s\n", $5, $8 }'

 3.9K build-meta-refresh-substitutes.pl
 138 convert-old-new.urls-to-htaccess-entries.sh
 4.1K htaccess
 3.7K htaccess2
 3.5K htaccess3
 4.0K htaccess4
 4.0K meta-refresh-versions
 5.0K old-new.urls
 20K refreshpages.zip
 6.0K sitemap.new
 5.0K sitemap.old

Get it? No?
The lines are treated one by one. Each argument is $1, $2, $3, etc.
The delimiter is by default, the shell delimiter. That’s capricorn weekly horoscope sign should climb upwards because financial security is necessary for this sign. whitespace (tab space).


looking for text in files and editing in vim

I often need to find something in code or text. Maybe I’m messing with wordpress stuff, and need to find a php function.

For example, finding a php function..

I want to look for a function called get_author* in the php files around here..

html $ find ~/public_html/ -name "*php" | xargs grep 'function get_author'
/home/leocharre/public_html/wp-includes/link-template.php:function get_author_feed_link( $author_id, $feed = '' ) {
/home/leocharre/public_html/wp-includes/rewrite.php: function get_author_permastruct() {
/home/leocharre/public_html/wp-includes/author-template.php:function get_author_posts_url($author_id, $author_nicename = '') {
/home/leocharre/public_html/wp-includes/theme.php:function get_author_template() {

Yes, but what would awk say?

Automating this somewhat..
The cool thing would be to automatically go there, or at least prit the commands so I can call up vim by cut and paste.

Ok.. not the easiest thing as it turns out… making use of this..

html $ find ~/public_html/ -name "*php" | xargs grep -s 'function get_author' | sed 's/:\s\ /:/' | sed "s/'.\ //" | grep2vim
vim '/home/leocharre/public_html/wp-includes/link-template.php' /'function get_author_feed_link( $author_id, $feed = '
vim '/home/leocharre/public_html/wp-includes/rewrite.php' /'function get_author_permastruct() {'
vim '/home/leocharre/public_html/wp-includes/author-template.php' /'function get_author_posts_url($author_id, $author_nicename = '
vim '/home/leocharre/public_html/wp-includes/theme.php' /'function get_author_template() {'

Where grep2vim is an awk script inside my bin dir..

html $ cat ~/bin/grep2vim
#!/bin/awk -f
BEGIN { FS=":" }
{ printf "vim '%s' /'%s'\n", $1, $2 }

Oy.

The output is pretty cool, it’s cut and paste, for example.. and then vim gets the commnand to search for that string, that’s what the / fuss is all about.

Okkkaaaaay…. Putting it all together..

html $ cat ~/bin/findphpfunction2vim
#!/bin/sh

BASEDIR=$1
if [ -z "$BASEDIR" ]; then
 echo "$0 missing DIR path"
 exit 1
fi

FUNCTIONNAME=$2
if [ -z "$FUNCTIONNAME" ]; then
 echo "$0 missing function name"
 exit 1
fi

find $BASEDIR -name "*.php" | xargs grep -s "function $FUNCTIONNAME" | sed 's/:\s\ /:/' | sed "s/'.\ //" | grep2vim

Example usage:

html $ findphpfunction2vim ./ is_user
vim './wp-includes/ms-functions.php' /'function is_user_member_of_blog( $user_id, $blog_id = 0 ) {'
vim './wp-includes/ms-functions.php' /'function is_user_spammy( $username = 0 ) {'
vim './wp-includes/ms-functions.php' /'function is_user_option_local( $key, $user_id = 0, $blog_id = 0 ) {'
vim './wp-includes/pluggable.php' /'function is_user_logged_in() {'
vim './wp-admin/includes/class-wp-importer.php' /'function is_user_over_quota() {'

Great, using my terminal emulator, I can just double click and middle click to cut and paste, automatically executed since select works including the carriage return.

command line wordpress via WordPress::CLI

Ok- Just released a beta version of WordPress::CLI, this is a command line set of tools to interact with wordpress.
This is pretty awesome and useful. It really speeds up posting tons of stuff to wordpress instead of using the icky gui.

  • upload a post to wordpress to one or more categories
  • set a date for a post, make it appear 5 years back
  • upload images to your wordpress blog via the command line
  • experimental (wors for me, see caveats in docs): set titles, set description, set date- for media files you upload

I thought thigs may be of interest to other people- since I’ve put out a little stuff on this before- and I get asked about this a lot.
This distro has fewer dependencies. It’s very simple. It’s mainly an interface to WordPress::XMLRPC, with some added hacks.
It does some cool useful things, like upload a text file content as post, lets you set date- you can upload media- etc etc.
All via the command line, remotely.
It’s extremely useful to me when I have to set up a site for a client, for example.

Ever wanted to say something like ..

   wordpress-upload-post ./content

Now you can. Yes.
Wait wait.. .. what aobut..

   wordpress-upload-media ./files*jpg

Yes.

It gets better too.
You can set category, date..
You can browse latest posts with

   wordpress-info

Seriously- check this distro out, please send me feedback via rt.cpan.org !!

I had a previous wordpress cli set of tools that did some awesome things, as mentioned- but these were too complicated. This in turn is easy- these commands just interact with WordPress::XMLRPC.

Please try this out, and enjoy- and feedback, please tell me what’s wrong- what
to improve. All of this is under cvs, so feel free to submit diff patchest etc.
Everyone gets credit for any contributions.
This is gpl software.

And yes, this post was uploaded with wordpress-upload-post !

textrender – render text via the command line

p>

textrender – Turn stdin into rendered text graphics.

DESCRIPTION
This allows you to make very nice renders of text for web reasons,
without using photoshop/firewors, etc. The advantage is that since it's
via the cli, you can save text and parameters to a file and use that as
configuration!

See textrender, the parent package.

USAGE
textrender [OPTION]..

-p number       pointsize, default 11
-f hex          color, such as ddffee
-b hex          background color, such as ddffee
-n string       font name or path
-o path         out filename
-g string       gravity, default is west
-d              debug, show the commands
-s              drop shadow, off by default
-S [1-100]      shadow opacity, default is 80 (sets -s to on)

If no out filename path is provided, a temporary file is chosen- such as
/tmp/textrender.2323525252.png and this path is printed to stdout.
If you provide a path argument, this is not printed to stdout.

USAGE EXAMPLES
     echo 'Leo Charre' | textrender -o /tmp/leocharre.png
textrender ./file-with.text

CAVEATS
In development, contact AUTHOR with any suggestions, etc. I strive to
make software on par with unix philosophy, if I have erred, please let
me know.

AUTHOR
Leo Charre leocharre at cpan dot org

LICENSE
This package is free software; you can redistribute it and/or modify it
under the same terms as Perl itself, i.e., under the terms of the
"Artistic License" or the "GNU General Public License".

DISCLAIMER
This package is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the "GNU General Public License" for more details.

——————————————————————————–

REQUIRES

Cache::File: 2.04
Carp: 1
Cwd: 3
Exporter: 5
Getopt::Std::Strict: 1.01
Smart::Comments: 1
String::ShellQuote: 1.03
Test::Simple: 0

INSTALL

perl Makefile.PL
make install

[download]

text to html

I’ve been trying out text2html. It’s pretty cool.
You point it to a text file (or stdin), and it spits out html.
There are some fucked up things about it though..

For one, by default, it does nothing.
Nah, I’m not kidding. It does nothing- spits out same shit that came in. Can you imagine if you ate an apple and shat and apple out, unchanged? That would not be cool. Ok ok.. so it does html entities.. fuck me..
Second- there’s no help option.
Every unix command must have a -h or –help option. Because it’s expected. The empirical *I* fucking expect it.
Instead you have to use $ man ‘text2html’. Oh- but.. wait.. what’s this? Not the complete manual? You have to read $ man HTML::FromText for the full options.
Shit.
This is enough to piss me the fuck off.

It does cool shit, but you have to wine and dine it before it’ll suck your dick. It won’t just take your fifty bucks to do it. And you know the unix way.. By default, this program should suck your dick, no surprises, and shut up.
It should eat my apple and shit out a shit.

Workaround..

Create an alias in your ~/.bashrc file. Add this line:

alias text2html='text2html --blockcode --bold --bullets --email --numbers --paras --tables --underline --urls'

Note that it won’t work until you start another shell session.

Now all options are on, and the thing behaves closer to what is expected.

To install this fine mess of a program… Well.. the application is awesome- the api is a fucking cracked out microsoft whore.
As root..

# cpan HTML::FromText
# man html2text

Here’s some example input and output…

Original Text:

I AM TEXT THAT WILL BE TRANSFORMED TO HTML

Let's see and try what happens here.. shall we.

And hopefully what we expect to happen will. And hopefully what we expect to happen will. And hopefully what we expect to happen will. And hopefully what we expect to happen will. And hopefully what we expect to happen will. And hopefully what we expect to happen will.

And hopefully what we expect to happen will. And hopefully what we expect to happen will. And hopefully what we expect to happen will. And hopefully what we expect to happen will. And hopefully what we expect to happen will. And hopefully what we expect to happen will. This is to test that the next word
will be wrapped up, and there won't be a break between the 'word' and 'will'.

   - thank you
   - i am also thanked of course
   - you think?

ALSO WHAT ABOUT

   A definition.
      I am goint to think so very much

   And what about this on?
      I will also think that. Thank you.

Great. What about a link? http://leocharre.com

Done.

Html output..

I AM TEXT THAT WILL BE TRANSFORMED TO HTML

Let's see and try what happens here.. shall we.

And hopefully what we expect to happen will. And hopefully what we expect to happen will. And hopefully what we expect to happen will. And hopefully what we expect to happen will. And hopefully what we expect to happen will. And hopefully what we expect to happen will.

And hopefully what we expect to happen will. And hopefully what we expect to happen will. And hopefully what we expect to happen will. And hopefully what we expect to happen will. And hopefully what we expect to happen will. And hopefully what we expect to happen will. This is to test that the next word
will be wrapped up, and there won't be a break between the 'word' and 'will'.

  • thank you
  • i am also thanked of course
  • you think?

ALSO WHAT ABOUT

A definition.
   I am goint to think so very much

And what about this on?
   I will also think that. Thank you.

Great. What about a link? http://leocharre.com

Done.


You may want to look at the source of this page for the little details. It’s pretty clean, very nice job. (No, it’s not being trasformed by freaking wordpress.. See wordpress raw html plugin )

browsing your partition with tree via the command line

These tools are useful for development.

Most of the time you work on the terminal, and to find your way around a project you use things like find, ls, and tab completion.
If you need more of a bird’s eye view, you may fire up a gui browser like konqueror. But that’s a gui, and guis are for users.

Another option is tree. Here is example output of tree:

[leo@localhost devel]$ tree
.
`-- WordPress
    |-- bin
    |   `-- wppost
    |-- lib
    |   `-- WordPress
    |       |-- Base.pm
    |       `-- Post.pm
    |-- t
    |-- wp-content
    |   `-- plugins
    |       |-- akismet
    |       |   |-- akismet.gif
    |       |   `-- akismet.php
    |       |-- hello.php
    |       |-- pictpress.php
    |       |-- pm_admin_menu.php
    |       |-- postmaster
    |       |   `-- readme.txt
    |       |-- postmaster.php
    |       `-- wp-db-backup.php
    |-- wp-mail.php
    `-- xmlrpc.php

9 directories, 13 files

Continue reading

editing images in the command line with convert and mogrify

One of the dumbest things I used to do in making web pages was to resize images and make thumbnails in ‘photoshop’.

The next less dumb thing I did was to script thumbnailing. To allow a server to make the thumbnails instantly.
Then I got comfortable with things like convert and mogrify.

Both of these are interfaces to image magick..
Continue reading

posting to wordpress via the command line

DEPRECATED

See command line wordpres via cli

I tackled this problem via XMLRPC and perl.
I ended up packaging this under WordPress::Post on cpan.

Usage examples:

wppost -t 'title of the post' -c stuff,fruit,cake -i 'This is what I think of bla.'

You can also write a text file, the name of the text file is the title.

wppost -i ./path/to/title_of_the_post.txt

This program is in its infancy. But dammit, it works. It’s how I posted this content via the command like. Also, you can include images in your posts. Multiple images. Via the command line.
Anything not in the argument is considered posting content..
So what if your post if about a party you went to, and you have 5 pictures..

First write your halloween_party.txt file, then have your photos lined up somewhere.. then..

wppost -i halloween_party.txt ./images/halloween_photos_post/*jpg

Done. Linux rocks.