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
# 8  
Old 08-27-2010
Quote:
Originally Posted by Franklin52
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)

This can be very helpful to me.

But its not workng in ksh script i put. I removed those 2 '.'s from above code.


Quote:
#!/bin/ksh
FileName=XXX.txt
lines=`wc -l $FileName | cut -f1 -d' '`
echo "lines :: $lines"
i=0
count=0
var=$(awk '{

count+=gsub("[^[:alnum:][unct:][:blank:]]","?",$0)
print $0 > "newfile"

}
END{print count}
' XXX.txt)
echo $var
Any changes required?
# 9  
Old 08-27-2010
Should work IMHO, did you get errors? Does the file newfile exist?
This User Gave Thanks to Franklin52 For This Post:
# 10  
Old 08-27-2010
Code:
shopt -s extglob
exec 6<"file"
total=0
while read -r LINE<&6
do
    olen="${#LINE}"
    LINE=${LINE//[^[:ascii:]]/}
    len="${#LINE}"
    total=$((olen-len+total))
done
exec 6<&-
echo "Total substitution: $total"

# 11  
Old 08-28-2010
Quote:
Originally Posted by Franklin52
Should work IMHO, did you get errors? Does the file newfile exist?
I am getting output like this , no warning/error. Output file is created , but with origional record only , no replacement.

Quote:
/abc/dtg/FinalAwk.sh
lines :: 1
0
/abc/dtg/$
Plz try to figure out exact error....

---------- Post updated at 12:06 PM ---------- Previous update was at 11:45 AM ----------

Sry , the code works perfect .. It was a characater missed by me.

thanks you very much Franklin ....
SmilieSmilie
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