Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Please help with sed

39 views
Skip to first unread message

buck

unread,
Jan 17, 2013, 5:40:34 PM1/17/13
to
I need to replace a question mark sandwiched within a leading and a
trailing space:

" ? "

with " > " (or something else that looks like a right-pointing triangle).

Would some kind soul provide correct sed syntax, please?
--
buck

Auric__

unread,
Jan 17, 2013, 6:00:24 PM1/17/13
to
Just escape the spaces, ? and > with a backslash:

sed s/\ \?\ /\ \>\ /

--
Learn to throw your voice! Fool your friends! Fun at parties!

grey...@mail.com

unread,
Jan 17, 2013, 6:37:20 PM1/17/13
to
Context. please.

A whole lot of tools will do this, usually using the same
syntax.


!/usr/bin/perl
open A, "<file1";
open B, ">file2";
while(<A>){
if ($_=~/\s\?\s/){
$_=~s/\s\?\s/ > /g;
}
print B;
}
Do you expect more than ' ? ' perl line?.. If not
,lost the trailing g

--
maus
.
.
...

Dan C

unread,
Jan 17, 2013, 9:15:36 PM1/17/13
to
man sed


--
"Ubuntu" -- an African word, meaning "Slackware is too hard for me".
"Bother!" said Pooh, as he descended down into Shayol Ghul.
Usenet Improvement Project: http://twovoyagers.com/improve-usenet.org/
Thanks, Obama: http://brandybuck.site40.net/pics/politica/thanks.jpg

grey...@mail.com

unread,
Jan 18, 2013, 5:21:32 AM1/18/13
to
On 2013-01-17, Auric__ <not.m...@email.address> wrote:
> buck wrote:
>
>> I need to replace a question mark sandwiched within a leading and a
>> trailing space:
>>
>> " ? "
>>
>> with " > " (or something else that looks like a right-pointing triangle).
>>
>> Would some kind soul provide correct sed syntax, please?
>
> Just escape the spaces, ? and > with a backslash:
>
> sed s/\ \?\ /\ \>\ /
>

Yup.

shmar...@ticnet.com

unread,
Jan 18, 2013, 9:24:12 AM1/18/13
to
uh... nope.

It seems to depend on the environment. From the command line, if single quotes are being used, there shouldn't be any
need to escape anything.

~/tmp/tryouts $ echo how?now ? brown?cow | sed 's/ ? / > /g'
how?now > brown?cow

Also, in GNU sed version 4.1.5 a '\?' isn't an escaped '?'. It means zero or one of the previous character. Your answer
of sed s/\ \?\ /\ \>\ / will match any character followed by a blank. I can't think of any reason a blank would ever need to be escaped.

-

Auric__

unread,
Jan 18, 2013, 9:35:57 AM1/18/13
to
shmartonak1 wrote:

> On Friday, January 18, 2013 4:21:32 AM UTC-6, grey...@mail.com wrote:
>> On 2013-01-17, Auric__ <not.m...@email.address> wrote:
>>
>> > buck wrote:
>> >
>> >> I need to replace a question mark sandwiched within a leading and a
>> >> trailing space:
>> >>
>> >> " ? "
>> >>
>> >> with " > " (or something else that looks like a right-pointing
>> >> triangle).
>> >>
>> >> Would some kind soul provide correct sed syntax, please?
>> >
>> > Just escape the spaces, ? and > with a backslash:
>> >
>> > sed s/\ \?\ /\ \>\ /
>>
>> Yup.
>
> uh... nope.
>
> It seems to depend on the environment. From the command line, if single
> quotes are being used, there shouldn't be any need to escape anything.
>
> ~/tmp/tryouts $ echo how?now ? brown?cow | sed 's/ ? / > /g'
> how?now > brown?cow

Ok, good to know. I didn't use any quoting; I did it as I posted.

> Also, in GNU sed version 4.1.5 a '\?' isn't an escaped '?'. It means
> zero or one of the previous character. Your answer of sed s/\ \?\ /\
> \>\ / will match any character followed by a blank. I can't think of
> any reason a blank would ever need to be escaped.

That's odd. GNU sed 4.2.1 acted as I expected with what I posted. I tested
on a file that contained this:

x ? x
x a x
x ? x

I entered this at the terminal:

sed s/\ \?\ /\ \>\ / testfile

...and sed returned this:

x > x
x a x
x > x

So... shrug.

--
I was betrayed, one more day of my short life.

buck

unread,
Jan 18, 2013, 2:07:26 PM1/18/13
to
shmar...@ticnet.com wrote in
news:1ef9dc54-b34e-4d06...@googlegroups.com:

> On Friday, January 18, 2013 4:21:32 AM UTC-6, grey...@mail.com wrote:
>> On 2013-01-17, Auric__ <not.m...@email.address> wrote:
>>
>> > buck wrote:
>> >> I need to replace a question mark sandwiched within a leading and
>> >> a trailing space:
>> >> " ? "
>> >> with " > " (or something else that looks like a right-pointing
>> >> triangle).
>>
>> > Just escape the spaces, ? and > with a backslash:
>> > sed s/\ \?\ /\ \>\ /
>>
>
> uh... nope.
>
> It seems to depend on the environment. From the command line, if
> single quotes are being used, there shouldn't be any need to escape
> anything.
>
> ~/tmp/tryouts $ echo how?now ? brown?cow | sed 's/ ? / > /g'
> how?now > brown?cow
>
> Also, in GNU sed version 4.1.5 a '\?' isn't an escaped '?'. It means
> zero or one of the previous character. Your answer of sed s/\ \?\ /\
> \>\ / will match any character followed by a blank. I can't think of
> any reason a blank would ever need to be escaped.

For context, here is the applicable portion of the bash script:
cat $i.br\
| sed s/\ \?\ /\ \>\ /g\
| sed 's/ ? / > /g'\
| sed -e s/`echo -en "\002"`/"\<CENTER\>"/g\
| sed -e s/`echo -en "\003"`/"\<\/CENTER\>"/g\
| sed -e s/`echo -en "\017""\027"`/"\<\/FONT\>"/g\
| sed -e s/`echo -en "\026""\016"`/"\<FONT COLOR='green'\>"/g\
| sed -e s/`echo -en "\016"`/"\<FONT COLOR='red'\>"/g\
| sed -e s/`echo -en "\026"`/"\<FONT COLOR='blue'\>"/g\
| sed -e s/`echo -en "\017"`/"\<\/FONT\>"/g\
| sed -e s/`echo -en "\027"`/"\<\/FONT\>"/g\ > $i.sed

Not that neither of the first 2 sed lines finds the question mark, and I
have no idea how to provide an echo that causes sed to cooperate.

Dan C, I defy you to provide anything from the man page that explains
this, Until you do, bugger off.
--
buck

Jules Richardson

unread,
Jan 18, 2013, 4:55:29 PM1/18/13
to
On Fri, 18 Jan 2013 14:35:57 +0000, Auric__ wrote:
>> Also, in GNU sed version 4.1.5 a '\?' isn't an escaped '?'. It means
>> zero or one of the previous character. Your answer of sed s/\ \?\ /\
>> \>\ / will match any character followed by a blank. I can't think of
>> any reason a blank would ever need to be escaped.
>
> That's odd. GNU sed 4.2.1 acted as I expected with what I posted.

Hmm, so it works in 4.2.1...

I've got 4.1.5 here and as the previous poster said, trying to just
escape the '?' fails. Putting it between square brackets (i.e. sed -e
's/ [?] / > /') seems to work OK in v4.1.5.

cheers

Jules

Auric__

unread,
Jan 18, 2013, 6:12:50 PM1/18/13
to
buck wrote:

> Dan C, I defy you to provide anything from the man page that explains
> this, Until you do, bugger off.

You may not like his attitude, but I can't remember ever seeing him in the
wrong.

--
Emotional swords slash my soul.

Dan C

unread,
Jan 19, 2013, 12:24:45 AM1/19/13
to
On Fri, 18 Jan 2013 19:07:26 +0000, buck wrote:

> Dan C, I defy you to provide anything from the man page that explains
> this, Until you do, bugger off.

I'll not waste my time reading/quoting you such trivial shit, doofus.

> User-Agent: Xnews/2006.08.24

Bugger off, Win-droid.


--
"Ubuntu" -- an African word, meaning "Slackware is too hard for me".
"Bother!" said Pooh, as he got cattle-prodded in the groin.

Loki Harfagr

unread,
Jan 19, 2013, 6:03:45 AM1/19/13
to
Fri, 18 Jan 2013 19:07:26 +0000, buck did cat :
I don't understand why you want to "provide an echo" since your
script uses 'cat' ?

this replace condition should do what you asked in your first post
(at least my interpretation of it as the "within a leading and a
trailing space" might need to chose if strict or not):
's/ \x3f / > /'
for instance (adding env suspenders and a 'g'lobally):
$ echo 'a ? b ? c ?? d ? end' | sed --posix 's/ \x3f / > /g'

In case it wouldn't work on your data the best next idea will be that
you'd deposit on some DLable place an example of short input file and
its desired output file.

>
> Dan C, I defy you to provide anything from the man page that explains
> this, Until you do, bugger off.

that's another question, another one could be why is your HTML code shouting?-)

Rob Windgassen

unread,
Jan 19, 2013, 7:45:41 AM1/19/13
to
On Fri, 18 Jan 2013 06:24:12 -0800, shmartonak wrote:


> It seems to depend on the environment. From the command line, if single
> quotes are being used, there shouldn't be any need to escape anything.

This depends on the shell you use and its settings, because the shell may
try to do pathname expansion (e.g. see man bash) or not.
Enclosing arguments within single quotes will prevent the shell to do that.

Rob

John McCue

unread,
Jan 19, 2013, 9:32:36 AM1/19/13
to
buck <bu...@private.mil> wrote:
<snip>
>
> Dan C, I defy you to provide anything from the man page that explains
> this, Until you do, bugger off.

Not the easest thing to do, but in "man sed" it says to
do "info sed". I dislike info, but under the sed info
page you select "* Regular Expressions::" and regular
exressions are described in relation to sed. Not too
intuitive though :)

John

Helmut Hullen

unread,
Jan 19, 2013, 9:39:00 AM1/19/13
to
Hallo, John,

Du meintest am 19.01.13:

>> Dan C, I defy you to provide anything from the man page that
>> explains this, Until you do, bugger off.

> Not the easest thing to do, but in "man sed" it says to
> do "info sed". I dislike info,

Then try "pinfo" ...

Viele Gruesse
Helmut

"Ubuntu" - an African word, meaning "Slackware is too hard for me".

Rob Windgassen

unread,
Jan 19, 2013, 11:42:13 AM1/19/13
to
On Sat, 19 Jan 2013 15:39:00 +0100, Helmut Hullen wrote:


>
> Then try "pinfo" ...

$ pinfo sed
bash: pinfo: command not found

... but, pinfo is available at slackbuilds.org:

http://slackbuilds.org/repository/14.0/misc/pinfo/

Good luck,

Rob

buck

unread,
Jan 19, 2013, 3:03:19 PM1/19/13
to
For google, here's what I settled for:

cat $i.br\
| sed -e s/`echo '^\x3f\x20'`/"\&bull; "/g\
| sed -e s/`echo '\x20\x3f\x20'`/" \&rArr; "/g\
| sed -e s/`echo -en "\002"`/"\<CENTER\>"/g\
| sed -e s/`echo -en "\003"`/"\<\/CENTER\>"/g\
| sed -e s/`echo -en "\017""\027"`/"\<\/FONT\>"/g\
| sed -e s/`echo -en "\026""\016"`/"\<FONT COLOR='green'\>"/g\
| sed -e s/`echo -en "\016"`/"\<FONT COLOR='red'\>"/g\
| sed -e s/`echo -en "\026"`/"\<FONT COLOR='blue'\>"/g\
| sed -e s/`echo -en "\017"`/"\<\/FONT\>"/g\
| sed -e s/`echo -en "\027"`/"\<\/FONT\>"/g\ > $i.sed

Loki, I find it easier to edit HTML when the tags are upper case.
--
buck

Loki Harfagr

unread,
Jan 20, 2013, 5:29:56 AM1/20/13
to
Sat, 19 Jan 2013 20:03:19 +0000, buck did cat :

> For google, here's what I settled for:
>
> cat $i.br\
> | sed -e s/`echo '^\x3f\x20'`/"\&bull; "/g\
> | sed -e s/`echo '\x20\x3f\x20'`/" \&rArr; "/g\
> | sed -e s/`echo -en "\002"`/"\<CENTER\>"/g\
> | sed -e s/`echo -en "\003"`/"\<\/CENTER\>"/g\
> | sed -e s/`echo -en "\017""\027"`/"\<\/FONT\>"/g\
> | sed -e s/`echo -en "\026""\016"`/"\<FONT COLOR='green'\>"/g\
> | sed -e s/`echo -en "\016"`/"\<FONT COLOR='red'\>"/g\
> | sed -e s/`echo -en "\026"`/"\<FONT COLOR='blue'\>"/g\
> | sed -e s/`echo -en "\017"`/"\<\/FONT\>"/g\
> | sed -e s/`echo -en "\027"`/"\<\/FONT\>"/g\ > $i.sed

There are a few points I don't understand in your reply :D)
Let's try just the simple ones ;-)
1 - your say "For google" , what is Google doing there?
2 - why the echo expansion since you're in a cat and have a \xnn?
3 - why the cascading pipes instead of joint sed commands?

for example (related to points 2 and 3):

---(bench file)-
$ cat -A infil
to bip or not ? two beeps$
? I am the lone mark$
this is a ^Bcenter^C fold$
-------------

---(test)-----
$ cat infile | \
sed 's/^\x3f\x20/\&bull; /g;
s/\x20\x3f\x20/\&rArr; /g;
s/\x02/\<CENTER\>/g;
s/\x03/\<\/CENTER\>/g'
-----------

---(output)---
to bip or not&rArr; two beeps
&bull; I am the lone mark
this is a <CENTER>center</CENTER> fold
-----------

>
> Loki, I find it easier to edit HTML when the tags are upper case.

if the resulting HTML is FYEO that's OK but if that's supposed to
go online at some time I think you know it is deprecated (and some
expressions as well, ex: "<center>") and since then you'll have to
tidy it up why not taking it easier and directly write your parser
in gawk? (eventually using a parse rules file and a simple parser engine)

buck

unread,
Jan 20, 2013, 3:43:11 PM1/20/13
to
Loki Harfagr <l0...@thedarkdesign.free.fr.INVALID> wrote in
news:pan.2013.01...@thedarkdesign.free.fr.INVALID:

> Sat, 19 Jan 2013 20:03:19 +0000, buck did cat¶ÿ:
>
>> For google, here's what I settled for:
>>
>> cat $i.br\
>> | sed -e s/`echo '^\x3f\x20'`/"\&bull; "/g\
>> | sed -e s/`echo '\x20\x3f\x20'`/" \&rArr; "/g\
>> | sed -e s/`echo -en "\002"`/"\<CENTER\>"/g\
>> | sed -e s/`echo -en "\003"`/"\<\/CENTER\>"/g\
>> | sed -e s/`echo -en "\017""\027"`/"\<\/FONT\>"/g\
>> | sed -e s/`echo -en "\026""\016"`/"\<FONT COLOR='green'\>"/g\
>> | sed -e s/`echo -en "\016"`/"\<FONT COLOR='red'\>"/g\
>> | sed -e s/`echo -en "\026"`/"\<FONT COLOR='blue'\>"/g\
>> | sed -e s/`echo -en "\017"`/"\<\/FONT\>"/g\
>> | sed -e s/`echo -en "\027"`/"\<\/FONT\>"/g\ > $i.sed
>
> There are a few points I don't understand in your reply :D)
> Let's try just the simple ones ;-)
> 1 - your say "For google" , what is Google doing there?

google is here because searching newsgroups containing "os.linux" has
provided me with many solutions. It has also pissed me off when the OP
fails to post the solution, so I posted this solution.

> 2 - why the echo expansion since you're in a cat and have a \xnn?

When "echo" is absent, the file output by sed is unchanged. I've no idea
why; don't care because echo makes sed work for me.

> 3 - why the cascading pipes instead of joint sed commands?

The cascade was done as I discovered stuff in the txt source that was not
properly rendered in html. Easier to test when cascaded. Never bothered
to join. In fact. the file output in this portion of the script is run
through sed a second time to convert some octal to decimal for html; eg
0205 to &#133;. That sed is also cascaded because I found 8 characters
incorrectly rendered, and not all of them are pretty when converted from
octal to decimal - so I picked something I liked better; eg 0267 to &#
149; rather than to &#183;.

>> Loki, I find it easier to edit HTML when the tags are upper case.
>
> if the resulting HTML is FYEO that's OK but if that's supposed to
> go online at some time I think you know it is deprecated (and some
> expressions as well, ex: "<center>") and since then you'll have to
> tidy it up why not taking it easier and directly write your parser
> in gawk? (eventually using a parse rules file and a simple parser
> engine)

I got tired of editing the txt files to replace the question mark, so
fixed the 5 bash scripts that convert txt to html. The html is online
and has been for years. It is a knowledge base and gets added to weekly.
At this time, there are 6006 entries in that KB. I don't give a damn
about deprecated so long as all browsers correctly render the pages.
When that stops happening, I'll cross that bridge.

Rather than gawk, perl seems more elegant, but sed + bash works...

Thank you for your time. I always enjoy your posts and often learn from
them.
--
buck

Grant

unread,
Jan 20, 2013, 4:43:34 PM1/20/13
to
On 20 Jan 2013 20:43:11 GMT, buck <bu...@private.mil> wrote:

>Loki Harfagr <l0...@thedarkdesign.free.fr.INVALID> wrote in
>news:pan.2013.01...@thedarkdesign.free.fr.INVALID:
>
>> Sat, 19 Jan 2013 20:03:19 +0000, buck did cat?:
...
>When that stops happening, I'll cross that bridge.

I agree it is pointless to rewrite working markup.

Maybe add CSS too? Would remove need for some of the tag salad you're
inserting (though changing the nature of said salad).
>
>Rather than gawk, perl seems more elegant, but sed + bash works...

Gawk is far easier than perl, I only use perl for 'technical' stuff like
utilising sockets, and there I was lucky to have some guidance.

Sed + bash is the hard way (cut & paste harder), gawk is easier,
perl is almost write-only code ;)

Grant.

grey...@mail.com

unread,
Jan 21, 2013, 4:55:01 AM1/21/13
to
Sed, in my opinion, is an extremely powerful tool, and in the hands of
someone who `just tries it', can cause chaos. O'Reillys book on
`Sed and awk' recommends trying it with precautions until experienced.

With perl, you can define input and (temperory) output files,
and copy the output file to input when satisfied.

Awk is `wunnerful'


-

Grant

unread,
Jan 21, 2013, 6:06:37 PM1/21/13
to
On 21 Jan 2013 09:55:01 GMT, grey...@mail.com wrote:

...
>Sed, in my opinion, is an extremely powerful tool, and in the hands of
>someone who `just tries it', can cause chaos. O'Reillys book on
>`Sed and awk' recommends trying it with precautions until experienced.
>
>With perl, you can define input and (temperory) output files,
>and copy the output file to input when satisfied.

Much as letting the result go to the console until one is satisfied with it?

That's how I tend to work with one-liners, until they Do the Right Thing ;)
>
>Awk is `wunnerful'

Yup :) I think is a good starting language, simple and easy to learn from
the online manual and there's an active newsgroup, comp.lang.awk.

People need to learn regular expressions (RE) from somewhere too, that
knowledge will apply (with syntax variations) to all 'modern' computer
languages as REs concisely define text search terms.

Grant.

Jules Richardson

unread,
Jan 22, 2013, 9:06:49 AM1/22/13
to
On Tue, 22 Jan 2013 10:06:37 +1100, Grant wrote:

> On 21 Jan 2013 09:55:01 GMT, grey...@mail.com wrote:
>
> ...
>>Sed, in my opinion, is an extremely powerful tool, and in the hands of
>>someone who `just tries it', can cause chaos. O'Reillys book on `Sed
>>and awk' recommends trying it with precautions until experienced.
>>
>>With perl, you can define input and (temperory) output files, and copy
>>the output file to input when satisfied.
>
> Much as letting the result go to the console until one is satisfied with
> it?

I don't know, the wrong kind of text on the console can make it catch
fire.

Loki Harfagr

unread,
Jan 22, 2013, 1:01:42 PM1/22/13
to
Tue, 22 Jan 2013 14:06:49 +0000, Jules Richardson did cat :
that's the reason why the first lesson a console user *must* pass is
to know how to type blindly
$ reset<enter>
or
$ echo '<ctrlV><escape>c'<enter>
and for some cases:
$ kbd_mode -a

on the two or three mappings that may exist in his vicinity
(ex: qwerty/azerty/qwertz I decided I'd took a bet on chance meeting dvorak ;-)

it is commonly assumed that the typist might indulge himself to fail
his first try at it but in case of repeated mistakes he'll be
banned off the pizza treat and get as much weeks on quiche as he barfed.

(in case you're asked questions about all this just say it's some Urban Legend :-&)

John McCue

unread,
Jan 23, 2013, 9:09:17 AM1/23/13
to
Helmut Hullen <Hel...@hullen.de> wrote:
> Hallo, John,
>
<snip>
>
> Then try "pinfo" ...
>
> Viele Gruesse
> Helmut

Thanks for the information, compiled and installed
pinfo and at least I can now deal with info pages :)

John

jo...@wexfordpress.com

unread,
Feb 21, 2013, 10:14:21 AM2/21/13
to
On Jan 18, 5:21 am, greyma...@mail.com wrote:
You can d something similar in vim, which may be more convenient that
putting together a sed run.

Joe Rosevear

unread,
Feb 23, 2013, 3:14:27 PM2/23/13
to
jo...@wexfordpress.com <jo...@wexfordpress.com> wrote:
> On Jan 18, 5:21?am, greyma...@mail.com wrote:
>> On 2013-01-17, Auric__ <not.my.r...@email.address> wrote:
>>
>> > buck wrote:
>>
>> >> I need to replace a question mark sandwiched within a leading and a
>> >> trailing space:
>>
>> >> " ? "
>>
>> >> with " > " ?(or something else that looks like a right-pointing triangle).
>>
>> >> Would some kind soul provide correct sed syntax, please?
>>
>> > Just escape the spaces, ? and > with a backslash:
>>
>> > ? sed s/\ \?\ /\ \>\ /
>>
>> Yup.
>>
>> --
>> maus
>> ?.
>> ? .
>> ...
>
> You can d something similar in vim, which may be more convenient that
> putting together a sed run.

I couldn't get a simple use of sed, like the above, to work. Here is a
sledge hammer approach that worked for me.

I used this test file called try.txt:

vvv
Hello Friends
This is a test ? ? ? .
? ? ? ? .
^^

I did this

cat try.txt | xxd -p | xxd -p | sed 's/0a//g' | xxd -p -r | \
sed 's/203f20/203e20/g' | xxd -p -r

which produced this output:

vvv
Hello Friends
This is a test > > > .
> > > > .
^^^

xxd is a hexdump tool. -p gives longer lines, but sadly they still
contain newlines. So I piped the hexdump of the hexdump through sed to
remove the newlines. I piped that through -p -r which reverses the
second hexdump, and I piped that through sed again to change the
sandwiched ? marks to sandwiched > marks (still in the form of a
hexdump). The final -p -r reverses the first hexdump.

If you play with this a bit, it's really not as perverse as it might
appear, and it avoid the confusion of using escapes in sed.

--
http://JosephRosevear.com
http://RosevearSoftware.com

Grant

unread,
Feb 23, 2013, 9:08:29 PM2/23/13
to
Awk (gawk) is much easier to use:

grant@deltree:~$ cat try.txt
Hello Friends
This is a test ? ? ? .
? ? ? ? .

grant@deltree:~$ awk '{ gsub(/ \? /," > "); print }' try.txt
Hello Friends
This is a test > > > .
> > > > .

Grant.
0 new messages