Posts Tagged awk

Using finger with awk, finding out lab account info of your teammates in case you forgot

On one evening during my third year at U of T, I was working on an CSC343(intro to databases) assignment with my partner. When the assignment was finally completed I was overjoyed, with the assignment due in less than an hour, my partner put me in charge of submitting the assignment as he had to leave immediately and run some errands. When I was ready to submit the assignment electronically on a online app, the app asked me for my partner’s lab account name. I was in total panick, because I forgot his account name! I took a DEEP breath and tried to call my partner on his cell phone, but he didn’t pick up. “FML, my partner’s gonna kille me” I thought . I then messaged a friend and asked for consultation and possibly prayers. Instead of prayers however, she gave me a bash shell script that uses for loops, finger, /etc/pwd and grep. The script was magix, because it located my partner’s account info, and to this day, I still think she is l33t.

Now a year later, I wondered if there were a simpler way to find out the account information of a classmate by name (or part of his/her name) without using shell scripts. And here is what I came up with

export $REGEX=<name>
cat /etc/passwd | awk -F : '{print $1}' |xargs finger -l|awk '/Name: [a-zA-Z]*'$REGEX'[a-zA-Z]*/{print $0;getline;print}'

The command might look daunting at first, but if you break it down, it is really simple.
cat /etc/passwd displays various information of all the user accounts on system, it usually looks like this

jack:x:1000:1000:jack,,,:/home/jack:/bin/bash
riak:x:114:123:Riak Data Store,,,:/var/lib/riak:/bin/bash
mysql:x:115:124:MySQL Server,,,:/nonexistent:/bin/false

for more information on /etc/passwod visit here

awk -F : {print $1}

The above command basically says that for each line, print the first ($1) element delimited by the symbol “:”. In our case it will display all of the user account name.

jack
riak
mysql

The above would be an example of user accounts.

xargs finger -l

finger allows us to find more information on a given account name, including the user’s name. This is pretty essential.

awk '/Name: [a-zA-Z]*'$REGEX'[a-zA-Z]*/{print $0;getline;print}'

prints the line that contain the values that are defined by $REGEX.

So this little trick command allows the user to find account information based on the user’s registered user name, I hope this is useful to you, and I hope my WTF moment doesn’t happen to you.

Advertisements

,

Leave a comment

Using awk to remove python2.7

Today I wanted to remove python 2.7, and I didn’t really want to do it manually by doing sudo rm on individual python2.7 file/folder. So instead I used whereis, xargs, and grep to help me. I used the following command 

whereis python | awk 'BEGIN{FS=" "}{for(i=1;i<NF;i++) print $i}' | grep python2.7 |xargs sudo rm -fr

Lets walk this through step by step.

>whereis python

Finds all python files that are in the standard directory, for example in my machine it produced
python2: /usr/bin/python2.7 /usr/bin/python2.6-config /usr/bin/python2.6 /etc/python2.7 /etc/python2.6 /usr/lib/python2.7 /usr/lib/python2.6 /usr/local/bin/python2.7 /usr/local/bin/python2.7-config /usr/local/lib/python2.7

We want to split this line by white spaces; this is where the awk expression comes along.
awk 'BEGIN{FS=" "}{for(i=1;i<NF;i++) print $i}

The above uses awk to split white space delimited strings into seperate lines, the FS is a special awk’s built-in variable that determines the delimited string, in our case it is a space. The NF stands for the total number of elements.
I got the following on my machine.

/usr/bin/python2.7
/usr/bin/python2.6-config
/usr/bin/python2.6
/etc/python2.7
/etc/python2.6
/usr/lib/python2.7
/usr/lib/python2.6
/usr/local/bin/python2.7
/usr/local/bin/python2.7-config
/usr/local/lib/python2.7
/usr/local/lib/python2.6

then with some help of grep and xargs I was able to successfully remove python2.7 from my system with precision.

grep python2.7 |xargs sudo rm -fr

This is only a little taste of awk’s power, it fully supports regular expression and makes command line scripting a breeze.
For example if you wanted to kill all processes that contains the word java, then you can just do

ps ax | grep java| awk '/java/{print $2;}' |xargs kill -9

especially useful for killing multiple java processes that hangs.
I for one have just begin to use awk, and I see it as a essential tool for all scripters in linux environment.

2 Comments