ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
And the file I used is attached.
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?
because printf doesn't insert a newline unless you tell it to, the output you see will be lines concat together, until the key word "the" is found, then print a newline. this is much more simpler to understand than the bunch of sed secret code
Last edited by ghostdog74; 11-10-2009 at 10:10 PM.
#!/bin/bash
while read -r LINE ; do
BUFFER="$BUFFER $LINE"
done
BUFFER="${BUFFER// / }"
BUFFER="${BUFFER:1}"
if [[ "${BUFFER:$((${#BUFFER}-1))}" == " " ]] ; then
BUFFER="${BUFFER:0:$((${#BUFFER}-1))}"
fi
BUFFER="${BUFFER// the/$'\n'the}"
echo "$BUFFER"
because printf doesn't insert a newline unless you tell it to, the output you see will be lines concat together, until the key word "the" is found, then print a newline. this is much more simpler to understand than the bunch of sed secret code
Did you run a speed test?
I'll be happy to do it, but later. Now I must watch "V"....
#!/bin/bash
while read -r LINE ; do
BUFFER="$BUFFER $LINE"
done
BUFFER="${BUFFER// / }"
BUFFER="${BUFFER:1}"
if [[ "${BUFFER:$((${#BUFFER}-1))}" == " " ]] ; then
BUFFER="${BUFFER:0:$((${#BUFFER}-1))}"
fi
BUFFER="${BUFFER// the/$'\n'the}"
echo "$BUFFER"
Isn't parameter expansion fun?
That just might be more obfuscated than my SED solution.....
no, please do. I suspect it will be slower, since I am calling printf every time. Might be better to save them in memory before printing at the right time. But well, i prefer readability more than speed concerns.
NB: please change your sed to take care of things like
Code:
the
thesis is
done
Last edited by ghostdog74; 11-10-2009 at 10:12 PM.
It was more of a joke, actually. I think it's funny how people often try to get everything done in one call to one command when it's almost never necessary, because it will probably just go into a script, anyway.
Kevin Barry
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.