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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting text at the start of a line where a search for text succeeds

Status
Not open for further replies.

MCubitt

Programmer
Mar 14, 2002
1,081
GB
Using awk, since that is what I am using to create a file from another, how may I prefix a line with text if the line contains certain text?

The input file is a list of file names, the output file is effectively a unix script to copy files.

However, I wish to condition any line which contains
/initXYZ.ora
or
/spfileXYZ.ora
or
/lkXYZ

(where XYZ is a variable: srcdb)

to prefix with # in the result.


The calling script has the line:
# Create backup and recovery scripts
awk -f bandr.unix backuploc=$despath rscript=$restorescript olddb=$srcdb newdb=$desdb $filelist>$backupscript

$despath would be something like /backup
$srcdb might be MYDB
$desdb might be TOBEDECIDED
$restorescript would be smth like restoreMYDB.unix
$filelist is filelist.txt
$backupscript might be backupMYDB.unix

the code for bandr.unix:
Code:
BEGIN {
FS = OFS = "/"
}

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

{
backup = backuploc $0
# Make new folder (if not present)
printf "mkdir -p $(dirname %s) 2> /dev/null\n",backup
# Copy file
printf "cp -p %s %s\n", $0, backup
# destination is to be decided
gsub(olddb,newdb)
printf "cp -p %s %s\n", backup, $0 >rscript
}

the final printf (creates recovery script) line is the one which should prefix the line with # if the condition is met.

Thanks in advance




Applications Support
UK
 
# Skip lines with "temp*.dbf".
toupper($0) ~ /TEMP[^\/]*\.DBF$/ { next }
{
backup = backuploc $0
# Make new folder (if not present)
printf "mkdir -p $(dirname %s) 2> /dev/null\n",backup
# Copy file
printf "cp -p %s %s\n", $0, backup
# destination is to be decided
cp=($0~olddb"(.ora|)$" ? "#" : "")"cp"
gsub(olddb,newdb)
printf "%s -p %s %s\n", cp, backup, $0 >rscript
}

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 for that (I missed my notification email, oops - sorry for the delay).

It did not appear to work...

I have
Code:
# Skip lines with "temp*.dbf".
toupper($0) ~ /TEMP[^\/]*\.DBF$/ { next }
{
backup = backuploc $0
# Make new folder (if not present)
printf "mkdir -p $(dirname %s) 2> /dev/null\n",backup
# Copy file
printf "cp -p %s %s\n", $0, backup
# destination is to be decided
cp=($0~olddb"(.ora|)$" ? "#" : "")"cp"
gsub(olddb,newdb)
printf "%s -p %s\n", cp, backup, $0 >rscript
}

Rather than creating
cp -p /backuppath/file /origpath/file

I get

cp -p /backuppath/file

Worse still, no lines had # preceeding them.

Thanks,




Applications Support
UK
 
Your script isn't the same as mine (last printf statement at least)
You may consider using nawk
You may have to tweak this line (olddb ?, .ora ?, ...):
cp=($0~olddb"(.ora|)$" ? "#" : "")"cp"

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

I beg your pardon, my mistake.

However, the # is still not preceeding, even with the new line with cp=...

The line
cp -p /ifsdoc/Backup/BackupofIFSD/oracle/app/oracle/product/9.2.0.1.0/dbs/initIFSD.ora /oracle/app/oracle/product/9.2.0.1.0/dbs/initDESTINATIONDBTOBEDECIDED.ora

should be
#cp -p /ifsdoc/Backup/BackupofIFSD/oracle/app/oracle/product/9.2.0.1.0/dbs/initIFSD.ora /oracle/app/oracle/product/9.2.0.1.0/dbs/initDESTINATIONDBTOBEDECIDED.ora

because of initIFSD.ora

where olddb=IFSD

Will try to think a bit harder.. but the line you provided totally blows my mind!







Applications Support
UK
 
Have you tried nawk ?
You may also try to change this:
cp=($0~olddb"(.ora|)$" ? "#" : "")"cp"
By this:
cp="cp";if($0~olddb"$" || $0~olddb".ora$")cp="#cp"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I have never used nawk and only heard of it while looking for help on awk.


However, the latest line WORKED!

Thanks PHV!




Applications Support
UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top