Tag Archives: php

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.