1754942 Members
3125 Online
108827 Solutions
New Discussion юеВ

Another Inquiry

 
SOLVED
Go to solution
Pando
Regular Advisor

Another Inquiry

Dear Gurus,

Another inquiry on file manipulation....
I have a file (more than 10000+) and i need to insert the following line with it.

Program Name,XXXXX
Program Version,YYYYYY
Stage,1

My file currently contains the following info:

...
Product Name, XXX/YY
Operator,1234
...

I need to insert between the Product Name and Operator the following:

Program Name,XXXXX
Program Version,YYYYYY
Stage,1


Such that my file will look like this:

...
Product Name, XXX/YY
Program Name,XXXXX
Program Version,YYYYYY
Stage,1
Operator,1234
...

Maximum point to all correct replies.
Thanks!
16 REPLIES 16
Dennis Handly
Acclaimed Contributor

Re: Another Inquiry

You need to explain better where you get the inserted 3 lines. Are the 3 lines fixed? Or are they composed of parts of lines from the original file.

Product Name, $X/$Y
Program Name,$X
Program Version,$Y
Stage,1

Do you need to copy the above $X and $Y strings to the inserted lines?
Pando
Regular Advisor

Re: Another Inquiry


Hi Dennis,

The 3 lines are fixed at the moment. or I was thinking of putting it in a file (3 lines) and merge it with the file provided that my file content will still look like this.

...
...
Product Name, XXX/YY
Program Name,XXXXX
Program Version,YYYYYY
Stage,1
Operator,1234
...
...

On your inquiry.

Do you need to copy the above $X and $Y strings to the inserted lines? Yes.
Peter Nikitka
Honored Contributor

Re: Another Inquiry

Hi,

it's stiil not clear for me:
This result:
1)
Product Name, XXX/YY
Program Name,XXX
Program Version,YY
Stage,1
Operator,1234

or that:
2)
Product Name, XXX/YY
Program Name,ZZZZZ
Program Version,TTTTTT
Stage,1
Operator,1234

A solution will severely depend on that.

mfg Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Pando
Regular Advisor

Re: Another Inquiry

Hi All,

The output should be:

Product Name,XXX/YY
Program Name,ZZZZZ
Program Version,TTTTTT
Stage,1
Operator,1234
James R. Ferguson
Acclaimed Contributor

Re: Another Inquiry

Hi:

If I understand correctly, this should do what you ask:

# cat inserter
#!/usr/bin/perl
use strict;
use warnings;
while (<>) {
print;
if (m/^Product Name/) {
print "Program Name,ZZZZZ\n";
print "Program Version,TTTTTT\n";
print "Stage,1\n";
}
}

...run as:

# ./inserter file

Regards!

...JRF...
Sandman!
Honored Contributor

Re: Another Inquiry

imho here's a solution if my understanding of your requirement is correct i.e. f2 is the file that contains the 3 lines and f1 is the file (10000+ lines) into which those 3 lines have to be inserted

# cat f1
...
Product Name, XXX/YY
Operator,1234
...

# cat f2
Program Name,XXXXX
Program Version,YYYYYY
Stage,1

use this ex construct...

# ex -s +'/^Product Name, XXX\/YY$/r f2 | wq' f1
Pando
Regular Advisor

Re: Another Inquiry

Hi JRF,

I've tested your script and the result does not change the file though the output at the screen is the right one. I need to change the file also just as the output at the screen.

Also, I can call the perl script you made from any other script, right?
Pando
Regular Advisor

Re: Another Inquiry

Hello Sandman!,

Sorry for the confusion. I have files (10000+) that needs to be change. I have tested the ex command and it actually, outputted the way i wanted to. But when i put it inside my script,
it ask for a key?
Sandman!
Honored Contributor

Re: Another Inquiry

Hi Pando,

I put the ex command inside a script too and it runs fine doesn't ask for a key. Could you attach your script here so it can be looked at. Also if you need to process 10000+ files simply put the ex command inside a for-loop i.e.


#!/usr/bin/sh

for i in $(ls -1 )
do
ex -s +'/^Product Name, XXX\/YY$/r insertfile | wq' $i
done