Re: Script to copy lines with certain string present (510 Views)
Reply
Esteemed Contributor
Coolmar
Posts: 1,074
Registered: ‎12-21-2004
Message 1 of 11 (510 Views)
Accepted Solution

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!
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 11 (510 Views)

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
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 11 (510 Views)

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
Coolmar
Posts: 1,074
Registered: ‎12-21-2004
Message 4 of 11 (510 Views)

Re: Script to copy lines with certain string present

Thanks again James....now is there a way to delete any duplicate lines?
Honored Contributor
Chan 007
Posts: 749
Registered: ‎12-06-2005
Message 5 of 11 (510 Views)

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
Honored Contributor
Victor Fridyev
Posts: 937
Registered: ‎07-01-1998
Message 6 of 11 (510 Views)

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
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 7 of 11 (510 Views)

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
Coolmar
Posts: 1,074
Registered: ‎12-21-2004
Message 8 of 11 (510 Views)

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
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 9 of 11 (510 Views)

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
Coolmar
Posts: 1,074
Registered: ‎12-21-2004
Message 10 of 11 (510 Views)

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
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 11 of 11 (510 Views)

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
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.