[Solved] Need to auto increment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Need to auto increment
# 8  
Old 01-15-2014
This is what I got:
Code:
$ 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);

Code:
$ 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);

# 9  
Old 01-15-2014
Quote:
Originally Posted by vbe
more clear now... In fact you want to add a line before with a counter...

That is another way of putting it, yes.

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

Quote:
Originally Posted by Yoda
This is what I got:
Code:
$ 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);

Code:
$ 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);

What shell are you using?
# 10  
Old 01-15-2014
It's probably the SunOS awk - try nawk (or /usr/xpg4/bin/awk) instead.
# 11  
Old 01-15-2014
Yes, try nawk instead:
Code:
nawk '/insert/{print "prompt inserting " ++c; print $0}' file

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

# 12  
Old 01-15-2014
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

Quote:
nawk '/Insert/{print "prompt Inserting " ++c; print $0}' fileA > fileB
I've also tried >> instead of >

What I'm seeing is:
Quote:
prompt Inserting 1
Insert into FS_NRIS_NVUM.NRN_STRATA
prompt Inserting 2
Insert into FS_NRIS_NVUM.NRN_STRATA
That's it..that's the full content.

What I should be seeing 111372 versions of:
Quote:
prompt Inserting 1
Insert into FS_NRIS_NVUM.NRN_STRATA
(STRATA_CN, SITE_CN_FK, STARTDATE, ENDDATE, MON,
TUES, WED, THUR, FRI, SAT,
SUN, USERNAME, C, N, L,
M, H, V, FINAL, LAST_UPDATE,
VPDUNIT_ID)
Values
('D46067E0975848E8E040DDA54F2A76BE', 'D46067E0975648E8E040DDA54F2A76BE', TO_DATE('10/01/2013 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('10/1
2/2013 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'L',
'L', 'L', 'L', 'L', 'L',
'L', 'TFRASER', 0, 0, 12,
0, 0, 0, 'Y', TO_TIMESTAMP_TZ('2/7/2013 4:23:26.000000 AM -06:00','fmMMfm/fmDDfm/YYYY fmHH12fm:MI:SS.FF AM TZH:TZM'),
'0407');
What am I doing wrong?
# 13  
Old 01-15-2014
Try:

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

This User Gave Thanks to Chubler_XL For This Post:
# 14  
Old 01-15-2014
Quote:
Originally Posted by Chubler_XL
Try:

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


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


Thanks again!
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