Jump to content
 English      
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
     Forums advanced search
HP.com Home
IT Resource Center Forums > HP-UX > general

sed script help

» 

IT Resource Center

» Login
» Register
» My profile
» Search knowledge base
» Forums
» Patch database
» Download drivers, software and firmware
» Warranty check
» Support Case Manager
» Software Update Manager
» Training and Education
» More maintenance and support options
» Online help
» Site map

Member icons
 
 HP moderator  HP moderator
 Expert in this area  Expert in this area
Member status
ITRC Pro ITRC Pro
250 points
ITRC Graduate ITRC Graduate
500 points
ITRC Wizard ITRC Wizard
1000 points
ITRC Royalty ITRC Royalty
2500 points
ITRC Pharaoh ITRC Pharaoh
7500 points
Olympian Olympian
20000 points
1-Star Olympian 1-Star Olympian
40000 points
2-Star Olympian 2-Star Olympian
80000 points
»  How to earn points
»  Support forums FAQs
Question status
Magical answer Magical answer
Message with a response that solved the author's question
Favorites status
Add to my favorites Add to my favorites
Delete from my favorites Delete from my favorites
This thread has been closed Thread closed
 

Content starts here
   Create a new message    Receive e-mail notification if a new reply is posted  Reply to this message
Author Subject: sed script help      Add to my favorites
John Meissner This member has accumulated 2500 or more points
May 20, 2003 14:31:19 GMT   

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
Note: If you are the author of this question and wish to assign points to any of the answers, please login first.For more information on assigning points ,click here


Sort Answers By: Date or Points
Ian Dennison This member has accumulated 2500 or more points
May 20, 2003 14:40:48 GMT  3 pts

John,

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

Ian
Rodney Hills This member has accumulated 7500 or more points
May 20, 2003 14:43:53 GMT  5 pts

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

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

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

HTH

-- Rod Hills
John Meissner This member has accumulated 2500 or more points
May 20, 2003 14:44:32 GMT    N/A: Question Author

sorry - I should clarify.... i just want to replace the lines that start with "impact"
Ian Dennison This member has accumulated 2500 or more points
May 20, 2003 14:51:31 GMT  10 pts

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
Massimo Bianchi This member has accumulated 7500 or more points
May 20, 2003 14:51:33 GMT  2 pts

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 This member has accumulated 2500 or more points
May 20, 2003 14:52:02 GMT    N/A: Question Author

rodney - yes the previous commented line will stay the same. I tried your line but it didn't change anything in the file.
Steve Steel This member has accumulated 7500 or more points
May 20, 2003 14:57:45 GMT  2 pts

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
Rodney Hills This member has accumulated 7500 or more points
May 20, 2003 15:01:18 GMT  5 pts

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

sed -e '/^impacts/d' -e '/^# by running/r testf2' <testf1

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
Rodney Hills This member has accumulated 7500 or more points
May 20, 2003 15:04:50 GMT  1 pts

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
John Meissner This member has accumulated 2500 or more points
May 20, 2003 15:05:28 GMT    N/A: Question Author Attachement is 3078.txt 

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.
Ian Dennison This member has accumulated 2500 or more points
May 20, 2003 15:08:46 GMT  3 pts

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
Curtis Larson
May 20, 2003 15:12:12 GMT  5 pts

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 This member has accumulated 2500 or more points
May 20, 2003 15:12:33 GMT    N/A: Question Author

rodney - I'm outputting your script into a new file and it didn't change anything. not sure why.
Jean-Louis Phelix This member has accumulated 2500 or more points
May 20, 2003 15:13:50 GMT  5 pts

hi,

This awk script could also do it ...

Regards.

awk '/^impact/ {if (flg != 1)
{
flg=1
system("cat mou110.impact")
}
next
}
{print}' core11.cfg
John Meissner This member has accumulated 2500 or more points
May 20, 2003 15:15:18 GMT    N/A: Question Author

Ian - both sets would be great - I just wasn't sure how to do the entire addressing issue.
Ian Dennison This member has accumulated 2500 or more points
May 20, 2003 15:34:41 GMT  10 pts Attachement is 3077.txt 

# 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
John Meissner This member has accumulated 2500 or more points
May 20, 2003 15:46:35 GMT    N/A: Question Author

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.
John Meissner This member has accumulated 2500 or more points
Jun 2, 2003 17:06:06 GMT    N/A: Question Author

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.
 
Create a new message    Receive e-mail notification if a new reply is posted   Reply to this message
 
 
Printable version
Privacy statement Using this site means you accept its terms
© 2009 Hewlett-Packard Development Company, L.P.