|
Home > Archive > Unix Shell > March 2006 > problems with sed! help please! it's urgent!
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
problems with sed! help please! it's urgent!
|
|
| sleepy_deni 2006-03-19, 12:02 pm |
| This is my problem. I must solve it usig sed (till tomorrow):
For every line of the files given as parameters on the
command line exchange the first word with the third
word. The words contains only letters and are separated
by anything else.
If you can give my a hint or something, i would appreciate.
thanks
| |
| Stephane CHAZELAS 2006-03-19, 12:02 pm |
| 2006-03-19, 06:07(-08), sleepy_deni:
> This is my problem. I must solve it usig sed (till tomorrow):
>
> For every line of the files given as parameters on the
> command line exchange the first word with the third
> word. The words contains only letters and are separated
> by anything else.
>
> If you can give my a hint or something, i would appreciate.
> thanks
See the "s" command to "s"ubstitute, and a manual about regular
expressions.
s/<regular-expression>/<replacement-string>/
You can catch what is matched by a part of the regexp in a
numbered register and reference it in the <replacement-string>
s/...\(...\).../foo \1 bar/
for instance.
I'm sure your teacher will have given you pointers to some
documentation, but generally the man pages are enough. See the
sed, regex (or regexp) man pages.
--
Stéphane
| |
| Loki Harfagr 2006-03-21, 3:17 am |
| Le Sun, 19 Mar 2006 06:07:43 -0800, sleepy_deni a écrit_:
> This is my problem. I must solve it usig sed (till tomorrow):
>
> For every line of the files given as parameters on the
> command line exchange the first word with the third
> word. The words contains only letters and are separated
> by anything else.
>
> If you can give my a hint or something, i would appreciate.
> thanks
As many hours passed by now, here's a closer hint, but
if youo didn't read what Stephane proposed it won't helpp
you much when your teacher will ask how you made it ;-)
(watch out the two small booby traps ;-)
$ sed 's/\([a-z]*\) *\([a-z]*\) *\([a-z]*\)/FIXED \3 \2 \1/' MISCFILES/somewords
| |
| Icarus Sparry 2006-03-21, 3:17 am |
| On Sun, 19 Mar 2006 19:26:32 +0100, Loki Harfagr wrote:
> Le Sun, 19 Mar 2006 06:07:43 -0800, sleepy_deni a écrit_:
>
>
> As many hours passed by now, here's a closer hint, but
> if youo didn't read what Stephane proposed it won't helpp
> you much when your teacher will ask how you made it ;-)
> (watch out the two small booby traps ;-)
>
> $ sed 's/\([a-z]*\) *\([a-z]*\) *\([a-z]*\)/FIXED \3 \2 \1/' MISCFILES/somewords
I think to solve the problem you need many more backreferences. Of course
this makes it even more unlikely that the teacher will accept it. I can
see more than 2 problems with you solution, in particular it is not clear
to me that your handling of and input line "abc" is a boobytrap.
#!/bin/sh
WD='[A-Za-z][A-Za-z]*'
NWD='[^A-Za-z][^A-Za-z]*'
sed "s/\($WD\)\($NWD\)\($WD\)\($NWD\)\($WD\)/\5\2\3\4\1/" "$@"
| |
| Loki Harfagr 2006-03-21, 3:17 am |
| Le Sun, 19 Mar 2006 17:24:45 -0800, Icarus Sparry a écrit_:
> On Sun, 19 Mar 2006 19:26:32 +0100, Loki Harfagr wrote:
>
>
> I think to solve the problem you need many more backreferences. Of course
> this makes it even more unlikely that the teacher will accept it.
Oh, that's certain, but I just meant he'd be well advised to at leat
spot these two, or the teacher would have had a big grin ;-)
> I can
> see more than 2 problems with you solution,
That's certain too, though I voluntarily decided to restrain
the charfield to [a-z] and blank, it was already hard enough to read
for a trainee.
> in particular it is not clear
> to me that your handling of and input line "abc" is a boobytrap.
Er. You lost me there, i don't think I understand the question !?
Could you precise or sample ?
> #!/bin/sh
> WD='[A-Za-z][A-Za-z]*'
> NWD='[^A-Za-z][^A-Za-z]*'
> sed "s/\($WD\)\($NWD\)\($WD\)\($NWD\)\($WD\)/\5\2\3\4\1/" "$@"
Excellent, but I wonder if then the teacher would have imagined that all
of a sudden, after many years of gloom he finally achieved the perfect
stage, the perfect pupil !-D)
Here's what I think the teacher would have accepted :
$ sed 's/\([a-z][a-z]*\) *\([a-z][a-z]*\) *\([a-z][a-z]*\)/\3 \2 \1/'
Or simply (as the charfield is captive)
$ sed 's/\([a-z]*\) *\([a-z]*\) *\([a-z]*\)/\3 \2 \1/'
I admit the namespace is quite "twisted" with these :D)
There you see the 2 traps I had in mind were :
1- to cope with the unstable regexp (XX* instead of X*, same for blanks) and
2- get rid of the prefix FIXED (in caps to make it look like some magic ;-)
Oh, well, I promise I won't play tricks on pupils for a long time,
it was the end of the week-end and it's been a long long time I've
been wary wary calm and wise :-) Sorry "sleepy_deni" ! I just hope
it helped you.
| |
| Icarus Sparry 2006-03-21, 3:17 am |
| On Mon, 20 Mar 2006 13:15:13 +0100, Loki Harfagr wrote:
> Le Sun, 19 Mar 2006 17:24:45 -0800, Icarus Sparry a écrit_:
>
>
> Oh, that's certain, but I just meant he'd be well advised to at leat
> spot these two, or the teacher would have had a big grin ;-)
>
>
> That's certain too, though I voluntarily decided to restrain
> the charfield to [a-z] and blank, it was already hard enough to read
> for a trainee.
>
>
> Er. You lost me there, i don't think I understand the question !?
> Could you precise or sample ?
Sorry, I left out a space.
echo "a bc" | sed 's/\([a-z]*\) *\([a-z]*\) *\([a-z]*\)/FIXED \3 \2 \1/'
gives "FIXED bc a", which is reversing the first two words. This is caused
by the X* rather than XX*, so you did count it as a trap.
| |
| Loki Harfagr 2006-03-21, 3:17 am |
| Le Mon, 20 Mar 2006 08:50:43 -0800, Icarus Sparry a écrit_:
<snipped for brevity of thread in a thread>
>
> Sorry, I left out a space.
Ah, allright then :-)
> echo "a bc" | sed 's/\([a-z]*\) *\([a-z]*\) *\([a-z]*\)/FIXED \3 \2 \1/'
>
> gives "FIXED bc a", which is reversing the first two words. This is caused
> by the X* rather than XX*, so you did count it as a trap.
Well, at least you weren't trap, which would've been quite a shock to me ;-)
Oh, seems we won't have any news about this Monday morning of the OP
Sigh ...
| |
| Stephane Chazelas 2006-03-21, 8:05 am |
| On 19 Mar 2006 06:07:43 -0800, sleepy_deni wrote:
> This is my problem. I must solve it usig sed (till tomorrow):
>
> For every line of the files given as parameters on the
> command line exchange the first word with the third
> word. The words contains only letters and are separated
> by anything else.
>
> If you can give my a hint or something, i would appreciate.
> thanks
letter='[[:alpha:]]'
non_letter='[^[:alpha]]'
plus='\{1,\}' # ERE + BRE equivalent
word="\($letter$plus\)"
sep="\($non_letter$plus\)"
# 1 2 3 4 5 6
sed "s/ ^\($non_letter*\)$word$sep$word$sep$word
/\1\6\3\4\5\2/"
--
Stephane
|
|
|
|
|