[Wolves] Hello...

Adam Sweet adam at adamsweet.org
Mon Jun 15 09:40:14 UTC 2009


Octavio Augusto Sánchez Velázquez wrote:
> Hello, I'm new to this mailing list, currently I don't live in
> Wolverhampton but I will upon September.

Welcome to the LUG :) Are you a Spanish student coming to Wolverhampton
University? I went there myself and was friends with a lot of Spanish
students, it was maybe 7 or 8 years ago now so they have all long since
left. Where are you from?

> I'm just getting used to the CLI and I think that is better than
> GUI, but is harder to learn. Specially all does languages like sed, and
> awk which I can't understand.

It took me quite a long time to get used to the command line, I started
slowly but picked up speed once I understood a few commands and got the
basics of how things go together using pipes '|' and redirects '<' or '>'.

I'm no master of sed or awk but the simple things aren't too hard and
you can build up from there, they're just difficult to pick when you
don't have a real reason to use them.

For example, use awk to pull a field out of of some output, such as
finding the process IDs from the output of ps, especially useful when
using grep to find processes owned by a particular program or service:

adams at selenium:~$ ps ax | grep apache
 3933 ?        Ss     0:02 /usr/sbin/apache2 -k start
11133 ?        S      0:00 /usr/sbin/apache2 -k start
11135 ?        S      0:00 /usr/sbin/apache2 -k start
11136 ?        S      0:00 /usr/sbin/apache2 -k start
11138 ?        S      0:00 /usr/sbin/apache2 -k start
11139 ?        S      0:00 /usr/sbin/apache2 -k start
25712 pts/0    S+     0:00 grep apache

You can see there is a process running called 'grep apache', which is
the grep finding its own process looking for apache. You can exclude
things from grep with -v so you can stop grep finding itself with 'grep
-v grep':

adams at selenium:~$ ps ax | grep apache | grep -v grep
 3933 ?        Ss     0:02 /usr/sbin/apache2 -k start
11133 ?        S      0:00 /usr/sbin/apache2 -k start
11135 ?        S      0:00 /usr/sbin/apache2 -k start
11136 ?        S      0:00 /usr/sbin/apache2 -k start
11138 ?        S      0:00 /usr/sbin/apache2 -k start
11139 ?        S      0:00 /usr/sbin/apache2 -k start

Now awk out the first field which contains the process IDs:

adams at selenium:~$ ps ax | grep apache | grep -v grep | awk {'print $1'}
3933
11133
11135
11136
11138
11139

Awk uses tabs as field separators, so I'm telling it to pull out the
first one.

Sed means stream editor. You take a stream of data, normally output from
a command or a from file and you edit it on the fly. You can't edit a
file in place with sed as it will still be reading in the file as you're
trying to write it. When editing files you have to write the output to
another file and then move it over the original afterwards.

As a fairly useless example, I'll take the output from the above awk
command and change all the 1s to 2s:

ps ax | grep apache | grep -v grep | awk {'print $1'} | sed 's/1/2/g'
3933
22233
22235
22236
22238
22239

Here I am passing in the output from the previous commands using the
pipe and telling sed to substitute (the s) 1 for 2 globally (the g). You
see how these commands build up? You just have to get data from A to B
to C, modifying it on the way. The difficulty is in knowing the commands
to do what you need in the first place :) Realistically, most normal
people won't ever need sed and awk :)

> Any way, I am willing to read all mail discussion over here so I can
> learn more and get some ideas about everything here.

Welcome aboard. If you're interested and you can make it to some of our
meetings when you arrive, I'm sure one of us can do a beginners guide to
the command line presentation, I've been meaning to do one for years but
most of the people it would have been useful to didn't come to meetings.

Regards,

Adam



More information about the Wolves mailing list