[Solved] Need to auto increment

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Need to auto increment
# 1  
Old 01-15-2014
[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"


"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.


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


Last edited by graphi; 01-15-2014 at 12:59 PM..
# 2  
Old 01-15-2014
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"
# 3  
Old 01-15-2014
Not sure if I understood your requirement clearly. So can you post a representative sample input and expected output in code tags?
# 4  
Old 01-15-2014
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.


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);
# 5  
Old 01-15-2014
Using awk:
awk '/insert/{print "prompt inserting " ++c; print $0}' file

# 6  
Old 01-15-2014
more clear now... In fact you want to add a line before with a counter...
# 7  
Old 01-15-2014
close Yoda...but not exactly.
That gave:
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);
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);
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Howto auto boot SPARC | How to auto supply "start /SYS" and "start /SP/console" commands

When I power ON my T4-1, I got a prompt -> where I have to start /SYS and start /SP/console. How can I auto supply these two commands ? (3 Replies)
Discussion started by: z_haseeb
3 Replies

2. Shell Programming and Scripting

Increment the password value

I want a script which increments the count when the script runs. Basically I want to send an password reset email notification for an application, the password value should be keep on changing whenever the script is executed for example, first time i execute it should be password1, second time... (2 Replies)
Discussion started by: JAGADESH GN
2 Replies

3. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

4. UNIX for Dummies Questions & Answers

[Solved] auto sftp

i was able to script: ftp -n hosname << DONE user username pwd put quite close DONE can i do same as sftp ? i tried, but it keeps asking for pwd. when i typed pwd, and it worked fine. (11 Replies)
Discussion started by: lawsongeek
11 Replies

5. Shell Programming and Scripting

[solved] merging two files and writing to another file- solved

i have two files as file1: 1 2 3 file2: a b c and the output should be: file3: 1~a 2~b 3~c (1 Reply)
Discussion started by: mlpathir
1 Replies

6. Shell Programming and Scripting

Increment in date

Hi, I have a variable lets say DATA_DATE. I have to pass some value to this variable in YYYYMMDD format. lets say today I have passed this variable as : DATA_DATE=20100107 Then pls help me how to calculate another variable DATA_DATE1 (which is DATA_DATE+1). The code should work... (3 Replies)
Discussion started by: 46019
3 Replies

7. Shell Programming and Scripting

auto increment

Hello Does anyone know how to auto-increment the value of a variable, preferably using awk or sed? I need to read values from a file and auto-increment those values to use them as line numbers I'd be doing: while read line do # auto-increment sed -n${line}p file> file1 done... (6 Replies)
Discussion started by: loperam
6 Replies

8. Shell Programming and Scripting

Increment value (starttime)

Hi All, I have created a script... #!/bin/sh datafile=ABC2008101601.OUT indfile=ABCIND20081016.1.OUT waittime=600 starttime=0 while do if then echo "Indicator file has arrived." break else sleep 10; ((starttime=$starttime+10)) echo $starttime (2 Replies)
Discussion started by: Amit.Sagpariya
2 Replies

9. Shell Programming and Scripting

increment an integer

hi I want to echo the variable $i while it auto-increments till 21 I set initially i to 1 any idea how to do that? thank you (4 Replies)
Discussion started by: melanie_pfefer
4 Replies
Login or Register to Ask a Question