gsub in Awk to capture count of replaced characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting gsub in Awk to capture count of replaced characters
# 1  
Old 08-27-2010
gsub in Awk to capture count of replaced characters

Hi ,

I am working on a script to replace special characters in ASCII file with '?'.

We need to get count of replaced characters from file. I am new to Awk and i read,

# The gsub function returns the number of substitutions made.

I was trying to replace characters with below code,
Code:
       gsub("[^[:alnum:][:punct:][:blank:]]","?",$0)

but i am unable to capture the count of replaced characters.

Can anyone help me with the code which will capture the count of replaced characters with above command.

I am kind of in hurry... Thanx in advance.

Moderator's Comments:
Mod Comment Use code tags please, ty.
# 2  
Old 08-27-2010
Code:
print gsub("[^[:alnum:][unct:][:blank:]]","?",$0)

# 3  
Old 08-27-2010
Thanx for your quick reply.

That worked.

Can i catch it in a variable ? That would be easier for me.
# 4  
Old 08-27-2010
Sure:
Code:
x=gsub("[^[:alnum:][unct:][:blank:]]","?",$0)

# 5  
Old 08-27-2010
This example write the output to a new file.
After that it prints the number of replaced characters and assigns it to the variable var:
Code:
var=$(awk '{
  .
  count+=gsub("[^[:alnum:][unct:][:blank:]]","?",$0)
  print $0 > "newfile"
  .
}
END{print count}
' file)

# 6  
Old 08-27-2010
Quote:
Originally Posted by bartus11
Sure:
Code:
x=gsub("[^[:alnum:][unct:][:blank:]]","?",$0)


If this works , how can i print values. I had tried this option already but i couldn't print value. I tried like

count=gsub("[^[:alnum:][unct:][:blank:]]","?",$0)
then print $count
print $'$count'
etc ,

sorry , if i am asking simple Qs ,but it wasted my whole day..
# 7  
Old 08-27-2010
Code:
count=gsub("[^[:alnum:][unct:][:blank:]]","?",$0); 
print count

In AWK most variables are not preceded with "$".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace characters in string with awk gsub

Hi I have a source file that looks like a,b,c,d,e,f,g,h,t,DISTI(USD),MSRP(USD),DIST(EUR),MSRP(EUR),EMEA-DISTI(USD),EMEA-MSRP(USD),GLOBAl-DISTI(USD),GLOBAL-MSRP(USD),DISTI(GBP), MSRP(GBP) I want to basically change MSRP(USD) to MSRP,USD and DIST(EUR) to DIST,EUR and likewise for all i'm using... (3 Replies)
Discussion started by: r_t_1601
3 Replies

2. Shell Programming and Scripting

awk script to count characters in file 1 in file 2

I need a scripting AWK to compare 2 files. file 1 and 2 are list of keywords 1 is a b c d 2 is aa aaa b bb ccc d I want the AWK script to give us the number of times every keyword in file 1 occurs in file 2. output should be a 2 (7 Replies)
Discussion started by: anhtt
7 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

using sed or gsub to substitute characters!

Is there a way to substitute the URL-encoding references of ( & and ` ) with their actual appearance? for example.... %26 is & say I want to convert every %26 in my file to &..... awk '{gsub(/%26/,"&");print}' Is there a way to do this? I also want to be able to convert ` too! (3 Replies)
Discussion started by: puttster
3 Replies

5. Shell Programming and Scripting

awk next time replaced by none

Hi Everyone, 1.txt a b c d e f d g gg output: abc de (3 Replies)
Discussion started by: jimmy_y
3 Replies

6. Shell Programming and Scripting

awk count characters, sum, and divide by another column

Hi All, I am another biologist attempting to parse a large txt file containing several million lines like: tucosnp 56762 T Y 228 228 60 23 .CcCcc,,..c.c,cc,,.C... What I need to do is get the frequency of periods (.) plus commas (,) in column 9, and populate this number into another... (1 Reply)
Discussion started by: peromhc
1 Replies

7. Shell Programming and Scripting

awk help needed in trying to count lines,words and characters

Hello, i am trying to write a script file in awk which yields me the number of lines,characters and words, i checked it many many times but i am not able to find any mistake in it. Please tell me where i went wrong. BEGIN{ print "Filename Lines Words Chars\n" } { filename=filename + 1... (2 Replies)
Discussion started by: salman4u
2 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. UNIX for Advanced & Expert Users

Special characters getting replaced by &Pound in Unix Environment

Hi, Please find the Question Summary below- In our email template document(.txt) bullets and Apostrophe are getting replaced by the string "&pound" in our Live environment.We are using sun solaris 8 in live. Can anybody let me know why this happens and how to prevent this . Thanks... (0 Replies)
Discussion started by: kaushik05
0 Replies
Login or Register to Ask a Question