[Solved] Need to auto increment

Hello, this is a VI question more than anything...

I'm using: SunOS 5.10 Generic_150400-04 sun4v sparc SUNW,T5240

I'm trying to find a problematic line(s) in a script.
The only solution I can think of is to tag each repetative line, and increment it.

This is an oracle insert script.

this line, "prompt inserting X" will echo "inserting X" to the STDOUT while the script executes.

So I have 20 different files that have inserts like the following:

insert into x (bla, bla, bla) values (1,2,3);

Each of those files has anywhere from 1500 to 110K+ repetitions of that line.
How can I run an automated script that edits the file and changes

"insert into xxxx"

into

"prompt inserting 1 -- for each iteration of this line, it needs to increment the number.
insert into xxxx"

That way when the file executes, I will see the incremented line number before the error it reports.

Thanks!

---------- Post updated at 10:18 AM ---------- Previous update was at 09:19 AM ----------

anyone?

What have you tried so far?
Not sure what you want in fact...
if insert into x (bla, bla, bla) values (1,2,3); if this is a valid sample, and you want to replace the red portion by "prompt inserting 1" then I dont understand what follows: - for each iteration of this line, it needs to increment the number.
insert into xxxx"

Not sure if I understood your requirement clearly. So can you post a representative sample input and expected output in code tags?

Thanks for the response. Thought I gave a better explanation.
The bla bla bla is irrelevant, as it will change with every instance.

What I'm wanting to do, which I know how to do inside of VI for the most part, is to add an incrementing value to a replacement...

so inside vi this would give me the following...
(existing syntax)
insert into x (bla, bla, bla) values (1,2,3); (this is oracle code inside a unix file.)

Making the change...
:%s/insert/prompt inserting\n/g

would result in: (actually it doesn't...it's not recognizing the \n as a new line)
prompt inserting
insert into x (bla, bla, bla) values (1,2,3);

What I need to do, which I don't know how to, is to ad an incrementing number to the end of that "prompt inserting" line, so it would increment each time it's replaced.

example...

prompt inserting 1
insert into x (bla, bla, bla) values (1,2,3);

prompt inserting 2
insert into x (bla, bla, bla) values (1,2,3);

prompt inserting 3
insert into x (bla, bla, bla) values (1,2,3);

Using awk:

awk '/insert/{print "prompt inserting " ++c; print $0}' file

more clear now... In fact you want to add a line before with a counter...

close Yoda...but not exactly.
That gave:
0
insert into x (bla, bla, bla) values (1,2,3);
1
insert into x (bla, bla, bla) values (1,2,3);
2
insert into x (bla, bla, bla) values (1,2,3);
3
insert into x (bla, bla, bla) values (1,2,3);
4
insert into x (bla, bla, bla) values (1,2,3);
5
insert into x (bla, bla, bla) values (1,2,3);

This is what I got:

$ cat file
insert into x (bla, bla, bla) values (1,2,3);
insert into x (bla, bla, bla) values (1,2,3);
insert into x (bla, bla, bla) values (1,2,3);
$ awk '/insert/{print "prompt inserting " ++c; print $0}' file
prompt inserting 1
insert into x (bla, bla, bla) values (1,2,3);
prompt inserting 2
insert into x (bla, bla, bla) values (1,2,3);
prompt inserting 3
insert into x (bla, bla, bla) values (1,2,3);

That is another way of putting it, yes.

---------- Post updated at 11:49 AM ---------- Previous update was at 11:09 AM ----------

What shell are you using?

It's probably the SunOS awk - try nawk (or /usr/xpg4/bin/awk) instead.

Yes, try nawk instead:

nawk '/insert/{print "prompt inserting " ++c; print $0}' file

OR

awk '/insert/{print "prompt inserting", ++c; print $0}' file

looks like the nawk did the trick.

THANKS!

---------- Post updated at 03:35 PM ---------- Previous update was at 01:12 PM ----------

For some reason this isn't working correctly.

I'm calling it like so... for ease of space, I will just call the file names fileA and fileB

I've also tried >> instead of >

What I'm seeing is:

That's it..that's the full content.

What I should be seeing 111372 versions of:

What am I doing wrong?

Try:

nawk '/Insert/{print "prompt Inserting " ++c}1' fileA > fileB
1 Like

That did it. I wonder why it worked one way, one minute and then not the next....

Thanks again!

Original solution appeared to work as the test data posted here all involved single line insert into statements so it's more a matter of incomplete test data rather than the program working and then suddenly not working.