1753751 Members
5574 Online
108799 Solutions
New Discussion юеВ

sed or awk examples

 
SOLVED
Go to solution
Victor_5
Trusted Contributor

sed or awk examples

I am looking for some resources about sed or awk usage, I prefer real example, so that I can get some ideas when I script something.

Web site, white paper, manual, or real case from yourself are all welcome. Thank you so much in advance.

Shawn


9 REPLIES 9
harry d brown jr
Honored Contributor
Solution

Re: sed or awk examples

Download some from here:

http://examples.oreilly.com/sed2/
Live Free or Die
harry d brown jr
Honored Contributor

Re: sed or awk examples

and awk from here

http://examples.oreilly.com/awkprog3/

Live Free or Die
Victor_5
Trusted Contributor

Re: sed or awk examples

Thank you so much Harry, I know you just got your crown, congras again!

10 points is ready for everybody.

Thanks.
Patrick Wallek
Honored Contributor

Re: sed or awk examples

Also check out the book 'sed & awk' 2nd Edition by Dale Dougherty & Arnold Robbins published by O'Reilly. - ISBN 1-56592-225-5

There is also a 'sed & awk' Pocket Reference by Arnold Robbins publiched by O'Reilly. ISBN - 1-56592-729-X
A. Clay Stephenson
Acclaimed Contributor

Re: sed or awk examples

Hi Shawn:

I too suggest that you get the O'reilly book 'Sed and Awk'. I think I'll just attach one of my scripts that does 2 things - along with a few comments.

This script is actually a shell script which builds awk scripts 'on the fly' and then in turn invokes the scripts. It also has traps to remove the temporary scripts upon program exit.
The math in this awk is fairly intense but what it does is to convert a calendar date into a 'Julian Day' - essentially the number of days since 4000 years ago. It is used by astronomers to make calculations easy. Real Julian Days are actually floating point values and the days begin at noon UTC so that midnite is really Julian Day + 0.5. This version, which is much more useful in the computer world uses integer arithmetic and start at midnite. It can also do the reverse calculation, so if you want to know what the date will be exactly 167 days from now, it's very easy.

I'm just including this as an example of a fairly complex awk program being invoked by a shell script. One thing that is not often mentioned in awk documentation is how to declare local variables in a function. To a programming purist, awk's method is very hokey but it does work.

function silly(s1,s2, i,j,s_out)
{
i = 10
j = 2 + i
s_out = sprintf("%s%s%d",s1,s2,j)
printf("%s\n",s_out)
return(j)
}

The main awk program which calls the function might do this:

cc = silly("String1",String2")

Note that the function has 5 formal arguments but only 2 actual arguments. The remaining 3 args become local variables to the function and thus that instance of i,j, and s_out will not conflict with the use of other variables with the same name; either in other functions or within the main awk body.

Another point I would make is to first develop complicated scripts as a file and use the awk -f myfile.awk method to run awk. The awk '{print $1}' method is fine for one-liners but gets very tricky when the script becomes a bit more complex.

Another fairly useful trick is to use more then one -f argument. You can supply multiple -f arguments and that becomes a method for including commonly used functions which are then combined to produce a complex program. If you have several complex awk functions, they can then be centrally located and changed in one place rather than having to hunt them in a ton of awk scripts.

One other point that I would make: I've been doing awk/sed for quite a few years but now I find myself writing more and more perl. If I were going to learn from scratch now, I would skip awk and sed and just use perl. I'm not putting awk and sed down, it's just that perl can do anything those guys can do and do it in a cross-platform environment. For example, it's very easy to set up bidrectional sockets between a UNIX box and an NT server using perl.

Food for thought, Clay
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: sed or awk examples

Hi again, after all that - I forgot to attach the example script so forget all that I seem to know about that programming stuff.

Clay
If it ain't broke, I can fix that.
Victor_5
Trusted Contributor

Re: sed or awk examples

Hi Harry:

Sorry to trouble you.

For progs.tar.gz, I donot know how to uncompress it, tar -xvf don't work.

For eap3.tar, I don't know how to read it on Unix, they are .xml.

Or, even you have better way so that I can read all of those files on Win NT?

Your reply is really appreciate.
A. Clay Stephenson
Acclaimed Contributor

Re: sed or awk examples

Hi Shawn:

Just use gunzip which is usually located in /usr/contrib/bin

gunzip /tmp/myfile.tar.gz

which will unzip to /tmp/myfile.tar

you then tar xvf /tmp/myfile.tar
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: sed or awk examples

Hi Shawn:

Here's one for 'sed' that I like (and has been around in a few places):

http://www.student.northpark.edu/pemente/sed/sed1line.txt

Regards!

...JRF...