Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with SED, creating script from file list 3

Status
Not open for further replies.

MCubitt

Programmer
Mar 14, 2002
1,081
0
0
GB
I have generated a file list which contains files (not directories) with the full path preceeding them, eg:
/oracledata/EDI/control01.ctl
/oracledata/EDI/EDI.dbf
/oracleindex/EDI/control02.ctl
/redolog1/EDI/redo01.log
/oracle/app/oracle/oradata/EDI/system01.dbf
/oracle/app/oracle/oradata/EDI/temp01.dbf


From that list i would like to create two new files, but deleting any line which has temp*.dbf and arranging the format, so...
(/backuploc is in a variable
TOBEDECIDED is in a variable)
file1:
cp -p /oracledata/EDI/control01.ctl /backuploc/oracledata/EDI/control01.ctl
cp -p /oracledata/EDI/EDI.dbf /backuploc/oracledata/EDI/EDI.dbf
cp -p /oracleindex/EDI/control02.ctl /backuploc/oracleindex/EDI/control02.ctl
cp -p /redolog1/EDI/redo01.log /backuploc/redolog1/EDI/redo01.log
cp -p /oracle/app/oracle/oradata/EDI/system01.dbf /backuploc/oracle/app/oracle/oradata/EDI/system01.dbf

file2:
cp -p /backuploc/oracledata/EDI/control01.ctl /oracledata/TOBEDECIDED/control01.ctl
cp -p /backuploc/oracledata/EDI/EDI.dbf /oracledata/TOBEDECIDED/TOBEDECIDED.dbf
cp -p /backuploc/oracleindex/EDI/control02.ctl /oracleindex/TOBEDECIDED/control02.ctl
cp -p /backuploc/redolog1/EDI/redo01.log /redolog1/TOBEDECIDED/redo01.log
cp -p /backuploc/oracle/app/oracle/oradata/EDI/system01.dbf /oracle/app/oracle/oradata/TOBEDECIDED/system01.dbf


Later, when a restore script is run I'd like to change "TOBEDECIDED" to be a selected value, so another find/replace.





Applications Support
UK
 
The line to delete can be temp or TEMP.
thanks


Applications Support
UK
 
for starters:
sed -e '1,$s#.*#cp -p & /backuploc/&#g' fileList.txt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks vlad,

I ended up with:

sed -e "/temp01.dbf/d" $filelist | sed -e '1,$s#.*#cp -p & '$despath'&#g' > $backupscript

which seems to do a bit of what I wanted.

I am unsure how to ignore case with sed...

thanks




Applications Support
UK
 
sed -e "/[tT][eE][mM][p].*\.dbf/d" -e '1,$s#.*#cp -p & '"$despath"'&#g' $filelist > $backupscript

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV, thanks..

I have to LOL when I see UNIX scripts, they are so bizarre they actually look encrypted!

So now I have my backup script.. I can work on my restore.




Applications Support
UK
 
I now have a requirement to add an additional line to the backup script.

where as
/oracledata/EDI/EDI.dbf
would create
cp -p /oracledata/EDI/EDI.dbf /backuploc/oracledata/EDI/EDI.dbf
it now must also create
mkdir -p $(dirname /backuploc/oracledata/EDI/EDI.dbf) 2> /dev/null
just before
(for each line)

Is that a goer?




Applications Support
UK
 
You may try the awk way (typed, untested):
awk -v dest="$destpath" '
/[tT][eE][mM][pP].*\.dbf/{next}
{printf "mkdir -p $(dirname %s%s)2>/dev/null\n",destpath,$0
printf "cp -p %s %s%s\n",$0,destpath,$0}
' $filelist > $backupscript

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
If not for the speed problem you speak of in your other thread thread822-989438 (and I do not know if this is so much un issue), it could be so much simpler with tar.
You could directly use the fileList.txt

Code:
# backup
(cd /; tar cf - -T fileLst.txt) | (cd /backuploc; tar xf -)
# restore
(cd /backuploc; tar cf - ) | (cd $TOBEDECIDED; tar xf -)

Can somebody give some inputs about the compared speed of tar versus cp.

--------------------

Denis
 
Oh gawd, that looks confusing!

I ended up doing this:
sed -e "/[tT][eE][mM][p].*\.dbf/d" -e '1,$s#.*#mkdir -p $(dirname '"$despath"'&) 2> /dev/null #g' $filelist > $backupscript
sed -e "/[tT][eE][mM][p].*\.dbf/d" -e '1,$s#.*#cp -p & '"$despath"'&#g' $filelist >> $backupscript

That was I do all the mkdir's and then all the cp's.

Now, I am ALSMOST there!

The restore script needs to do this:
Take
/oracledata/EDI/control01.ctl
/oracledata/EDI/EDI.dbf
/oracle/app/initEDI.ora
and produce
cp -p /backup/oracledata/EDI/control01.ctl /oracledata/TOBEDECIDED/control01.ctl
/oracledata/TOBEDECIDED/TOBEDECIDED.dbf
/oracle/app/initTOBEDECIDED.ora

so it changes the EDI (actually $srcdb) to TOBEDECIDED as a find/replace.

Thanks.



Applications Support
UK
 
Denis,

Thank you for your post.

It's not quite as straight forward as you suggest, with TOBEDECIDED replacing occurences of the source DB in path and file, so
/oracle/EDI/initEDI.ora
would actually be restored to (as far as the script reads)
/oracle/TOBEDECIDED/initTOBEDECIDED.ora

and at restore time, the restore script would be regenerated changing TOBEDECIDED to the name of the chosen DB.

...if that makes sense!





Applications Support
UK
 
Have you tried the awk way ?
It would be more flexible.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV,

To be honest it didn't make sense to me..
awk -v dest="$destpath" '
/[tT][eE][mM][pP].*\.dbf/{next}
{printf "mkdir -p $(dirname %s%s)2>/dev/null\n",destpath,$0
printf "cp -p %s %s%s\n",$0,destpath,$0}
' $filelist > $backupscript

what is -v ?

why use dest= ?


I'd rather use a piece of script I understand (even if it's not the most efficient) than one I don't and can't get close to understanding.. and also that would stuff my colleagues up!

At least the sed has some degree of readability - just!




Applications Support
UK
 
that's the first time I heard sed being more readable than awk [wink].

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Run with
awk -f backup.awk backuploc="/foo" rscript="file2" data
>file1

backup.awk:
Code:
BEGIN {
  FS = OFS = "/"
}

# Skip lines with "temp*.dbf".
toupper($0) ~ /TEMP[^\/]*\.DBF$/ { next }

{
  backup = backuploc $0
  printf "mkdir -p $(dirname %s) 2> /dev/null\n",
    backup
  printf "cp -p %s %s\n", $0, backup
  DB = $(NF-1)
  $(NF-1) = "TOBEDECIDED"
  sub( "^" DB, "TOBEDECIDED", $NF )
  printf "cp -p %s %s\n", backup, $0  >rscript
}

Let me know whether or not this helps.

If you have nawk, use it instead of awk because on some systems awk is very old and lacks many useful features. Under Solaris, use /usr/xpg4/bin/awk.

For an introduction to Awk, see faq271-5564.

 
vlad, good point, awk and sed are both equally awful, powerful, but disgustingly unclear!


Applications Support
UK
 
Future,

Thanks for the suggestion.

What is "data" ? Constant, variable?



Applications Support
UK
 
Future,

Actually, I can not work out how to use that at all, sorry.


Applications Support
UK
 
Future,

I am looking at the script you sent.

Would you please annotate it so it's clear what it's doing?

What is FS? What is OFS?

# Skip lines with "temp*.dbf".
toupper($0) ~ /TEMP[^\/]*\.DBF$/ { next }
This bit is clear.

What is DB, NF, $NF ?

What should rscript be? What is data?

Thanks





Applications Support
UK
 
Finally I worked it out (I think!)

awk -f backup.awk backuploc="/ifsdoc/Backup" rscript="recover_script" filelist.txt>backup_script

(where filelist.txt is the listing of files to include)



Applications Support
UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top