1827808 Members
3150 Online
109969 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
Pando
Regular Advisor

Re: Another Inquiry

Hello Sandman!,

Please see attched script and some raw file. This also includes the file that needs to be inserted.
In the script, i have changed/replaced the /^Product Name,XXX/YY$/ to /^Diffusion Lot ID,NA$/ (Kindly check the raw data) because the Diffusion Lot ID is more fixed.
Thanks and hoping to hear from you soon.
Pando
Regular Advisor

Re: Another Inquiry

Hello Sandman!,

Please see attached script and some raw file (zipped format). This also includes the file that needs to be inserted (integ) to the 3 file with TDF extension.
In the script, i have changed/replaced the /^Product Name,XXX/YY$/ to /^Diffusion Lot ID,NA$/ (Kindly check the raw data) because the Diffusion Lot ID is more fixed.
Thanks and hoping to hear from you soon.
Peter Nikitka
Honored Contributor
Solution

Re: Another Inquiry

Hi,

a short one to test:
File content before:
cat /tmp/x
aa
bb
cc
aa
dd
gg

Running the command
perl -i -e 'while (<>) { if($f) {print "xx\nyy\n";$f=0;} print; chomp; if ($_ eq "aa") { $f=1} }' /tmp/x

results in
cat /tmp/x
aa
xx
yy
bb
cc
aa
xx
yy
dd
gg

Perhaps this is enough - just change the conditions for inserting as needed.

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"
inventsekar_1
Respected Contributor

Re: Another Inquiry

Hi Pando,
just 2 suggestions:
1. writing the subject line clearly will give good fast response and while searching it will help a lot.
2. submit points to those who helped you.

Be Tomorrow, Today.
Pando
Regular Advisor

Re: Another Inquiry

Thanks to all replies!
James R. Ferguson
Acclaimed Contributor

Re: Another Inquiry

Hi (again) Pando:

If you want to update the file "in-place" while creating a backup copy with the extension '.old', use this variation of my script.

# cat inserter
#!/usr/bin/perl -i.old
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

No output occurs to your terminal. You can 'cat' the modified file when done. As noted, the original file is preserved as 'file.old' where 'file' is whatever filename you specified.

Regards!

...JRF...
Pando
Regular Advisor

Re: Another Inquiry

Thanks Peter and Sandman!,

This forum is really great!