awk's gsub variable in replacement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk's gsub variable in replacement
# 1  
Old 04-10-2011
awk's gsub variable in replacement

I been trying to figure out how to use element of array as a replacement pattern. This works as I expected:
Code:
$ echo "one two three" | awk '{
  gsub(/wo/,"_BEG_&_END_",$2); 
  print }'
one t_BEG_wo_END_ three 
$ echo "one two three" | awk '{
  tmp="foo"; 
  gsub(/wo/,"_BEG_" tmp "_END_",$2); 
  print}'
one t_BEG_foo_END_ three
$ echo "one two three" | awk '{
  tmp["wo"]="foo"; 
  gsub(/wo/,"_BEG_" tmp["wo"] "_END_",$2); 
  print}'
one t_BEG_foo_END_ three
$ echo "one two three" | awk '{
  tmp["wo"]="foo"; 
  a="wo"; 
  gsub(/wo/,"_BEG_" tmp[a] "_END_",$2); 
  print}'
one t_BEG_foo_END_ three

So why does this not?
Code:
$ echo "one two three" | awk '{tmp["wo"]="foo"; gsub(/wo/,"_BEG_" tmp["&"] "_END_",$2); print}'
one t_BEG__END_ three

How do I access the element of array tmp[<captured_string>]?
Thank you.

Last edited by mirni; 04-11-2011 at 12:41 AM..
# 2  
Old 04-11-2011
Think your going to need to use match with RSTART and RLENGTH:

Code:
$ echo "one two three" | awk '{tmp["wo"]="foo";
if(match($0, "wo")) {
    fnd=substr($0, RSTART, RLENGTH);
    print substr($0, 1, RSTART-1) "_BEG_" tmp[fnd] "_END_" substr($0,RSTART+RLENGTH)
}}'
one t_BEG_foo_END_ three

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 04-11-2011
Thanks Chubler.
Confusing part is, that i can use array inside gsub replacement argument like
Code:
gsub(/pattern/,array[var],$0);

, but not array["&"].... like
Code:
gsub(/pattern/,array["&"],$0);

# 4  
Old 04-11-2011
This is not confusing if you think of gsub as a function that takes 3 parameters. The value of the 2nd parameter has to be evaluated before the function is called.

As you can see the value of what's being replaced cannot be known until the funtion returns and at this point the 2nd parameter has already be evaluated.
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 04-11-2011
Ahhh... now it makes sense. (Although I had to re-read your post a few times Smilie )

I guess I was hoping that the function would take as a second argument a pointer of the array, then the element could be retrieved from within, after the captured pattern is known.
(I'm thinking in terms of C-style array, whereas assoc. arrays of awk are quite different, aren't they?)
Thanks a lot once again.
# 6  
Old 04-11-2011
You could always write your own function to do this:

Code:
awk 'function arrayrep(pattern, array, string) {
    while (match(string, pattern)) {
        fnd=substr(string, RSTART, RLENGTH);
        string = substr(string, 1, RSTART-1) array[fnd] substr(string,RSTART+RLENGTH)}
    return string
}
BEGIN {
  G["%NAME1%"]="Jack"
  G["%NAME2%"]="Jill"
  print arrayrep("%[^%]*%", G, "%NAME1% and %NAME2% went up the hill ... %NAME1% fell down") }'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assign awk gsub result to a variable

Hello, I have searched but failed to find what exactly im looking for, I need to eliminate first "." in a output so i can use something like the following echo "./abc/20141127" | nawk '{gsub("^.","");print}' what i want is to use gsub result later on, how could i achieve it? Let say... (4 Replies)
Discussion started by: EAGL€
4 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

Trouble with passing Variable from bash to awk gsub command

Would really appreciate it if someone could point out my mistake in this line of code, i've been staring blankly at it trying everything i can think of some time now and coming up with nothing. #!/bin/bash echo "Enter Username" read Username awk -F: -v var=${Username} '/^var:/... (9 Replies)
Discussion started by: Nostyx
9 Replies

5. Shell Programming and Scripting

Awk; gsub in fields 3 and 4

I want to transform a log file into input for a database. Here's the log file: Tue Aug 4 20:17:01 PDT 2009 Wireless users: 339 Daily Average: 48.4285 = Tue Aug 11 20:17:01 PDT 2009 Wireless users: 295 Daily Average: 42.1428 = Tue Aug 18 20:17:01 PDT 2009 Wireless users: 294 Daily... (6 Replies)
Discussion started by: Bubnoff
6 Replies

6. Shell Programming and Scripting

Awk gsub error.

I want to replace comma with space and "*646#" with space. I am using the following code: nawk -F"|" '{gsub(","," ",$3); gsub(/\*646\#/"," ",$3);print}' OFS="|" file I am getting following error: Help is appreciated (5 Replies)
Discussion started by: pinnacle
5 Replies

7. Shell Programming and Scripting

Awk Gsub Query

Hi, Can some one please explain the following line please throw some light on the ones marked in red awk '{print $9}' ${FTP_LOG} | awk -v start=${START_DATE} 'BEGIN { FS = "." } { old_line1=$0; gsub(/\-/,""); if ( $3 >= start ) print old_line1 }' | awk -v end=${END_DATE} 'BEGIN { FS="." } {... (3 Replies)
Discussion started by: crosairs
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

Awk variable replacement

I have a function awkvarrep() { awk -F'|' '$1~/$1/{printf "%-10s %-30s %-15s %-30s %-15s\n", $2,$3,$4,$5,$6}' testfile } I'm calling it by this VARREP=XYZ awkvarrep $VARREP since i'm passing $VARREP to the awkvarrep() function I want to use this with $1 but it dosen't seem to be... (5 Replies)
Discussion started by: iAm4Free
5 Replies
Login or Register to Ask a Question