Replacing string with awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replacing string with awk
# 1  
Old 04-07-2012
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 04:41 PM..
# 2  
Old 04-07-2012
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 06:02 PM..
# 3  
Old 04-07-2012
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 06:24 PM..
# 4  
Old 04-08-2012
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 = )
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing a string

Hi All, I have a many folders in a directory under which there are many subdirectories containing text files containing the word "shyam" in them.I want all the files in all the directories containing "shyam to "ram" ?? sed "s/shyam/ram/g" does it ??But anyone can help me with the script ?? ... (3 Replies)
Discussion started by: Pradeep_1990
3 Replies

2. Shell Programming and Scripting

Help with replacing string

Hi All, I have below requirement: I need to read each line in file.txt and replace string starting from position 9 to 24 {111111111111111,222222222222222,333333333333333} by common string "444444444444444" and save file. File.txt: 03000003111111111111111 ... (3 Replies)
Discussion started by: smalode
3 Replies

3. UNIX for Dummies Questions & Answers

replacing a string with another string in a txt file

Dear all, I have a file like below. I want to replace all the '.' in the 3rd column with 'NA'. I don't know how to do that. Anyone has an iead? Thanks a lot! 8 70003200 21.6206 9 70005700 17.5064 10 70002200 . 11 70005100 19.1001 17 70008000 16.1970 32 70012400 26.3465 33... (9 Replies)
Discussion started by: forevertl
9 Replies

4. Shell Programming and Scripting

Help replacing string

Help! I'm trying this command but keep getting illegal syntax etc. awk '{ sub(/00012345/,"000123456"); print}' >newfile I don't understand. It works on one unix machine but not another! (4 Replies)
Discussion started by: Grueben
4 Replies

5. Shell Programming and Scripting

replacing a string in multiple subdirs to a new string??

I have following set of dirs: /dir1/dir2/subdir1 file1 file2 /dir1/dir3/subdir1 file4 file5 /dir1/dir4/subdir1 file6 file7 All of these files have a common string in them say "STRING1", How can I... (3 Replies)
Discussion started by: Hangman2
3 Replies

6. UNIX for Dummies Questions & Answers

Replacing string

Hi there, I'd like to replace STRING_ZERO in FILE_ZERO.txt with the value of VALUEi-th by using something like that: VALUE1=1000 VALUE2=2000 VALUE3=3000 for((i=1;i<=3;i++)); do sed "s/STRING_ZERO/$VALUE'$i'/" FILE_ZERO.txt >> FILE_NEW.txt; done but it doesn't work... Any help... (9 Replies)
Discussion started by: Giordano Bruno
9 Replies

7. Shell Programming and Scripting

Problem replacing the string

I have three files that the string inside it I want to replace so my code will be #!/bin/bash read -p "please input the old string:" string1 read -p "please input the new string:" string2 sed -i "s/string1/string2/g" *.c but the problem is.. the string that I want to replace can't be... (2 Replies)
Discussion started by: Viken
2 Replies

8. Programming

replacing char with string

how we can replace char with a string example char *a="a.s" so finally what i ant to do raplace a with ant and s sree so in my array a i want to store the value as "ant.sree" thank u in advance (1 Reply)
Discussion started by: phani_sree
1 Replies

9. Shell Programming and Scripting

string replacing

hii, i need a unix command which replaces all occurrences of a substring within a string with another substring. My solution: string="plalstalplal" sub1="al" sub2="mlkl" echo sed 's/$s2/$s3/g' < s1 > p i want to know how to read the variables s2 and s3.. thaks a lot bye (1 Reply)
Discussion started by: priya_9patil
1 Replies
Login or Register to Ask a Question