1827893 Members
1726 Online
109969 Solutions
New Discussion

Re: sed script help

 
SOLVED
Go to solution
John Meissner
Esteemed Contributor

sed script help

I am trying to replace and entire address in one file with the entire address in another file. The following is an excerpt from the first file (called core11.cfg):

# ftp and remsh sources can use a full path:
# archive_path = "/pub/IUXarchives/B.11.00_32bitCDE.gz"

# The data for the "impacts" statements are found
# by running /opt/ignite/lbin/archive_impact
impacts = "/" 27Kb
impacts = "/.dt" 35Kb
impacts = "/etc" 1864Kb
impacts = "/export" 1Kb
impacts = "/home" 1Kb
impacts = "/opt" 74096Kb
impacts = "/sbin" 13449Kb
impacts = "/stand" 1Kb
impacts = "/tmp" 1Kb
impacts = "/users" 40Kb
impacts = "/usr" 225951Kb
impacts = "/var" 5705Kb
exrequisite += "golden image - 64 bit OS"
visible_if = can_run_32bit


and this is the entire 2nd file (called mou110.impact):

impacts = "/" 23Kb
impacts = "/.sw" 100Kb
impacts = "/dev" 14Kb
impacts = "/etc" 41366Kb
impacts = "/home" 8Kb
impacts = "/opt" 1246434Kb
impacts = "/sbin" 37158Kb
impacts = "/stand" 55954Kb
impacts = "/tmp" 6Kb
impacts = "/users" 1Kb
impacts = "/usr" 909819Kb
impacts = "/var" 542014Kb

I'm trying to replace all the "impact" entried in the first file with the entried in the second file. The reasone I am trying to replace the whole section is that the entried are not all the same....
i.e.
/.dt (original file)
/.sw (replacement file)

I'm in the process of scripting the creation of a bootable ingnite DVD (iso image)
Any help would be appreciated.
Thanks
All paths lead to destiny
18 REPLIES 18
Ian Dennison_1
Honored Contributor

Re: sed script help

John,

Replace all entries named "impacts" in the first file with contents of the second file? Is that the right interpretation?

Ian
Building a dumber user
Rodney Hills
Honored Contributor

Re: sed script help

If we assume the comment line before the first "impacts" stays the same-

sed -e '/^impacts/d' -e '/^# by running/r mou110.impact' newcore11.cfg

This will delete all "impacts" line from original and "r"ead the replacment file following the comment.

HTH

-- Rod Hills
There be dragons...
John Meissner
Esteemed Contributor

Re: sed script help

sorry - I should clarify.... i just want to replace the lines that start with "impact"
All paths lead to destiny
Ian Dennison_1
Honored Contributor
Solution

Re: sed script help

rm /tmp/newfile
cat /tmp/file1 |awk 'BEGIN {indic=0} $1 ~ /impact/ && indic == 0 {indic=1;rc=system("cat /tmp/impacts")}
indic == 1 && $1 !~ /impact/ {indic=2}
indic == 0 && $1 !~ /impact/ {print $0}
indic == 2 {print $0} ' >/tmp/newfile

Sample code, testing and works. /tmp/newfile = new output, /tmp/impacts = new list of impacts (no blank lines).

Share and Enjoy! Ian
Building a dumber user
Massimo Bianchi
Honored Contributor

Re: sed script help

I would suggest a clean way:

#!/sbin/sh
TMP=$$

cat core11.cfg | grep -v impact > /tmp/$TMP.spool

cat /tmp/$TMP.spool > core11.cfg

cat mou110.impact >> core11.cfg

####EOF


Doing so you will have all the variable where you want.

HTH,
Massimo

John Meissner
Esteemed Contributor

Re: sed script help

rodney - yes the previous commented line will stay the same. I tried your line but it didn't change anything in the file.
All paths lead to destiny
Steve Steel
Honored Contributor

Re: sed script help

Hi

using file1 file2 in,temp as work and file3 out

grep -v impacts file1 > temp
grep "#" temp > file3
cat file2 >> file3
grep -v "#" temp >> file3
rm temp


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Rodney Hills
Honored Contributor

Re: sed script help

I put your 2 files on my system and run the following-

sed -e '/^impacts/d' -e '/^# by running/r testf2'
and this is the results-

# ftp and remsh sources can use a full path:
# archive_path = "/pub/IUXarchives/B.11.00_32bitCDE.gz"

# The data for the "impacts" statements are found
# by running /opt/ignite/lbin/archive_impact
impacts = "/" 23Kb
impacts = "/.sw" 100Kb
impacts = "/dev" 14Kb
impacts = "/etc" 41366Kb
impacts = "/home" 8Kb
impacts = "/opt" 1246434Kb
impacts = "/sbin" 37158Kb
impacts = "/stand" 55954Kb
impacts = "/tmp" 6Kb
impacts = "/users" 1Kb
impacts = "/usr" 909819Kb
impacts = "/var" 542014Kb
exrequisite += "golden image - 64 bit OS"
visible_if = can_run_32bit

I had both testf1 and testf2 both under my current directory.

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: sed script help

Remember, "sed" does not change the original file. It creates a "new" text file.

If you want to replace the original, then "mv" the resultant file to the original.

-- Rod Hills
There be dragons...
John Meissner
Esteemed Contributor

Re: sed script help

Ian - your solution does indeed work. Just so everyone else knows... the original file is much larger than I included in my first post. I'm attaching the entire file just so there is no misunderstandings.
All paths lead to destiny
Ian Dennison_1
Honored Contributor

Re: sed script help

John,

Have just noticed the recursion within your sample file (2 sets of "impacts" lines).

Do we need to code around this, by either
a) replacing both sets of "impacts" lines, or
b) only replacing a specific set of "impacts" lines?

Have solutions to both; let us know if you need them, and which one.

Share and Enjoy! Ian
Building a dumber user
Curtis Larson_2
Advisor

Re: sed script help

I'd do something this:

#!/usr/bin/ksh

file1=
file2=
tmpFile=
rm $tmpFile


cat file1 |
while read a b c size
do
start="$a $b $c"

case $start in
^${start}*)
size=$(grep "^$start" file2 | cut -f4)
;;
esac

print "$start $size" >> $tmpFile
done

haven't tested but should be pretty close to what your looking to do
John Meissner
Esteemed Contributor

Re: sed script help

rodney - I'm outputting your script into a new file and it didn't change anything. not sure why.
All paths lead to destiny
Jean-Louis Phelix
Honored Contributor

Re: sed script help

hi,

This awk script could also do it ...

Regards.

awk '/^impact/ {if (flg != 1)
{
flg=1
system("cat mou110.impact")
}
next
}
{print}' core11.cfg
It works for me (© Bill McNAMARA ...)
John Meissner
Esteemed Contributor

Re: sed script help

Ian - both sets would be great - I just wasn't sure how to do the entire addressing issue.
All paths lead to destiny
Ian Dennison_1
Honored Contributor

Re: sed script help

# Second code - Checks for keywords that indicate we have found our subsection (Reset every time we hit a right curly brace)
cat /tmp/file1 |awk -vsubset="64 bit" 'BEGIN {indic=0;subsect=0}
$0 ~ "\}" {indic=0;subsect=0}
$0 ~ subset {subsect=1}
$1 ~ /impact/ && indic == 0 && subsect == 1 {indic=1;rc=system("cat /tmp/impacts")}
indic == 1 && $1 !~ /impact/ {indic=2}
indic == 0 && $1 ~ /impact/ && subsect != 1 {print $0}
indic == 0 && $1 !~ /impact/ {print $0}
indic == 2 {print $0} ' >/tmp/newfile

# First code - resets back to Zero everytime if finds a curly brace (does all occurences)
# cat /tmp/file1 |awk 'BEGIN {indic=0}
# $0 ~ "\{" || $0 ~ "\}" {indic=0}
# $1 ~ /impact/ && indic == 0 {indic=1;rc=system("cat /tmp/impacts")}
# indic == 1 && $1 !~ /impact/ {indic=2}
# indic == 0 && $1 !~ /impact/ {print $0}
# indic == 2 {print $0} ' >/tmp/newfile

Attached as a file. First piece of code is commented out so I could test the second. Both pieces tested on your sample, second part tested on keywords "32 bit" and "64 bit".

Share and Enjoy! Ian
Building a dumber user
John Meissner
Esteemed Contributor

Re: sed script help

Ian - your script does exactly what I was looking for. I'm still testing some of the others and will report back and assign points to the rest.
All paths lead to destiny
John Meissner
Esteemed Contributor

Re: sed script help

Ian -
It's been a while since you solved this problem... I was bored today and was looking over this ... I've never used this type of structure (indic) before and was wondering if you could explain it a little?
if not thanks for the solution....

catch a man a fish and you feed him for a day.... teach a man to fish and you feed him for a lifetime.
All paths lead to destiny