Unix Shell - sed problem

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > March 2005 > sed problem





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 sed problem
one2001boy@yahoo.com

2005-03-26, 2:48 am

Hello,

Is there a way to get sed work to match multiple lines?

I have an input file p.h with content below:

========================================
=============
extern PNG_EXPORT(png_uint_32,png_access_versio
n_number)
extern PNG_EXPORT(void,
png_set_sig_bytes)
extern PNG_EXPORT(void,
png_out,
png_in,
png_set_sig_bytes)
========================================
===========

I want to remove PNG_EXPORT(), this is the expected the output
========================================
======================
extern png_uint_32 png_access_version_number
extern void png_set_sig_bytes
extern void png_out png_in png_set_sig_bytes
========================================
======================

I created the following p.sed file to handle it, but it doesn't succeed.

/PNG_EXPORT/ {
$!N
s/PNG_EXPORT(\([^,]*\),\([^)].*\))/\1 \2/
P;D
}

any suggestions to make a change to get it work?

Thanks.
Laurent Vogel

2005-03-26, 6:05 pm


<one2001boy@yahoo.com> wrote:
> I have an input file p.h with content below:
>
> ========================================
=============
> extern PNG_EXPORT(png_uint_32,png_access_versio
n_number)
> extern PNG_EXPORT(void,
> png_set_sig_bytes)
> extern PNG_EXPORT(void,
> png_out,
> png_in,
> png_set_sig_bytes)
> ========================================
===========
>
> I want to remove PNG_EXPORT(), this is the expected the output
> ========================================
======================
> extern png_uint_32 png_access_version_number
> extern void png_set_sig_bytes
> extern void png_out png_in png_set_sig_bytes
> ========================================
======================


Hmm, I didn't try to undestand your logic, but here is what I get.
The idea is to continue append lines while a closing ')' is not found,
then replace the commas and closing parentheses with a space to get
your particular output.

/PNG_EXPORT(/{
s///
:loop
/)/!{
N
s/\n *//
b loop
}
s/[,)]/ /g
}

Laurent



Ed Morton

2005-03-26, 6:05 pm



one2001boy@yahoo.com wrote:
> Hello,
>
> Is there a way to get sed work to match multiple lines?
>
> I have an input file p.h with content below:
>
> ========================================
=============
> extern PNG_EXPORT(png_uint_32,png_access_versio
n_number)
> extern PNG_EXPORT(void,
> png_set_sig_bytes)
> extern PNG_EXPORT(void,
> png_out,
> png_in,
> png_set_sig_bytes)
> ========================================
===========
>
> I want to remove PNG_EXPORT(), this is the expected the output
> ========================================
======================
> extern png_uint_32 png_access_version_number
> extern void png_set_sig_bytes
> extern void png_out png_in png_set_sig_bytes
> ========================================
======================
>
> I created the following p.sed file to handle it, but it doesn't succeed.
>
> /PNG_EXPORT/ {
> $!N
> s/PNG_EXPORT(\([^,]*\),\([^)].*\))/\1 \2/
> P;D
> }
>
> any suggestions to make a change to get it work?


Change from sed to awk where things like this are trivial and obvious
after brief exposure to the language:

awk -vRS=")" '{sub("PNG_EXPORT\\(","");gsub(","," ")}$1=$1'

The above sets the Record Separator to a ")" so each multi-line
export...) is treated as a record, then deletes "PNG_EXPORT(", then
deletes every ",", then sets the first field to it's own value to force
awk to re-construct the input record using the default field separator
(i.e. a single space, thereby getting rid of all th extraneous newlines,
tabs, chains of blanks, etc. The side effect of the "true condition"
$1=$1 is to invoke the default action which is to print the current record.

Regards,

Ed.
Alan Connor

2005-03-26, 6:05 pm

On comp.unix.shell, in <6I6dnfEpmMZfKdjfRVn-oQ@comcast.com>, "Ed
Morton" wrote:

> one2001boy@yahoo.com wrote:
>
>
> Change from sed to awk where things like this are trivial and
> obvious after brief exposure to the language:
>


Why do you keep repeating this garbage? You _know_ awk and
therefore it is simple for you to use it.

The awk script I have snipped below reads like gibberish to me.
But I _know_ sed, and the sed solution is as clear as a bell.
May I suggest that you return to college and retake Logic 101?

Sed is an excellent tool, and the fact that so many _more_
accomplished shell scripters than you choose to use it instead of
awk should tell you something.

Your prejudice against sed is most unreasonable. Perhaps a few
sessions with a therapist are also in order?

What you are doing is MIS-informing people that come here to
learn about shellscripting, Ed, and that really STINKS.

People obviously want to learn sed, and it just pisses you
off because you don't know sed very well and can't strut
your knowledge before them.

So instead of taking the time to learn sed, you dis it and
tell them to use awk, which you know better.

Chidlish.

<snip>

AC
Ed Morton

2005-03-26, 6:05 pm



Alan Connor wrote:

> On comp.unix.shell, in <6I6dnfEpmMZfKdjfRVn-oQ@comcast.com>, "Ed
> Morton" wrote:
>
>
>
>
> Why do you keep repeating this garbage? You _know_ awk and
> therefore it is simple for you to use it.


Alan - I've been using sed for abut 20 years. I've been using awk for
about 5. I have an adequate working knowledge of both tools. If you
search the archives you'll see I often post sed solutions when
appropriate. They are both fine tools and each has it's place on my
toolbelt.

> The awk script I have snipped below reads like gibberish to me.
> But I _know_ sed, and the sed solution is as clear as a bell.
> May I suggest that you return to college and retake Logic 101?
>
> Sed is an excellent tool, and the fact that so many _more_
> accomplished shell scripters than you choose to use it instead of
> awk should tell you something.
>
> Your prejudice against sed is most unreasonable. Perhaps a few
> sessions with a therapist are also in order?


You misunderstand the term "prejudice". "prejudice" means judging
(judice) something before (pre) you have first hand experience of it. As
I mentioned above, I have about 20 years of experience with sed so
advising people not to use it for complex text manipulation tasks is a
well considered applicability judgment rather than prejudice.

You keep telling us that you know sed but don't know awk and in your
uninformed opinion sed is a better tool for every application. That is
PRECISELY the definition of prejudice.

> What you are doing is MIS-informing people that come here to
> learn about shellscripting, Ed, and that really STINKS.
>
> People obviously want to learn sed, and it just pisses you
> off because you don't know sed very well and can't strut
> your knowledge before them.
>
> So instead of taking the time to learn sed, you dis it and
> tell them to use awk, which you know better.
>
> Chidlish.



If you think sed is better for complex text manipulation jobs ask
yourself this - whey do you never see someone post an awk solution to a
problem and then have someone else follow up with a sed solution that
they claim is more easily readable? You've said youreslf in other posts
that sed is more like assembly language while awk is more like a high
level language so your position appears to be that large programs are
more easily written and understood in assembly language than in a high
level language. There's rather a large body of experience that would
disagree with that statement.

Ed.

> <snip>
>
> AC

Alan Connor

2005-03-26, 6:05 pm

On comp.unix.shell, in <euudnRdn6tTDVdjfRVn-ug@comcast.com>, "Ed Morton" wrote:
>
>
>
>
> Alan Connor wrote:
>
[vbcol=seagreen]
>
> Alan - I've been using sed for abut 20 years. I've been using
> awk for about 5. I have an adequate working knowledge of both
> tools. If you search the archives you'll see I often post sed
> solutions when appropriate.


When _you_ think they are appropriate. Others who are just as
experienced, or more so, post sed solutions to the same problems.

Obviously, _they_ think the sed solutions are appropriate.

> They are both fine tools and each
> has it's place on my toolbelt.


But you _prefer_ awk and wrongly dis sed. Regularly. You
and Chris F.A. Johnson.

>
>
> You misunderstand the term "prejudice". "prejudice" means
> judging (judice) something before (pre) you have first hand
> experience of it. As I mentioned above, I have about 20
> years of experience with sed so advising people not to use
> it for complex text manipulation tasks is a well considered
> applicability judgment rather than prejudice.
>


One that many experienced shellscripters obviously disagree with.

The "sed & awk" book by Dougherty and Robbins contains sed
solutions to extremely complex text manipulation tasks and they
chose to include it _with_ awk, yet before awk, in the same
instructional/reference book.

With the builtin functions of even a simple shell like sh,
sed can do anything that awk can do.

You _prefer_ awk, and that's fine.

But time and again, you dis sed and post awk solutions while
seemingly oblivious to the sed solutions that are posted by
various people.

When someone asks for a sed solution, and you don't know one,
then what you should do is just bow out.

Posting an awk solution and dissing sed when someone wants
a sed solution, and wants to learn sed, is just RUDE.

To tell people that awk is superior doesn't do anything but
demonstrate your ignorance, at best.

They are just _different_.

<snip>

AC

one2001boy@yahoo.com

2005-03-26, 6:05 pm

Ed Morton wrote:
>
>
> one2001boy@yahoo.com wrote:
>
>
>
> Change from sed to awk where things like this are trivial and obvious
> after brief exposure to the language:
>
> awk -vRS=")" '{sub("PNG_EXPORT\\(","");gsub(","," ")}$1=$1'
>
> The above sets the Record Separator to a ")" so each multi-line
> export...) is treated as a record, then deletes "PNG_EXPORT(", then
> deletes every ",", then sets the first field to it's own value to force
> awk to re-construct the input record using the default field separator
> (i.e. a single space, thereby getting rid of all th extraneous newlines,
> tabs, chains of blanks, etc. The side effect of the "true condition"
> $1=$1 is to invoke the default action which is to print the current record.


My real goal is to have the input as below:

extern PNG_EXPORT(int,png_check_sig) PNGARG((png_bytep sig, int num));
extern PNG_EXPORT(void,png_set_sig_bytes) PNGARG((png_structp png_ptr,
int num_bytes));
extern PNG_EXPORT(png_structp,png_create_read_s
truct)
PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
png_error_ptr error_fn, png_error_ptr warn_fn));


the output is:

extern int png_check_sig(png_bytep sig, int num);
extern void png_set_sig_bytes(png_structp png_ptr,int num_bytes);
extern png_structp png_create_read_struct(png_const_charp user_png_ver,
png_voidp error_ptr,png_error_ptr error_fn, png_error_ptr warn_fn);


I find the following sed script will work:

/PNG_EXPORT(/{
:loop
/));/!{
N
s/\n *//
b loop
}
s/PNG_EXPORT(\([^,]*\),\([^)]*\)).*PNGARG(\([^)]*\))/\1 \2\3/
}

not sure how easy it can be done in awk.
I am sure if getting more familar with awk, it should be easy to get the
job done in awk. thanks.



>
> Regards,
>
> Ed.

one2001boy@yahoo.com

2005-03-26, 6:05 pm

Laurent Vogel wrote:
> <one2001boy@yahoo.com> wrote:
>
>
>
> Hmm, I didn't try to undestand your logic, but here is what I get.
> The idea is to continue append lines while a closing ')' is not found,
> then replace the commas and closing parentheses with a space to get
> your particular output.
>
> /PNG_EXPORT(/{
> s///
> :loop
> /)/!{
> N
> s/\n *//
> b loop
> }
> s/[,)]/ /g
> }
>
> Laurent


With your script above, I am able to create a sed script

/PNG_EXPORT(/{
:loop
/));/!{
N
s/\n *//
b loop
}
s/PNG_EXPORT(\([^,]*\),\([^)]*\)).*PNGARG(\([^)]*\))/\1 \2\3/
}


it will take the input:

extern PNG_EXPORT(int,png_check_sig) PNGARG((png_bytep sig, int num));
extern PNG_EXPORT(void,png_set_sig_bytes) PNGARG((png_structp png_ptr,
int num_bytes));
extern PNG_EXPORT(png_structp,png_create_read_s
truct)
PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
png_error_ptr error_fn, png_error_ptr warn_fn));


and get the ouput:

extern int png_check_sig(png_bytep sig, int num);
extern void png_set_sig_bytes(png_structp png_ptr,int num_bytes);
extern png_structp png_create_read_struct(png_const_charp user_png_ver,
png_voidp error_ptr,png_error_ptr error_fn, png_error_ptr warn_fn);

One thing I am not sure is that how I can get the job done without
opening a file.

The following syntax won't work for sed.

sedstring="
/PNG_EXPORT(/{
:loop
/));/!{
N
s/\n *//
b loop
}
s/PNG_EXPORT(\([^,]*\),\([^)]*\)).*PNGARG(\([^)]*\))/\1 \2\3/
}"

sed $sedstring input_file output_file


Thank you.
Ed Morton

2005-03-26, 6:05 pm



one2001boy@yahoo.com wrote:
> Ed Morton wrote:
>
>
>
> My real goal is to have the input as below:
>
> extern PNG_EXPORT(int,png_check_sig) PNGARG((png_bytep sig, int num));
> extern PNG_EXPORT(void,png_set_sig_bytes) PNGARG((png_structp png_ptr,
> int num_bytes));
> extern PNG_EXPORT(png_structp,png_create_read_s
truct)
> PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
> png_error_ptr error_fn, png_error_ptr warn_fn));
>
>
> the output is:
>
> extern int png_check_sig(png_bytep sig, int num);
> extern void png_set_sig_bytes(png_structp png_ptr,int num_bytes);
> extern png_structp png_create_read_struct(png_const_charp user_png_ver,
> png_voidp error_ptr,png_error_ptr error_fn, png_error_ptr warn_fn);
>
>
> I find the following sed script will work:
>
> /PNG_EXPORT(/{
> :loop
> /));/!{
> N
> s/\n *//
> b loop
> }
> s/PNG_EXPORT(\([^,]*\),\([^)]*\)).*PNGARG(\([^)]*\))/\1 \2\3/
> }
>
> not sure how easy it can be done in awk.
> I am sure if getting more familar with awk, it should be easy to get the
> job done in awk. thanks.


Yup. Here is is in gawk:

awk -vRS=");"
'{$0=gensub(/PNG_EXPORT\(([^,]*),([^)]*).*PNGARG\((.*)/,"\\1
\\2\\3;","")}$1=$1'

Other awks don't have gensub() which works well here since it's got a
very similair syntax to sed for REs, but you can do something similair
with gsub() in other awks if you need to.

Regards,

Ed.
Janis Papanagnou

2005-03-26, 6:05 pm

Alan Connor wrote:

> "Ed Morton" wrote:
>
[vbcol=seagreen]
> To tell people that awk is superior doesn't do anything but
> demonstrate your ignorance, at best.
>
> They are just _different_.


It seems time to strip down the size of the postings when there is so
much noise in between the lines that there got lost what had been said.

Janis
Alan Connor

2005-03-26, 6:05 pm

On comp.unix.shell, in
<VSk1e.1903$FN4.1734@newssvr21.news.prodigy.com>,
"one2001boy@yahoo.com" wrote:

<a lot snipped away>

> One thing I am not sure is that how I can get the job done
> without opening a file.
>
> The following syntax won't work for sed.


>
> sedstring="
> /PNG_EXPORT(/{
> :loop
> /));/!{
> N
> s/\n *//
> b loop
> }
> s/PNG_EXPORT(\([^,]*\),\([^)]*\)).*PNGARG(\([^)]*\))/\1 \2\3/
> }"
>
> sed $sedstring input_file output_file
>


Yeh. I tried running a script using a here document, and it
wouldn't work either:

cat file | sed <<EOF
s/....//
EOF


AC

Ed Morton

2005-03-26, 6:05 pm



Alan Connor wrote:

<snip>
> But you _prefer_ awk and wrongly dis sed. Regularly. You
> and Chris F.A. Johnson.


If you want to have a discussion you need to read what the other person
is posting. For the last time - they are both good tools and they each
have applications for which they are better suited.

<snip>
> With the builtin functions of even a simple shell like sh,
> sed can do anything that awk can do.


That's what I did for 15 years before discovering awk. Talk about a
breath of fresh air. You just said that sed needs other facilities to
compete with awk. I've heard you go through this before - sed's as
useful as awk, except when it isn't, and then there's shell. That's not
much of an argument.

> You _prefer_ awk, and that's fine.
> But time and again, you dis sed and post awk solutions while
> seemingly oblivious to the sed solutions that are posted by
> various people.
>
> When someone asks for a sed solution, and you don't know one,
> then what you should do is just bow out.


As I mentioned, I've spent 20 years using sed and I doubt if there's
much I couldn't get it to do. I just honestly can't imagine why I'd
bother when there's another tool that lets me write a solution faster
and clearer.

> Posting an awk solution and dissing sed when someone wants
> a sed solution, and wants to learn sed, is just RUDE.


Nonsense. We're constantly seeing people ask how to do a job with a
given tool just because that's the only tool they thought could do the
job. Alerting them to a better alternative is always appropriate.

> To tell people that awk is superior doesn't do anything but
> demonstrate your ignorance, at best.
>
> They are just _different_.


So are scissors and a lawn mower. I'd tend to use one of them for
cutting hair and the other for cutting grass but you're right - it may
take a while but you certainly could cut your grass with a pair of
scissors if you don't know how to operate a lawn mower.

Much as I'd love to think that anyone but you is hanging on my every
word, I doubt if my opinion is being taken as gospel by other
participants in this NG. Each time I post an awk solution to a "sed
question" either:

a) someone else posts a sed solution and so the OP can compare for
themselves and decide which to use, or
b) no sed solution gets posted and you can infer what you want from that
but at least the OP gets a workable solution.

Sounds to me like a win-win situation for the OP and anyone else reading
the thread.

I'm done with this discussion and I don't intend to rehash it again in a
few days. Hopefully you now understand where I'm coming from and won't
post another challenging email without first reading the responses
you've gotten so far and learning how to use awk so you too can make a
fair comparison.

Ed.

> <snip>
>
> AC
>

Ed Morton

2005-03-26, 6:05 pm



Alan Connor wrote:

> On comp.unix.shell, in
> <VSk1e.1903$FN4.1734@newssvr21.news.prodigy.com>,
> "one2001boy@yahoo.com" wrote:
>
> <a lot snipped away>
>
>
>
>
>
> Yeh. I tried running a script using a here document, and it
> wouldn't work either:
>
> cat file | sed <<EOF
> s/....//
> EOF
>


You don't need a here document just appropriate arguments and quoting:

sedstring="
/PNG_EXPORT(/{
:loop
/));/!{
N
s/\n *//
b loop
}
s/PNG_EXPORT(\([^,]*\),\([^)]*\)).*PNGARG(\([^)]*\))/\1 \2\3/
}"
sed -e "$sedstring" input_file > output_file

Regards,

Ed.
Alan Connor

2005-03-26, 6:05 pm

On comp.unix.shell, in <d24nln$tdd$1@online.de>, "Janis
Papanagnou" wrote:

> Alan Connor wrote:
>
>
>
> It seems time to strip down the size of the postings when there
> is so much noise in between the lines that there got lost what
> had been said.
>
> Janis


Indeed!

:-)

AC

Alan Connor

2005-03-26, 6:05 pm

On comp.unix.shell, in <kgk1e.4874$gI5.1490@newsread1.news.pas.earthlink.net>, "Alan Connor" wrote:
>


<snip>

Ed: I've heard what you have to say, ad naseum.

No, I am not going to bother reading the same
arguments again.

I think you are wrong.

You are going to have to live with that fact. Really.

No, Ed, you are not going to bully me into accepting your
specious arguments. Really.

Apparently, you expect to be taken as the World's Foremost
Authority on shell scripting.

Sorry, Ed. You aren't even close. There are a number people on
this group alone that make you look like a rank amatuer.

Some of those people regularly post sed solutions to problems
that you post awk solutions to, that work just as well.

Do you really think that others don't notice this?

That they are going to dismiss the evidence of their own
eyes because YOU say so?

ROTFLMAO!!!!

Put a sock in it, Ed. You are boring everyone to tears.

AC


Ed Morton

2005-03-26, 8:48 pm



one2001boy@yahoo.com wrote:
> Ed Morton wrote:
>
>
>
> Just tried the above command. it doesn't work. the input_file is:
>
> extern PNG_EXPORT(int,png_check_sig) PNGARG((png_bytep sig, int num));
> extern PNG_EXPORT(void,png_set_sig_bytes) PNGARG((png_structp png_ptr,
> int num_bytes));
> extern PNG_EXPORT(png_structp,png_create_read_s
truct)
> PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
> png_error_ptr error_fn, png_error_ptr warn_fn));
>
> Thanks.


In what way doesn't it work? Here's what I get in bash under cygwin:

PS1> cat input_file
extern PNG_EXPORT(int,png_check_sig) PNGARG((png_bytep sig, int num));
extern PNG_EXPORT(void,png_set_sig_bytes) PNGARG((png_structp png_ptr,
int num_bytes));
extern PNG_EXPORT(png_structp,png_create_read_s
truct)
PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
png_error_ptr error_fn, png_error_ptr warn_fn));
PS1> sedstring="
/PNG_EXPORT(/{
:loop
/));/!{
N
s/\n *//
b loop
}
s/PNG_EXPORT(\([^,]*\),\([^)]*\)).*PNGARG(\([^)]*\))/\1 \2\3/
}"
PS1> sed -e "$sedstring" input_file
extern int png_check_sig(png_bytep sig, int num);
extern void png_set_sig_bytes(png_structp png_ptr,int num_bytes);
extern png_structp png_create_read_struct(png_const_charp user_png_ver,
png_voidp error_ptr,png_error_ptr error_fn, png_error_ptr warn_fn);

which I think is what you wanted.

Ed.
one2001boy@yahoo.com

2005-03-27, 2:47 am

Ed Morton wrote:
>
>
> one2001boy@yahoo.com wrote:
>
>
>
> In what way doesn't it work? Here's what I get in bash under cygwin:
>
> PS1> cat input_file
> extern PNG_EXPORT(int,png_check_sig) PNGARG((png_bytep sig, int num));
> extern PNG_EXPORT(void,png_set_sig_bytes) PNGARG((png_structp png_ptr,
> int num_bytes));
> extern PNG_EXPORT(png_structp,png_create_read_s
truct)
> PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
> png_error_ptr error_fn, png_error_ptr warn_fn));
> PS1> sedstring="
> /PNG_EXPORT(/{
> :loop
> /));/!{
> N
> s/\n *//
> b loop
> }
> s/PNG_EXPORT(\([^,]*\),\([^)]*\)).*PNGARG(\([^)]*\))/\1 \2\3/
> }"
> PS1> sed -e "$sedstring" input_file
> extern int png_check_sig(png_bytep sig, int num);
> extern void png_set_sig_bytes(png_structp png_ptr,int num_bytes);
> extern png_structp png_create_read_struct(png_const_charp user_png_ver,
> png_voidp error_ptr,png_error_ptr error_fn, png_error_ptr warn_fn);
>
> which I think is what you wanted.


I tried the following code in linux:

/home/junk> cat t.sh
sedstring="
/PNG_EXPORT(/{
:loop
/));/!{
N
s/\n *//
b loop
}
s/PNG_EXPORT(\([^,]*\),\([^)]*\)).*PNGARG(\([^)]*\))/\1 \2\3/
}"

sed -e "$sestring" t.h >ttt

/home/junk> cat t.h
extern PNG_EXPORT(int,png_check_sig) PNGARG((png_bytep sig, int num));
extern PNG_EXPORT(void,png_set_sig_bytes) PNGARG((png_structp png_ptr,
int num_bytes));
extern PNG_EXPORT(png_structp,png_create_read_s
truct)
PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
png_error_ptr error_fn, png_error_ptr warn_fn));

/home/junk> ./t.sh

/home/junk> cat ttt
extern PNG_EXPORT(int,png_check_sig) PNGARG((png_bytep sig, int num));
extern PNG_EXPORT(void,png_set_sig_bytes) PNGARG((png_structp png_ptr,
int num_bytes));
extern PNG_EXPORT(png_structp,png_create_read_s
truct)
PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
png_error_ptr error_fn, png_error_ptr warn_fn));

the sed verion in linux is GNU sed version 4.0.8

I also tried bash and GNU sed version '4.1.2' under windows, the result
is the same as above.

Also, in bash,

/home/junk$ sedstring="
> /PNG_EXPORT(/{
> :loop
> /));/!{

bash: !{: event not found
> N
> s/\n *//
> b loop
> }
> s/PNG_EXPORT(\([^,]*\),\([^)]*\)).*PNGARG(\([^)]*\))/\1 \2\3/
> }"


You see the error that "bash: !{: event not found"
Even if you try to put everything in one line for assignment, it will
fail with the same error.

/home/junk$ sedstring=" /PNG_EXPORT(/{ :loop /));/!{ N s/\n *// b loop }
s/PNG_EXPORT(\([^,]*\),\([^)]*\)).*PNGARG(\([^)]*\))/\1 \2\3/ }"
bash: !{: event not found




>
> Ed.

Ed Morton

2005-03-27, 2:47 am



one2001boy@yahoo.com wrote:
<snip>
> I tried the following code in linux:
>
> /home/junk> cat t.sh
> sedstring="
> /PNG_EXPORT(/{
> :loop
> /));/!{
> N
> s/\n *//
> b loop
> }
> s/PNG_EXPORT(\([^,]*\),\([^)]*\)).*PNGARG(\([^)]*\))/\1 \2\3/
> }"
>
> sed -e "$sestring" t.h >ttt


Note - you assigned "sedstring" but referenced "sestring" (missing a
"d") so this is the same as

sed -e "" t.h >ttt

which just reproduces the input.

<snip>
> Also, in bash,
>
> /home/junk$ sedstring="
> bash: !{: event not found
>
> You see the error that "bash: !{: event not found"


Looks like bash is interpretting part of the text. Instead of typing it
directly, go into the command-line editor (e.g. escape-v to get into vi)
then enter the assignment there. It never even ocurred to me to try
typing it one line at a time so I didn't see that failure until now.

Ed.
Alan Connor

2005-03-27, 2:47 am

On comp.unix.shell, in <ubq1e.1979$FN4.1780@newssvr21.news.prodigy.com>, "one2001boy@yahoo.com" wrote:

<snip>

> bash: !{: event not found


From man bash:

When the command history expansion facilities are being
used, the history expansion character, usually !, must be
quoted to prevent history expansion.

There are three quoting mechanisms: the escape character,
single quotes, and double quotes.

A non-quoted backslash (\) is the escape character. It
preserves the literal value of the next character that
follows, with the exception of <newline>. If a \<newline>
pair appears, and the backslash is not itself quoted, the
\<newline> is treated as a line continuation (that is, it
is removed from the input stream and effectively ignored).

endquote


AC

Janis Papanagnou

2005-03-28, 2:49 am

Alan Connor wrote:

> "Ed Morton" wrote:
>
[vbcol=seagreen]
> To tell people that awk is superior doesn't do anything but
> demonstrate your ignorance, at best.
>
> They are just _different_.


It seems time to strip down the size of the postings when there is so
much noise in between the lines that there got lost what had been said.

Janis
one2001boy@yahoo.com

2005-03-28, 2:49 am

Ed Morton wrote:
>
>
> one2001boy@yahoo.com wrote:
> <snip>
>
>
>
> Note - you assigned "sedstring" but referenced "sestring" (missing a
> "d") so this is the same as
>
> sed -e "" t.h >ttt


yes, it is my fault with the simple typo problem.
Thanks so much for your help. it works with the scritp file above.

There is one more issue that I am not sure sed do.

With the above sed script file,

extern PNG_EXPORT(int, png_check_sig) PNGARG((png_bytep sig, int num));
extern PNG_EXPORT(int, png_check_sig) PNGARG((png_bytep sig, int
num,char c));

will become:

extern int png_check_sig(png_bytep sig, int num);
extern int png_check_sig(png_bytep sig, int num, char c);

For those functions with the same function name png_check_sig(),
I only need to keep the first function.
the expected output is:
extern int png_check_sig(png_bytep sig, int num);

Is there a way for me to get rid of second function with sed?

one2001boy@yahoo.com

2005-03-28, 2:49 am

Alan Connor wrote:
> On comp.unix.shell, in <ubq1e.1979$FN4.1780@newssvr21.news.prodigy.com>, "one2001boy@yahoo.com" wrote:
>
> <snip>
>
>
>
> From man bash:
>
> When the command history expansion facilities are being
> used, the history expansion character, usually !, must be
> quoted to prevent history expansion.
>
> There are three quoting mechanisms: the escape character,
> single quotes, and double quotes.
>
> A non-quoted backslash (\) is the escape character. It
> preserves the literal value of the next character that
> follows, with the exception of <newline>. If a \<newline>
> pair appears, and the backslash is not itself quoted, the
> \<newline> is treated as a line continuation (that is, it
> is removed from the input stream and effectively ignored).
>
> endquote



adding \! will do.
Thanks.


>
>
> AC
>

Ed Morton

2005-03-28, 7:56 am



one2001boy@yahoo.com wrote:
> Ed Morton wrote:
>
>
>
> yes, it is my fault with the simple typo problem.
> Thanks so much for your help. it works with the scritp file above.
>
> There is one more issue that I am not sure sed do.
>
> With the above sed script file,
>
> extern PNG_EXPORT(int, png_check_sig) PNGARG((png_bytep sig, int num));
> extern PNG_EXPORT(int, png_check_sig) PNGARG((png_bytep sig, int
> num,char c));
>
> will become:
>
> extern int png_check_sig(png_bytep sig, int num);
> extern int png_check_sig(png_bytep sig, int num, char c);
>
> For those functions with the same function name png_check_sig(),
> I only need to keep the first function.
> the expected output is:
> extern int png_check_sig(png_bytep sig, int num);
>
> Is there a way for me to get rid of second function with sed?
>


This falls into the "I don't even want to think about it" category
because it's trivial in awk. Just tweak this script I posted earlier:

awk -vRS=");" '
{$0=gensub(/PNG_EXPORT\(([^,]*),([^)]*).*PNGARG\((.*)/,"\\1
\\2\\3;","")}$1=$1' input_file

to look skip the record if the third field (the function name) has
already been seen:

awk -vRS=");" 'f[$3]++{next}
{$0=gensub(/PNG_EXPORT\(([^,]*),([^)]*).*PNGARG\((.*)/,"\\1
\\2\\3;","")}$1=$1' input_file

As mentioned earlier, the above uses gawk to take advantage of gensub()
plus multi-char RSs.

Regards,

Ed.

one2001boy@yahoo.com

2005-03-29, 2:49 am

Ed Morton wrote:
>
>
> one2001boy@yahoo.com wrote:
>
>
> This falls into the "I don't even want to think about it" category
> because it's trivial in awk. Just tweak this script I posted earlier:
>
> awk -vRS=");" '
> {$0=gensub(/PNG_EXPORT\(([^,]*),([^)]*).*PNGARG\((.*)/,"\\1
> \\2\\3;","")}$1=$1' input_file
>
> to look skip the record if the third field (the function name) has
> already been seen:
>
> awk -vRS=");" 'f[$3]++{next}
> {$0=gensub(/PNG_EXPORT\(([^,]*),([^)]*).*PNGARG\((.*)/,"\\1
> \\2\\3;","")}$1=$1' input_file
>
> As mentioned earlier, the above uses gawk to take advantage of gensub()
> plus multi-char RSs.


gawk is nice in handling it. However, gawk might not be portable in
solaris by default. my program requires portability.

Thanks.

>
> Regards,
>
> Ed.
>

Ed Morton

2005-03-29, 6:21 pm



one2001boy@yahoo.com wrote:
> Ed Morton wrote:

<snip>
>
>
> gawk is nice in handling it. However, gawk might not be portable in
> solaris by default. my program requires portability.


Personally, I'd just install gawk wherever I need it since it's free and
does run on solaris and solves all your problems BUT you could do
something like this instead if you feel energetic:

whole=""
sep=""
while read line
do
whole="${whole}${sep}${line}"
sep=" "
case $whole in
*\; ) whole=`echo "$whole" | sed 's/,/, /g'`
set -- $whole
case "$seen" in
*" $3 "*) ;;
* )
echo "$whole" |
sed 's/PNG_EXPORT(\([^,]*\),\([^)]*\)).*PNGARG(\([^)]*\))/\1 \2\3/'
seen="$seen $3 "
;;
esac
whole=""
sep=""
;;
esac
done < input_file

Note that the above shell script concatenates multi-line records onto a
single line before sed gets called, so the sed script no longer needs
the loop, etc. The above is untested so it may need to be tweaked....

Regards,

Ed.
Alan Connor

2005-03-29, 6:21 pm

On comp.unix.shell, in <d2c5bs$i4b@netnews.proxy.lucent.com>, "Ed
Morton" wrote:

<snip>

>
> Personally, I'd just install gawk wherever I need it since it's
> free and does run on solaris and solves all your problems BUT
> you could do something like this instead if you feel energetic:
>



> whole=""
> sep=""
> while read line
> do
> whole="${whole}${sep}${line}"
> sep=" "
> case $whole in
> *\; ) whole=`echo "$whole" | sed 's/,/, /g'`
> set -- $whole
> case "$seen" in
> *" $3 "*) ;;
> * )
> echo "$whole" |
> sed 's/PNG_EXPORT(\([^,]*\),\([^)]*\)).*PNGARG(\([^)]*\))/\1 \2\3/'
> seen="$seen $3 "
> ;;
> esac
> whole=""
> sep=""
> ;;
> esac
> done < input_file
>



> Note that the above shell script concatenates multi-line
> records onto a single line before sed gets called, so the sed
> script no longer needs the loop, etc. The above is untested so
> it may need to be tweaked....
>


You could also do the concatenating with sed (general concept):

sed '/regex/{N;s/\n//;}' | sed ....

> Regards,
>
> Ed.



Excellent! Thanks for demonstrating my earlier point:

sed + shell = awk

And you could do things with sed + shell that would be quite
impossible for awk.

AC

--
I ignore posters who use common first names for aliases.
Pick a unique alias and stick with it. Check the alias at:
http://groups.google.com/advanced_group_search
Do not include the email address in the alias.
one2001boy@yahoo.com

2005-03-30, 2:50 am

Ed Morton wrote:
>
>
> one2001boy@yahoo.com wrote:
> <snip>
>
>
>
> Note - you assigned "sedstring" but referenced "sestring" (missing a
> "d") so this is the same as
>
> sed -e "" t.h >ttt


yes, it is my fault with the simple typo problem.
Thanks so much for your help. it works with the scritp file above.

There is one more issue that I am not sure sed do.

With the above sed script file,

extern PNG_EXPORT(int, png_check_sig) PNGARG((png_bytep sig, int num));
extern PNG_EXPORT(int, png_check_sig) PNGARG((png_bytep sig, int
num,char c));

will become:

extern int png_check_sig(png_bytep sig, int num);
extern int png_check_sig(png_bytep sig, int num, char c);

For those functions with the same function name png_check_sig(),
I only need to keep the first function.
the expected output is:
extern int png_check_sig(png_bytep sig, int num);

Is there a way for me to get rid of second function with sed?

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com