How to use gsub and array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use gsub and array
# 1  
Old 01-28-2012
How to use gsub and array

Hello,
i'm searching for a solution to this problem.
I have 2 files, the first one is like:

Code:
<HTML>
<HEAD>
<TITLE>{$String1}</TITLE>
</HEAD>
<BODY>
<P>{$String2}</P>
</BODY>
</HTML>

and the other one:
Code:

{$String1}; french
{$String2}; italian
{$String3}; english
...
{$StringN}; 

I have to substitute in the html file all the
{$String1} with the right word from the second file
Code:
BEGIN{    FS=";";
    while ((getline < "language.txt")>0){
            array[$1]=$2;
    }
}
{
    gsub(/$string[0-9]/, array[&]);   
    print
}

but this operation fail

Code:
awk: 12: unexpected character '&'

anyone can solve ?

thank you in advance
# 2  
Old 01-28-2012
Your dollar sign in {$String1} has to be escaped otherwise they will think it is the end anchor in the regular expression.

Also, I don't think there is a way to back reference in awk, unlike sed

Anyway, herer is my attempt with the following assumptions:
  • one occurrence of {$String1} in each line
  • there is a max of 9 languages, ie String1 to String9
Code:
awk '
NR==FNR {
	n=split($0, a, "[; ]")
	array[a[1]]=a[n]
	next
}
/{\$String[0-9]}/ {
	start=index($0, "{$String")
	string=substr($0, start, 10)
	re=string
	gsub("\$", "\\$", re)
	gsub(re, array[string])
	print
	next
}
{
	print
}' language.txt inputfile

# 3  
Old 01-29-2012
Quote:
Originally Posted by chihung
Your dollar sign in {$String1} has to be escaped otherwise they will think it is the end anchor in the regular expression.
Sure, you are right, ty.

Quote:
Also, I don't think there is a way to back reference in awk, unlike sed
So, i cant scann the array trying to find match with the regex before?
I dont understand this point, could you explain please in an other way?
# 4  
Old 01-29-2012
Try this...
Code:
awk -F"[; ]" 'NR==FNR{sub(/\$/,"\\$",$1);a[$1]=$3;next} {for(i in a){gsub(i,a[i])}print}' file2 file1

file1 - html file
file2 - language file

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Masking with gsub command

My file "test.dat" data as below Requirement is to mask(replace) all english characters with "X" EXCEPT first 7 characters of every line. my command awk '{gsub("]","X")}1' test.dat looks not working properly, Appreciate any suggestion... (6 Replies)
Discussion started by: JSKOBS
6 Replies

2. Shell Programming and Scripting

Gsub function in awk

Hello, I had some difficulty to understand the gsub function and maybe the regex in this script to remove all the punctuations: awk 'gsub(//, " ", $0)' text.txtFile text.txt: This is a test for gsub I typed this random text file which contains punctuation like ,.;!'"?/\ etc. The script... (6 Replies)
Discussion started by: yifangt
6 Replies

3. Shell Programming and Scripting

awk gsub

Hi, I want to print the first column with original value and without any double quotes The output should look like <original column>|<column without quotes> $ cat a.txt "20121023","19301229712","100397" "20121023","19361629712","100778" "20121030A","19361630412","100838"... (3 Replies)
Discussion started by: ysrini
3 Replies

4. Shell Programming and Scripting

GSUB/Regex Help

I am trying to write my gsub regex to replace a bunch of special characters with spaces, so i can split it to an array and look at each word independently. However, my regex skills are slightly lacking and I appear to be missing a quote or something here. I am trying to replace the following... (6 Replies)
Discussion started by: nitrobass24
6 Replies

5. Shell Programming and Scripting

awk extend array for gsub()

Want to extend an array to remove the rest of the field $2. Can anybody suggest the correct code? This code: awk 'NR==FNR{x;next}{for(r in x) gsub(r/.*/, "",$2)}1' patternlist infileneither this works: awk 'NR==FNR{x;next}{for(r in x) r=r+"/.*/" gsub(r/.*/, "",$2)}1' patternlist infile (5 Replies)
Discussion started by: sdf
5 Replies

6. Shell Programming and Scripting

awk gsub with variables?

Hey, I would like to replace a string by a new one. Teh problem is that both strings should be variables to be flexible, because I am having a lot of files (with the same structure, but in different folders) for i in daysim_* do cd $i/5/ folder=`pwd |awk '{print $1}'` awk '{ if... (3 Replies)
Discussion started by: ergy1983
3 Replies

7. Shell Programming and Scripting

Help with awk and gsub using C shell

Being new to awk, I am still running into little stupid things. For this issues I am trying to search for all occurrences of a string in a file and replace all of those occurrences with a replacement string. I tried doing awk '{gsub("|750101|", "|000000|", $0)}' infile > outfile Unix... (3 Replies)
Discussion started by: jclanc8
3 Replies

8. Shell Programming and Scripting

awk gsub

Hi all I want to do a simple substitution in awk but I am getting unexpected output. My function accepts a time and then prints out a validation message if the time is valid. However some times may include a : and i want to strip this out if it exists before i get to the validation. I have shown... (4 Replies)
Discussion started by: pxy2d1
4 Replies

9. Shell Programming and Scripting

Help with AWK and gsub

Hello, I have a variable that displays the following results from a JVM.... 1602100K->1578435K I would like to collect the value of 1578435 which is the value after a garbage collection. I've tried the following command but it looks like I can't get the > to work. Any suggestions as... (4 Replies)
Discussion started by: npolite
4 Replies

10. Shell Programming and Scripting

Gsub and nawk

Hello I have problem with reg-expr and function gsub(); File that I want to preprocess look like this: int table ; printf(" variable : ", variable) ; Using nawk I try something like this: for ( .... ) { line = $0 reg_expr = "\.\=]*" "" variable "" "\.\=]*" ; gsub( reg_expr... (1 Reply)
Discussion started by: scotty_123
1 Replies
Login or Register to Ask a Question