Gsub and nawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Gsub and nawk
# 1  
Old 05-10-2007
Gsub and nawk

Hello

I have problem with reg-expr and function gsub();

File that I want to preprocess look like this:
int table [variable][variable];
printf(" variable : ", variable) ;

Using nawk I try something like this:
for ( .... )
{
line = $0
reg_expr = "[^\"][ a-zA-Z0-9\+\-\*\/\&\^\%\#\!\~\,\[\]\.\=]*" "" variable "" "[ a-zA-Z0-9\+\-\*\/\&\^\%\#\!\~\,\[\]\.\=]*[^\"]" ;
gsub( reg_expr ,'200',line)
print line
}

After preprocesing it should look like this :

int table[200][200];
printf("variable:", 200);


My program shouldn't change "variable" in function printf.
Main problem is with quotation marks .
After removing expresions like: " .. . . . .." , I can write :
gsub(variable ,'200', line ), and everything is fine .
Second problem with my reg_expr is that it's removes braces and other characters .

How to write proper reg-expr ?
# 2  
Old 05-10-2007
I'm not sure that you can do all the work with a single regex.
A possible solution :
Code:
   Line = $0;
   while ( match(Line, /\"[^\"]*variable[^\"]*"|[0-9a-zA-Z_]variable|variable[0-9a-zA-Z_]/) ) {
      str = substr(Line, RSTART, RLENGTH);
      gsub(/variable/, SUBSEP, str);
      Line = substr(Line, 1, RSTART-1) str substr(Line, RSTART+RLENGTH);
   }
   gsub(/variable/, "200", Line);
   gsub(SUBSEP, "variable", Line);
   print Line;

Input:
Code:
/* test */
int another_variable;
int variable2;
int table [variable][variable];

printf(" variable  : ", variable) ;
printf(" variable2 :", variabl2) ;

Output:
Code:
/* test */
int another_variable;
int variable2;
int table [200][200];

printf(" variable  : ", 200) ;
printf(" variable2 :", variabl2) ;

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nawk Problem - nawk out of space in tostring on

Hi.. i am running nawk scripts on solaris system to get records of file1 not in file2 and find duplicate records in a while with the following scripts -compare nawk 'NR==FNR{a++;next;} !a {print"line"FNR $0}' file1 file2duplicate - nawk '{a++}END{for(i in a){if(a-1)print i,a}}' file1in the middle... (12 Replies)
Discussion started by: Abhiraj Singh
12 Replies

2. 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

3. 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

4. Shell Programming and Scripting

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: <HTML> <HEAD> <TITLE>{$String1}</TITLE> </HEAD> <BODY> <P>{$String2}</P> </BODY> </HTML>and the other one: {$String1}; french {$String2}; italian {$String3}; english ... {$StringN}; I... (3 Replies)
Discussion started by: heaven25
3 Replies

5. Shell Programming and Scripting

Nesting - two nawk into one nawk

hi people; this is my two awk code: nawk '/cell+-/{r=(NF==8) ? $4FS$5FS$6 : NF==7 ? $4FS$5 : $4 ;c=split(r,rr);for (i=1;i<=c;i++){if(rr != "111111"){printf($3" %d ""\n",(i+3))}}printf("")}' /home/gc_sw/str.txt > /home/gc_sw/predwn.txt nawk -F'*' '{gsub(/ *$/,"")}$0=$1$($NF-2)'... (2 Replies)
Discussion started by: gc_sw
2 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

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

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

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

10. 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
Login or Register to Ask a Question