Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-07-2012
Registered User
 
Join Date: Dec 2009
Location: London
Posts: 776
Thanks: 51
Thanked 6 Times in 6 Posts
Replacing string with awk

I am trying to replace


Code:
javascript:top.showarticle('geo',1985,'

with


Code:
../../abstracts/geo1985/

I am using awk but getting an error


Code:
awk -v s="javascript:top.showarticle('geo',1985,'" -v d="../../abstracts/geo1985/" '{gsub(s,d); print} ' geo50n12.html > geo50n12.html.tmp

Fixed it using


Code:
awk -v s="javascript:top.showarticle\('geo',1985,'" -v d="../../abstracts/geo1985/" '{gsub(s,d); print} ' geo50n12.html > geo50n12.html.tmp

---------- Post updated at 02:44 PM ---------- Previous update was at 01:18 PM ----------

I also want to replace


Code:
','geo    with    /geo

and


Code:
',0)   with   .pdf

I have tried using the following but is not working


Code:
awk -v s=$src -v d=$dst '{gsub(s,d); gsub("','geo","/geo"); gsub("',0)",".pdf"); print}' $f > $ftmp


Last edited by kristinu; 04-07-2012 at 03:41 PM..
Sponsored Links
    #2  
Old 04-07-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,346
Thanks: 144
Thanked 1,755 Times in 1,592 Posts
s="$src" and d="$dst" need double quotes
s, being the first part in the sub function is not a string but an extended regular expression. As such special characters need to be escaped, with 4 backslashes:


Code:
src="javascript:top.showarticle\\\\('geo',1985,'"

Alternatively you could use this:

Code:
src="javascript:top.showarticle[(]'geo',1985,'"

Technically a . means "any character" so in theory that would need to be escaped, but in practice that will probably not be necessary..

Last edited by Scrutinizer; 04-07-2012 at 05:02 PM..
Sponsored Links
    #3  
Old 04-07-2012
Registered User
 
Join Date: Dec 2009
Location: London
Posts: 776
Thanks: 51
Thanked 6 Times in 6 Posts
One thing at a time

I am writing the commands to a file, called


Code:
commands.run

and then run everything using

./commands.run

So I need to print the command.

I am doing this using


Code:
set year = "1985"
echo "awk -v s="javascript:top\.showarticle[(]'geo',${year},'" -v d="../../abstracts/geo${year}/" '{gsub(s,d); print}' $f > $ftmp" >> $frun

How can I introduce the " in the file?

---------- Post updated at 05:04 PM ---------- Previous update was at 04:13 PM ----------

I am now just trying to do the command:

Quote:
Replace


Code:
 javascript:top.showarticle('geo',1985

with


Code:
../../../geopdf/geo1985/

Quote:
Replace


Code:
','geo

with


Code:
/geo

Quote:
Replace


Code:
',0)

with


Code:
.pdf

I am trying coding it, however the second and third replacements I am finding problematic.


Code:
awk -v s="javascript:top\.showarticle[(]'geo',1985,'" -v d="" '{gsub(s,d); gsub(/',0)/,.pdf); print}' geo50n01.html

The following is part of the file:


Code:
<font FACE=arial><b>On: S. Hammer's replies to N. C. Steenland, A. T. Herring, and W. C. Pearson's discussions of "Airborne gravity is here" (GEOPHYSICS, 49, 310-311, March 1984; and 49, 470-477, April 1984).</b></font> <font size=2>(170-170)</font><br>
M. J. Hall<br><a href=../../abstracts/geo1985/geo50n01/geo5001r01700171.html>Abstract</a> | <a href=javascript:top.showarticle('geo',1985,'geo50n01','geo5001r01700171',0)>PDF</a><p>

<font FACE=arial><b>Reply by the author to M. J. Hall</b></font> <font size=2>(170-171)</font><br>
Sigmund Hammer<br><a href=../../abstracts/geo1985/geo50n01/geo5001r01700171_b.html>Abstract</a> | <a href=javascript:top.showarticle('geo',1985,'geo50n01','geo5001r01700171',0)>PDF</a><p>

<font FACE=arial><b>On: "Predictive deconvolution and the zero-phase source" by B. Gibson and K. Larner (GEOPHYSICS, 49, 379-397, April 1984).</b></font> <font size=2>(172-172)</font><br>
W. Harry Mayne, Consultant<br><a href=../../abstracts/geo1985/geo50n01/geo5001r01720172.html>Abstract</a> | <a href=javascript:top.showarticle('geo',1985,'geo50n01','geo5001r01720172',0)>PDF</a><p>

<font FACE=arial><b>Reply by the authors to W. Harry Mayne</b></font> <font size=2>(172-172)</font><br>
Bruce Gibson; Ken Larner<br><a href=../../abstracts/geo1985/geo50n01/geo5001r01720172_b.html>Abstract</a> | <a href=javascript:top.showarticle('geo',1985,'geo50n01','geo5001r01720172',0)>PDF</a><p>

---------- Post updated at 07:35 PM ---------- Previous update was at 05:04 PM ----------

I have now fixed the problem with


Code:
awk -v s="javascript:top\.showarticle[(]'geo',1985,'" -v d="../../../geopdf/geo1985/" '{gsub(s,d); print}' geo50n01.html | sed -e "s:','geo:/geo:g" | sed -e "s:',0):\.pdf:g"

I am now trying using a year as a variable, to no avail.


Code:
set year = "1888"
awk -v s="javascript:top\.showarticle[(]'geo',${year},'" -v d="../../../geopdf/geo${year}/" '{gsub(s,d); print}' geo50n01.html | sed -e "s:','geo:/geo:g" | sed -e "s:',0):\.pdf:g"


Last edited by kristinu; 04-07-2012 at 05:24 PM..
    #4  
Old 04-08-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,346
Thanks: 144
Thanked 1,755 Times in 1,592 Posts
I think the problem is here:
Quote:
Code:
set year = "1888"

That is still csh-ish (due to habit?) but it has an entirely different effect in Bourne derived shells (it will not set the variable, but it will set $1 to "year" , $2 to "=" and $3 to 1888 ..)
It should be:

Code:
year=1888

(no spaces around the = )
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
replacing a string with another string in a txt file forevertl UNIX for Dummies Questions & Answers 9 03-18-2011 01:58 PM
Help replacing string Grueben Shell Programming and Scripting 4 11-07-2010 03:30 AM
replacing a string in multiple subdirs to a new string?? Hangman2 Shell Programming and Scripting 3 10-01-2009 02:59 AM
Replacing string Giordano Bruno UNIX for Dummies Questions & Answers 9 10-23-2008 10:01 AM
string replacing priya_9patil Shell Programming and Scripting 1 11-03-2006 11:05 AM



All times are GMT -4. The time now is 08:47 PM.