SED vs. AWK showdown
1 Attachment(s)
Every so often, there's enthusiastic debate** about which is better: SED or AWK. I'd like to see this be a fun thing---maybe even a learning thing.
Here's a problem: Given a file with a word list, rearrange into sentences which all begin with a keyword. Remove any leading spaces, and extra spaces between words. Here is a SED solution (keyword = "the"): Code:
sed -n '${H;x;s/\n/ /g;s/^ *//;s/ \+/ /g;s/ the/\nthe/g;p};H' words.txt The logic used: while not at the end of file, append each line to the hold register. when EOF is reached, also append that last line, then bring the hold register into the working register. Now we have the whole file in the register. Then: replace all linefeeds with spaces replace all spaces at the beginning replace all multiple spaces with just one insert line breaks before all instances of the keyword (the), except at the beginning, where there is no space. print the result So: Is there an AWK solution which is: faster? less code? **many of which I lost......;) |
less code doesn't mean its always legible or understandable.
Code:
awk 'NR>1&&$1=="the"{print ""}{ printf "%s ",$0}' words.txt |
Neither sed or awk, but..
Code:
#!/bin/bash |
its a small issue. you forget the file name
|
It's stdin, of course! Like any good "filter" type script ought to behave.
|
Quote:
regarding your script, if you are going to buffer every line of the file before processing, its going to be very slow when the file sizes are huge. |
Quote:
I'll be happy to do it, but later. Now I must watch "V".... |
Quote:
|
Code:
echo `cat words.txt` | sed 's/ the/\nthe/g' |
Quote:
NB: please change your sed to take care of things like Code:
the |
Quote:
|
Quote:
Kevin Barry |
The bigger problem is if words.txt ends with ";rm -rf ~"
|
Quote:
Kevin Barry |
Quote:
|
All times are GMT -5. The time now is 06:20 AM. |