awk programming

Hi folks,

I would like to know the difference between shell programming and awk programming. Actually i have developed a few applications in both but i could not find a better difference other than the syntax differences.

For example, the awk programming syntax is complicated. It has "{" and "}" usage everywhere in the program.

Can anyone tell me some important differences between them and the uses of awk.

TIA,
Nisha

In shell programming all variables have the datatype string and you do not need to declare them. To assign a value to a variable you write:
varname=value

To get the value back you just put a dollar sign in front of the variable:
#!/bin/sh
# assign a value:
a="hello world"
# now print the content of "a":
echo "A is:"
echo $a

Most of the time awk is used to extract fields from a text line. The default field separator is space. To specify a different one use the option -F.

cat file.txt | awk -F, '{print $1 "," $3 }'

The comma as field separator and print the first and third ($1 $3) columns. If file.txt has lines like:
Bor, 34, India
Miller, 22, USA

then this will produce:
Bor, India
Miller, USA

There is much more you can do with awk but this is a very common use.

Hey thanks ... but can i replace an awk programming with shell programming and vice versa?

The original shell was the bourne sh. It had limited capabilities and was designed to lean very heavily on external programs. It had almost no built-in functions. The authors of awk wrote awk in this environment. And they stated roughly that awk was intended to greatly extend the kinds of shell scripts that can be written.

The first shells were written before awk was written. awk was never intended to be a new shell. awk was intended by its authors to work with shells, not replace them.

Both awk and the shells are now much more powerful. You can write stand-alone awk programs. And you can even write stand-alone sed programs for that matter. But both awk and sed are generally used inside of a shell script.

When I wrote in the the bourne shell, I leaned on awk very heavily. But I always saw awk as a crutch for a weak shell. Now that I have switched to the korn shell, I almost never use awk anymore.

But that's just me. Other folks really like awk and use it extensively. Unix has many tools and there are many ways to do most things. You really need to survey the tools and pick your own favorite.

I will say this though, no one uses awk for a login shell. awk programmers will need to know a shell well enough to at least log in.

:o Think about it...
If you can use sh shell in your own unix system, but you not sure it can work fine in other system...vice versa
You wrote a program by awk, that is.. on both system running well;)