Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Script to copy lines with certain string present

SOLVED
Go to Solution
Esteemed Contributor Esteemed Contributor

Script to copy lines with certain string present

I have a file that contains many lines like the following:

/u01/oracle/product/8.1.7/bin/oracle::::APPLICATIONS,abc09def

I need a script that will find all lines with 8.1.9 and copy that exact line right underneath the original line but replace with 8.1.7 with 9.2.0...so it looks like the following:

/u01/oracle/product/8.1.7/bin/oracle::::APPLICATIONS,abc09def
/u01/oracle/product/9.2.0/bin/oracle::::APPLICATIONS,abc09def

Thanks for the help!
1 ACCEPTED SOLUTIONS
Acclaimed Contributor Acclaimed Contributor

Re: Script to copy lines with certain string present

Hi sally:

# perl -nle 'print;print if s%/8.1.7/%/9.2.0/%' file

Regards!

...JRF...
10 REPLIES
Acclaimed Contributor Acclaimed Contributor

Re: Script to copy lines with certain string present

Hi sally:

# perl -nle 'print;print if s%/8.1.7/%/9.2.0/%' file

Regards!

...JRF...
Acclaimed Contributor Acclaimed Contributor

Re: Script to copy lines with certain string present

Hi (again) Sally:

To be strictly safe in our matching, i *should* escape the dot character :-))

# perl -nle 'print;print if s%/8\.1\.7/%/9\.2\.0/%' file

Regards!

...JRF...
Esteemed Contributor Esteemed Contributor

Re: Script to copy lines with certain string present

Thanks again James....now is there a way to delete any duplicate lines?
Honored Contributor Honored Contributor

Re: Script to copy lines with certain string present

Hi Sally,

to remove duplicate use

cat /tmp/test |sort |uniq

will give only one line

Chan
Highlighted
Honored Contributor Honored Contributor

Re: Script to copy lines with certain string present

Hi,

awk '/8.1.7/ {print}' filename |
sed 's/8.1.7/9.2.0/g' |sort |sort -u

This is not so beautiful as with perl, but it looks much simpler 8))))

HTH
Entities are not to be multiplied beyond necessity - RTFM
Acclaimed Contributor Acclaimed Contributor

Re: Script to copy lines with certain string present

Hi (again) Sally:

You don't need extra processes spawned (e.g. 'cat' into a pipe). Rather:

# sort -u file|perl -nle 'print;print if s%/8\.1\.7/%/9\.2\.0/%'

Regards!

...JRF...
Esteemed Contributor Esteemed Contributor

Re: Script to copy lines with certain string present

but the dupilicates happen after the perl ... so shouldn't the sort happen after the perl to get rid of the lines?
Acclaimed Contributor Acclaimed Contributor

Re: Script to copy lines with certain string present

Hi Sally:

You wrote, "but the duplicates happen after the perl ... so shouldn't the sort happen after the perl to get rid of the lines?"

Please show some input and the output you want. I'm confused by your question.

Regards!

...JRF...

Esteemed Contributor Esteemed Contributor

Re: Script to copy lines with certain string present

It's ok, I figured it out.
Now, when I do the sort -u it sorts the whole file...is there a way to tell it to sort only on lines beginning with /u01 and leave the rest of the file entact?
Honored Contributor Honored Contributor

Re: Script to copy lines with certain string present

Here's an awk construct that adds lines with 9.2.0 below each of the lines containing 8.1.7 and removes only those duplicate lines that begin with "/u01".

cat inp | awk '{x=$0;print x;if(x~/8.1.7/){gsub("8.1.7","9.2.0");print}}' | awk '{if($0~/^\/u01/)l[$0]++;else print} END{for(i in l) print i}' > out

~cheers