Operating System - Linux
1748028 Members
5182 Online
108757 Solutions
New Discussion юеВ

Re: Script to copy lines with certain string present

 
SOLVED
Go to solution
Coolmar
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!
10 REPLIES 10
James R. Ferguson
Acclaimed Contributor
Solution

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...
James R. Ferguson
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...
Coolmar
Esteemed Contributor

Re: Script to copy lines with certain string present

Thanks again James....now is there a way to delete any duplicate lines?
Chan 007
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
Victor Fridyev
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
James R. Ferguson
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...
Coolmar
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?
James R. Ferguson
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...

Coolmar
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?