insert "comment" characters in a file

insert "comment" characters in a file

am 06.12.2007 18:56:28 von AFC

I have two files...master_file.sql & error_statements.sql. Both are
large files...master_file.sql has about 120,000 lines and
error_statements.sql has about 16,000 lines. All line in
error_statements.sql are in master_file.sql file.

what I need to do is...I have to read first line in
error_statements.sql & find that line in master_file.sql & insert "--
" in front of that line in master_file.sql. Then, do the same thing
for all subsequent lines in error_statements.sql. A line
error_statements.sql occurs only once in master_file.sql.

I have no clue how I can do this...please help me out.

Thanks,
AFC

Re: insert "comment" characters in a file

am 06.12.2007 19:12:32 von Janis Papanagnou

AFC wrote:
> I have two files...master_file.sql & error_statements.sql. Both are
> large files...master_file.sql has about 120,000 lines and
> error_statements.sql has about 16,000 lines. All line in
> error_statements.sql are in master_file.sql file.
>
> what I need to do is...I have to read first line in
> error_statements.sql & find that line in master_file.sql & insert "--
> " in front of that line in master_file.sql. Then, do the same thing
> for all subsequent lines in error_statements.sql. A line
> error_statements.sql occurs only once in master_file.sql.
>
> I have no clue how I can do this...please help me out.

This may do what you want (untested)...

awk '
NR==FNR { e[$0] ; next } $0 in e { printf("-- ") } 1
' error_statements.sql master_file.sql > new_master_file.sql


Janis

>
> Thanks,
> AFC

Re: insert "comment" characters in a file

am 06.12.2007 19:33:02 von Steffen Schuler

On Thu, 06 Dec 2007 09:56:28 -0800, AFC wrote:

> I have two files...master_file.sql & error_statements.sql. Both are
> large files...master_file.sql has about 120,000 lines and
> error_statements.sql has about 16,000 lines. All line in
> error_statements.sql are in master_file.sql file.
>
> what I need to do is...I have to read first line in error_statements.sql
> & find that line in master_file.sql & insert "-- " in front of that line
> in master_file.sql. Then, do the same thing for all subsequent lines in
> error_statements.sql. A line error_statements.sql occurs only once in
> master_file.sql.
>
> I have no clue how I can do this...please help me out.
>
> Thanks,
> AFC

A tested solution:

$ cat ins-comment.awk
BEGIN { while ((getline s < error_file) > 0) lerr[s] = 1 }
$0 in lerr { $0 = "-- " $0}
1
$ awk -v error_file=error_statements.sql -f ins-comment.awk
master_file.sql

Regards,

Steffen "goedel" Schuler

Re: insert "comment" characters in a file

am 06.12.2007 22:54:26 von AFC

On Dec 6, 1:33 pm, Steffen Schuler
wrote:
> On Thu, 06 Dec 2007 09:56:28 -0800, AFC wrote:
> > I have two files...master_file.sql & error_statements.sql. Both are
> > large files...master_file.sql has about 120,000 lines and
> > error_statements.sql has about 16,000 lines. All line in
> > error_statements.sql are in master_file.sql file.
>
> > what I need to do is...I have to read first line in error_statements.sql
> > & find that line in master_file.sql & insert "-- " in front of that line
> > in master_file.sql. Then, do the same thing for all subsequent lines in
> > error_statements.sql. A line error_statements.sql occurs only once in
> > master_file.sql.
>
> > I have no clue how I can do this...please help me out.
>
> > Thanks,
> > AFC
>
> A tested solution:
>
> $ cat ins-comment.awk
> BEGIN { while ((getline s < error_file) > 0) lerr[s] = 1 }
> $0 in lerr { $0 = "-- " $0}
> 1
> $ awk -v error_file=error_statements.sql -f ins-comment.awk
> master_file.sql
>
> Regards,
>
> Steffen "goedel" Schuler- Hide quoted text -
>
> - Show quoted text -

Thanks for your help...
awk wasn't working, I had to use nawk:
nawk 'BEGIN { while ((getline s < "error_statements.sql") > 0) lerr[s]
= 1 } $0 in lerr { $0 = "-- " $0} 1' master_file.sql >
new_master_file.sql

Re: insert "comment" characters in a file

am 07.12.2007 19:31:52 von Icarus Sparry

On Thu, 06 Dec 2007 13:54:26 -0800, AFC wrote:

> On Dec 6, 1:33 pm, Steffen Schuler
> wrote:
>> On Thu, 06 Dec 2007 09:56:28 -0800, AFC wrote:
>> > I have two files...master_file.sql & error_statements.sql. Both are
>> > large files...master_file.sql has about 120,000 lines and
>> > error_statements.sql has about 16,000 lines. All line in
>> > error_statements.sql are in master_file.sql file.
>>
>> > what I need to do is...I have to read first line in
>> > error_statements.sql & find that line in master_file.sql & insert "--
>> > " in front of that line in master_file.sql. Then, do the same thing
>> > for all subsequent lines in error_statements.sql. A line
>> > error_statements.sql occurs only once in master_file.sql.
>>
>> > I have no clue how I can do this...please help me out.
>>
>> > Thanks,
>> > AFC
>>
>> A tested solution:
>>
>> $ cat ins-comment.awk
>> BEGIN { while ((getline s < error_file) > 0) lerr[s] = 1 } $0 in
>> lerr { $0 = "-- " $0}
>> 1
>> $ awk -v error_file=error_statements.sql -f ins-comment.awk
>> master_file.sql
>>
>> Regards,
>>
>> Steffen "goedel" Schuler- Hide quoted text -
>>
>> - Show quoted text -
>
> Thanks for your help...
> awk wasn't working, I had to use nawk: nawk 'BEGIN { while ((getline s <
> "error_statements.sql") > 0) lerr[s] = 1 } $0 in lerr { $0 = "-- " $0}
> 1' master_file.sql > new_master_file.sql

You have your solution now. Another approach would be to convert the
error_statement.sql into a sed script, and then use that to edit the
master file.

(untested)
sed -e 's/[]*\\\/]/\\&/g' -e 's:.*:/&/s/^/-- :' error_statements.sql > t
sed -f t master_file.sql > new_master_file.sql