Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing special chars using AWK

Status
Not open for further replies.

uksnowman

Technical User
Apr 2, 2001
4
CH
Hi,

I am trying to remove all instances of the special backspace or delete chars in a log file field. The backspace shows up as ^H in the field.

Currently I use gsub(/["^H"]/, "") to remove the ^H but ideally I also want to delete the previous char. i.e. someone enters gg^H^Hhello (hello). I want to delete the first two chars also. Can someone let me know how I can do this? I imagine a for loop with if statement to check each char in the field.

I would rather not step through each char in this field on every line of the log file as it would take forever. If it's possible to remove the previous char upon detection of the backspace, that would be perfect.

Secondly, I use the statement
END { print NR, "sessions read." }
to display the total no of lines read from the log file. Since I am deleting certain lines, how do I keep a track of the modified no. of lines?? Again some king of for loop??

Hope you can help!!
snowman
 
uksnowman-

This little snippet seems to do the trick.

Modify for your use as required.


awk 'BEGIN{FS=OFS=","}

{
while($3 ~ /\010/) {
sub(/.\010/,"") # any char followed by bs = null
$3 = $3 # reassign new contents
if($3 !~ /\010/) ++num # no more - then, count line
}
print

}END{print NR, "Sessions read."
print num,"Modifications made."}' $1 > $2

Hope this helps you!


flogrr
flogr@yahoo.com

 
can't you use strings?
It remove all special chars.

Regards Gregor.

Gregor.Weertman@mailcity.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top