LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-31-2012, 08:44 PM   #16
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Rep: Reputation: Disabled

Quote:
Originally Posted by danielbmartin View Post
Suggestion: take any code you wrote which contains non-trivial awks and write a derivative version in which some of those awks are replaced with functionally equivalent seds. Then, make careful timings to determine which version runs faster. Compare the code to decide which version is more readable. Post your results on this forum.

Daniel B. Martin
Great idea Daniel! I have to finish up fixing some runtine errors on my archbox and then I will give it a go. Thanks
 
Old 08-01-2012, 12:06 AM   #17
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
I would agree on the level that for a one of substitution sed can often offer a more straight forward solution, however, the complexity that awk can go to as a scripting
language is for the most part not repeatable in sed, to wit, anywhere you have complex decision making or variables such as arrays.

That being sed, as always, right tool for the job is the best motto
 
Old 08-01-2012, 12:32 AM   #18
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
I would agree on the level that for a one of substitution sed can often offer a more straight forward solution,
AGREED!

Quote:
however, the complexity that awk can go to as a scripting
language is for the most part not repeatable in sed
I now see that.

I was trying to remove duplicate lines with Sed but it was rediculously complicated! Whereas, I could just use Awk and do:
Code:
awk '!a[$0]++'
I found a solution with Sed:
Code:
sed '$!N; /^\(.*\)\n\1$/!P; D'
Who in there right mind would use the latter? A devout Sed user maybe? A masochist lol

Quote:
anywhere you have complex decision making or variables such as arrays.

That being sed, as always, right tool for the job is the best motto
I am coming to an understanding, as to where I should use Sed and where I should use Awk.

My previous frame of mind was to avoid Sed at all costs but I now see that is not wise!

Thanks to David's elegant solution:
Code:
echo "my documents here that are made (bla).doc" | sed 's/ [^ ]*$//'

Last edited by amboxer21; 08-01-2012 at 12:35 AM.
 
Old 08-01-2012, 10:38 AM   #19
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Yep, as always, try to choose the right tool for the job.

sed is pretty powerful, but in truth it's nowhere near the level of awk. awk is a full scripting language with variables, arrays, functions (built-in and user-defined), a full floating-point math engine, and more. sed just can't compete with all that.

The real difference though is in the underlying design of the work-flow. sed is the "stream editor", a line-oriented tool. It's designed for quickly parsing (and modifying, inserting, or deleting) lines in start-to-end sequence. As long as you stick with that paradigm, it is very fast and efficient. But when it comes to multi-line work, particularly when you have to "look back" to previous lines, it can get tedious to impossible, since it's really designed for moving one-way only.

awk, on the other hand, is field-oriented. It breaks the input text up into "records", then breaks each record up into fields (both user definable). Then you can use the large number of features it offers to manipulate those fields pretty much any way you wish. It does have some quirks that can make it awkward to use in some situations, though. Trying to preserve the same exact layout of input delimiters in the output is one particularly irritating example.


To sum it up, sed is best suited for line-oriented, unidirectional text modification, particularly free-form text, while awk is designed more for use on tables and other regularly-delimited, fixed-format data, and when you need to do more advanced data manipulations (such as arithmetic).


Finally, you might consider learning how to use ed as well. It's the original source of much of sed's syntax, so they are similar in many ways, but as an actual text editor, it can be more flexible when scripting modifications to text files, since it doesn't suffer sed's one-way streaming limitation.

How to use ed:
http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] using awk and comparing a field's value oreka18 Programming 3 05-13-2012 07:04 AM
awk error awk: line 2: missing } near end of file boscop Linux - Networking 2 04-08-2012 10:49 AM
[SOLVED] awk: how to print a field when field position is unknown? elfoozo Programming 12 08-18-2010 03:52 AM
awk printing from Nth field to last field sebelk Programming 2 01-08-2010 09:39 AM
AWK: print field to end, and character count? ridertech Linux - Newbie 1 05-07-2004 05:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration