Computer Hope

Software => BSD, Linux, and Unix => Topic started by: surekha on November 02, 2008, 12:20:29 AM

Title: difference between awk and sed scripting
Post by: surekha on November 02, 2008, 12:20:29 AM
the main difference is that in sed script he script is made to excute once,
 but not in awk
but i am  not clear with it .

can any one help me out ...........
Title: Re: difference between awk and sed scripting
Post by: ghostdog74 on November 02, 2008, 01:19:47 AM
the main difference is that the script is made to excute once in sed but not in awk
but i am clear with it .

can any one help me out ...........


you have to make your question clear. Both are tools for processing text. awk is a programming language whose main purpose is processing text BUT it can be used as a general purpose programming language as well.
Title: Re: difference between awk and sed scripting
Post by: surekha on November 02, 2008, 07:59:04 PM
yes both can be used  as programming language
but i dont know whats the difference ,since both are used to search pattern
Title: Re: difference between awk and sed scripting
Post by: Caleneledh on March 11, 2010, 02:03:21 PM
Although sed and awk are similar they do have different purposes. 

Sed is a stream editor mainly used to change change a field.  For example:

sed -e 's/oldstuff/newstuff/g' inputFileName > outputFileName

would change all occurences of oldstuff to newstuff in inputFileName, and redirect the output to outputFileName.

Awk is more usualy used to format fields.  For example:

awk '/regex/ { print x }; { x=$0 }'

This would search for occurences of /regex/ and print the line FOLLOWING it.


This is not hard and fast though as there is some overlap between the two.
Title: Re: difference between awk and sed scripting
Post by: Geek-9pm on March 11, 2010, 02:18:29 PM
Helo, Caleneledh and wel come to the CH forum.

This may be AWK ward, but no a criticisms of what yu just  SED.

Most likely ghostdog74  will not respond to your answer. Its been a year from the time he made the post. But maybe he will drop it and see it.

But anyway, I enjoyed your answer. Now  I know the difference.

Here is a reference:
http://www.faqs.org/docs/abs/HTML/sedawk.html
I put that here so the search engine will have some information for the keywords AWK and SED and what is the difference between AWK and SED.
Title: Re: difference between awk and sed scripting
Post by: ghostdog74 on March 24, 2010, 11:40:46 PM
Although sed and awk are similar they do have different purposes. 

Sed is a stream editor mainly used to change change a field.  For example:

sed -e 's/oldstuff/newstuff/g' inputFileName > outputFileName

would change all occurences of oldstuff to newstuff in inputFileName, and redirect the output to outputFileName.

Awk is more usualy used to format fields.  For example:

awk '/regex/ { print x }; { x=$0 }'

This would search for occurences of /regex/ and print the line FOLLOWING it.


This is not hard and fast though as there is some overlap between the two.

sadly, not true. sed is a stream editor to manipulate and change text. All it ever does is using regular expression. Plus some buffers to hold data. that's about it. It lacks abilities to count (at least easily), or lacks arrays, and other programming constructs. While, awk on the other hand, does the job of sed more easily and more intuitively. it can also replace text as easily as using sed. In the end, they actually do not have different purposes in terms of text/file processing.
Code: [Select]
sed 's/old/new' file

same as
Code: [Select]
awk '{sub("old","new")}1' file

Now, take a csv file that is comma separated and say i want to find the value of the 3rd field. Its more easily done with awk than with sed. eg

awk
Code: [Select]
awk -F"," '{print $3}' csvfile
sed
Code: [Select]
sed 's/\(.[^,]*\),\(.[^,]*\),\(.[^,]*\)\(.*\)/\3/' csvfile

You can learn both sed and awk, but frankly, for all your text/file processing + programming needs, just learn awk will do.
Title: Re: difference between awk and sed scripting
Post by: ghostdog74 on March 24, 2010, 11:42:18 PM
Most likely ghostdog74  will not respond to your answer. Its been a year from the time he made the post. But maybe he will drop it and see it.
well, i seldom click "New replies to your posts", but if i do, i would reply as necessary :)