help on "sed" back reference

help on "sed" back reference

Post by Prince Kum » Thu, 19 Aug 2004 04:24:16



CREATE TABLE "table_name_x" (
 ..
 ..
 )

 CREATE TABLE "table_name_y" (
 ..
 ..
 )

I have a file with the contents similar to the above one.

I want to add a line "DROP TABLE "table_name_?" before each of the
create table. When I try sed '/CREATE TABLE \(.*\)  ($/i\^J DROP TABLE
\1' my_file.sql

 DROP TABLE 1

 CREATE TABLE "table_name_x" (
 ..
 ..
 )

 DROP TABLE 1

 CREATE TABLE "table_name_y" (
 ..
 ..
 )

I get the literal 1 instead of the pattern. How would I fix this?

 
 
 

help on "sed" back reference

Post by Ed Morto » Thu, 19 Aug 2004 05:16:27



> CREATE TABLE "table_name_x" (
>  ..
>  ..
>  )

>  CREATE TABLE "table_name_y" (
>  ..
>  ..
>  )

> I have a file with the contents similar to the above one.

> I want to add a line "DROP TABLE "table_name_?" before each of the
> create table. When I try sed '/CREATE TABLE \(.*\)  ($/i\^J DROP TABLE
> \1' my_file.sql
<snip>
> I get the literal 1 instead of the pattern. How would I fix this?

No idea. I'd just use awk:

        awk '/CREATE TABLE/{print "DROP TABLE " $3}{print}' file

Regards,

        Ed.

 
 
 

help on "sed" back reference

Post by rakesh shar » Thu, 19 Aug 2004 15:14:35



> CREATE TABLE "table_name_x" (
>  ..
>  ..
>  )

>  CREATE TABLE "table_name_y" (
>  ..
>  ..
>  )

> I have a file with the contents similar to the above one.

> I want to add a line "DROP TABLE "table_name_?" before each of the
> create table. When I try sed '/CREATE TABLE \(.*\)  ($/i\^J DROP TABLE
> \1' my_file.sql

>  DROP TABLE 1

>  CREATE TABLE "table_name_x" (
>  ..
>  ..
>  )

>  DROP TABLE 1

>  CREATE TABLE "table_name_y" (
>  ..
>  ..
>  )

> I get the literal 1 instead of the pattern. How would I fix this?

the i\ command in 'sed' doesn't understand backreferences, all it does
is insert literally what follows.

use the s/// command for that or use the hold area for manipulations:

# based on hold manips.
sed -e '
/^CREATE TABLE "table_name_/{
h
s/CREATE/DROP/;s/(//
G

Quote:}

' yourfile

# based on the s/// command only
sed -e '
s/^CREATE TABLE "\([^"]*\)".*/DROP TABLE "\1"\
&/
' yourfile

 
 
 

help on "sed" back reference

Post by Walter Brisco » Thu, 19 Aug 2004 16:19:15





>> CREATE TABLE "table_name_x" (
>>  ..
>>  ..
>>  )
>>   CREATE TABLE "table_name_y" (
>>  ..
>>  ..
>>  )
>>   I have a file with the contents similar to the above one.   I want
>>to add a line "DROP TABLE "table_name_?" before each of the
>> create table. When I try sed '/CREATE TABLE \(.*\)  ($/i\^J DROP TABLE
>> \1' my_file.sql
><snip>
>> I get the literal 1 instead of the pattern. How would I fix this?

>No idea. I'd just use awk:

>       awk '/CREATE TABLE/{print "DROP TABLE " $3}{print}' file

I find solutions which avoid problem constraints an irritation.
The OP used \1 in an i command. In sed, \n is only a back-references in
a BRE or the replacement of an s command. The OP should try a command
like:
/^ *CREATE TABLE \("[^"][^"]*"\)/s//DROP TABLE \1\
&/

or
/^ *CREATE TABLE \(.*\) *($/s//DROP TABLE \1\
&/
or
s/^ *CREATE TABLE \(.*\) *($/DROP TABLE \1\
&/

\n meaning <newline> in the replacement is a non-standard extension. (I
think it slightly inconsistent, given that \n means <newline> in a
standard sed BRE.) I can do
s/^ *CREATE TABLE \(.*\) *($/DROP TABLE \1\n&/

The OP will probably want to do
sed 's/^ *CREATE TABLE \(.*\) *($/DROP TABLE \1\^J&/' my_file.sql
--
Walter Briscoe

 
 
 

help on "sed" back reference

Post by Ed Morto » Thu, 19 Aug 2004 21:36:53






>>>CREATE TABLE "table_name_x" (
>>> ..
>>> ..
>>> )
>>>  CREATE TABLE "table_name_y" (
>>> ..
>>> ..
>>> )
>>>  I have a file with the contents similar to the above one.   I want
>>>to add a line "DROP TABLE "table_name_?" before each of the
>>>create table. When I try sed '/CREATE TABLE \(.*\)  ($/i\^J DROP TABLE
>>>\1' my_file.sql

>><snip>

>>>I get the literal 1 instead of the pattern. How would I fix this?

>>No idea. I'd just use awk:

>>      awk '/CREATE TABLE/{print "DROP TABLE " $3}{print}' file

> I find solutions which avoid problem constraints an irritation.

There's 2 types of solution which avoid problem constraints:

1) Those that provide the expected output given the advertised input
2) Those that don't

Of course avoiding problem constraints the result in "2", such as
ignoring input formatting specifications, are undesirable, but "1" seems
  perfectly reasonable given the variety of possible solutions to many
problems.

You can't expect everyone who posts a question here to know about every
tool and we usually ask for people to post their attempted solutions. If
they've attempted a solution that uses one tool, should we condemn them
  to only solutions that use that tool? If the OP had asked "how do I
print the last 10 lines of a file using sed", I'd be disappointed if
someone didn't refer them to "tail".

If I'm posting a question asking how to do XYZ using the only tool I
know of that could manage the task, I'd want to hear about it if there's
an alternative tool that can do the same job in a simpler way.

<snip>

Quote:> The OP will probably want to do
> sed 's/^ *CREATE TABLE \(.*\) *($/DROP TABLE \1\^J&/' my_file.sql

Obviously ;-).

        Ed.

 
 
 

help on "sed" back reference

Post by Prince Kum » Fri, 20 Aug 2004 06:02:41


Thank you for all your responses.

I used walters approach.





> >> CREATE TABLE "table_name_x" (
> >>  ..
> >>  ..
> >>  )
> >>   CREATE TABLE "table_name_y" (
> >>  ..
> >>  ..
> >>  )
> >>   I have a file with the contents similar to the above one.   I want
> >>to add a line "DROP TABLE "table_name_?" before each of the
> >> create table. When I try sed '/CREATE TABLE \(.*\)  ($/i\^J DROP TABLE
> >> \1' my_file.sql
>  <snip>
> >> I get the literal 1 instead of the pattern. How would I fix this?

> >No idea. I'd just use awk:

> >       awk '/CREATE TABLE/{print "DROP TABLE " $3}{print}' file
> I find solutions which avoid problem constraints an irritation.
> The OP used \1 in an i command. In sed, \n is only a back-references in
> a BRE or the replacement of an s command. The OP should try a command
> like:
> /^ *CREATE TABLE \("[^"][^"]*"\)/s//DROP TABLE \1\
> &/

> or
> /^ *CREATE TABLE \(.*\) *($/s//DROP TABLE \1\
> &/
> or
> s/^ *CREATE TABLE \(.*\) *($/DROP TABLE \1\
> &/

> \n meaning <newline> in the replacement is a non-standard extension. (I
> think it slightly inconsistent, given that \n means <newline> in a
> standard sed BRE.) I can do
> s/^ *CREATE TABLE \(.*\) *($/DROP TABLE \1\n&/

> The OP will probably want to do
> sed 's/^ *CREATE TABLE \(.*\) *($/DROP TABLE \1\^J&/' my_file.sql

 
 
 

help on "sed" back reference

Post by Walter Brisco » Fri, 20 Aug 2004 17:15:33









[snip]

Quote:>>>>  I have a file with the contents similar to the above one.   I want
>>>>to add a line "DROP TABLE "table_name_?" before each of the
>>>>create table. When I try sed '/CREATE TABLE \(.*\)  ($/i\^J DROP TABLE
>>>>\1' my_file.sql

>>><snip>

>>>>I get the literal 1 instead of the pattern. How would I fix this?

>>>No idea. I'd just use awk:

>>>      awk '/CREATE TABLE/{print "DROP TABLE " $3}{print}' file
>>  I find solutions which avoid problem constraints an irritation.

>There's 2 types of solution which avoid problem constraints:

>1) Those that provide the expected output given the advertised input
>2) Those that don't

>Of course avoiding problem constraints the result in "2", such as
>ignoring input formatting specifications, are undesirable, but "1"
>seems  perfectly reasonable given the variety of possible solutions to
>many problems.

>You can't expect everyone who posts a question here to know about every
>tool and we usually ask for people to post their attempted solutions.
>If they've attempted a solution that uses one tool, should we condemn
>them  to only solutions that use that tool? If the OP had asked "how do
>I print the last 10 lines of a file using sed", I'd be disappointed if
>someone didn't refer them to "tail".

>If I'm posting a question asking how to do XYZ using the only tool I
>know of that could manage the task, I'd want to hear about it if
>there's an alternative tool that can do the same job in a simpler way.

><snip>
>> The OP will probably want to do
>> sed 's/^ *CREATE TABLE \(.*\) *($/DROP TABLE \1\^J&/' my_file.sql

>Obviously ;-).

We both agree and disagree. "No idea" suggests the author writes from
ignorance. I prefer: "Here is what you requested but this may better
serve". Perl proponents often post "You are better with this". I find
that impertinent. As it happens, I think your awk solution is better
than any sed solution. I use sed but would advocate awk if I knew it
well enough.
--
Walter Briscoe