Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

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
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?
Highlighted
Sandman!
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