Dynamic number to replace with line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dynamic number to replace with line
# 1  
Old 07-30-2013
Dynamic number to replace with line

Hi,

I have file as below

Code:
COMMIT
# at 34572

# at 23432

COMMIT
# at 5674

# at 7856

I want to replace with as below


Code:
COMMIT
insert into test.counter(id) values(34572);

# at 23432

COMMIT
insert into test.counter(id) values(5674);

# at 7856

Only the numbers are below commit should change
# 2  
Old 07-30-2013
Code:
awk '/COMMIT/ {print $0;getline;print "insert into test.counter(id) values("$3")\n";getline;getline;print $0"\n"}' file

Code:
COMMIT
insert into test.counter(id) values(34572)

# at 23432

COMMIT
insert into test.counter(id) values(5674)

# at 7856

Edit: some other suggestion
Code:
awk 'a {$0="insert into test.counter(id) values("$3")";a=0} /COMMIT/ {a=1} 1' file


Last edited by Jotne; 07-30-2013 at 03:44 AM..
# 3  
Old 07-30-2013
Thanks for reply

This command works in file but the remaining data does not show between "# at"
That I had not mentioned

I tried below

Code:
grep "# at" dynamic | sed s/'# at '/'insert into test.dummy(id) values ();'/
insert into test.dummy(id) values ();2345
insert into test.dummy(id) values ();2346
insert into test.dummy(id) values ();2478
insert into test.dummy(id) values ();2489

I want like below

Code:
insert into test.dummy(id) values (2345);
insert into test.dummy(id) values (2346);
insert into test.dummy(id) values (2478);
insert into test.dummy(id) values (2489);


Last edited by kaushik02018; 07-30-2013 at 05:45 AM..
# 4  
Old 07-30-2013
try this perl script

Code:
#!/usr/bin/perl

# Your input filename here.
my $lines = `cat filename`;
$lines =~ s/COMMIT(.)# at (\d+)/COMMIT\1insert into test\.counter(id) values(\2);/sg;
print $lines;

# 5  
Old 07-30-2013
Some like this?
Code:
awk '/# at/ {print "insert into test.counter(id) values("$3");"}' dynamic
insert into test.counter(id) values(34572);
insert into test.counter(id) values(23432);
insert into test.counter(id) values(5674);
insert into test.counter(id) values(7856);

# 6  
Old 07-30-2013
Quote:
Originally Posted by Jotne
Some like this?
Code:
awk '/# at/ {print "insert into test.counter(id) values("$3");"}' dynamic
insert into test.counter(id) values(34572);
insert into test.counter(id) values(23432);
insert into test.counter(id) values(5674);
insert into test.counter(id) values(7856);

Jotne Its works But My Concern is

Original file output
Code:
COMMIT
# at 2345

# at 2346
COMMIT
# at 2478
dummy
# at 2489

and by your command

Code:
$ awk '/# at/ {print "insert into test.counter(id) values("$3");"}' dynamic
insert into test.counter(id) values(2345);
insert into test.counter(id) values(2346);
insert into test.counter(id) values(2478);
insert into test.counter(id) values(2489);

I want to replace # at 2489 to insert into test.counter(id) values(2489);

But in this case rest thing miss out as dummy , COMMIT

I want rest thing remain as it is
# 7  
Old 07-30-2013
I just using your test example.
Please post a much large input file and how you like to output to be, and how to get there. Its difficult to guess.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a number in the last line of a delimited file.

Hi all, I am fairly new to UNIX and I was wondering if you could provide me with some help! Lets say i have a file as below : Line 1 Line 2 Line 3 ABC|12|4|2 Now the number 4 in bold, this number will represent the number of row there is in the file excluding the header and footer... (10 Replies)
Discussion started by: Stinza
10 Replies

2. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

Replace a field with line number in file

I am working on a script to convert bank data to a csv file. I have the format done - columns etc. The final piece of the puzzle is to change the second field (after the R) of every line to reflect its' line number in the file. I am stumped. I can use awk on each line but need help looping through... (9 Replies)
Discussion started by: Melah Gindi
9 Replies

4. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

5. Shell Programming and Scripting

Replace first number of each line in a file with another number

Hi, I have a set of files in a directory that I have to read and replace the first occurrence of a number with another dummy number. This is what I have so far but it does not seem to work. The files have lot of other data in each row and each data element is separated by ,@, for file in... (13 Replies)
Discussion started by: scorpioraghu
13 Replies

6. Shell Programming and Scripting

replace line starting with not a number

Dear users, I have a file like this: geometry,geometry_vertex_count,Id,strnum,platecode,datatype,dtnum,refnum,appearance,disappeara,color,geogdesc,datatype_ft_style,import_notes "<LineString><coordinates>-130.6539,51.5103,0 -130.7708,51.6287,0 -130.8356,51.6832,0 -130.9211,51.7772,0... (5 Replies)
Discussion started by: Gery
5 Replies

7. Shell Programming and Scripting

replace blank line number

hlow all i need help how can i replace blank number with awk input.txt 300::|355264313178490 301::|358814003239510 302::|358316038113400 303::|357954002633660 304::|354072040694090 305::|356956015214190 306::|352943020525180 307::|359574033836610 308::|381810990023580 so will be like... (4 Replies)
Discussion started by: zvtral
4 Replies

8. Shell Programming and Scripting

Replace 2nd column for each line in a csv file with fixed string+random number

Hi experts, My csv file looks like this U;cake;michael;temp;;;; U;bread;john;temp;;;; U;cocktails;sarah;temp;;;; I'd like to change the value fo 2nd column to cf+random number , which will look maybe something like this U;cf20187;michael;temp;;;; U;cf8926;john;temp;;;;... (7 Replies)
Discussion started by: tententen
7 Replies

9. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

10. UNIX for Dummies Questions & Answers

Replace line number 2 with another line

Hi, How can i replace second Line of a file with another line. The whole 2nd line should be replaced with new line. Please let me know the solution. Thanks, Sri (1 Reply)
Discussion started by: srilaxmi
1 Replies
Login or Register to Ask a Question