omcr
asked on
regex to remove strange character from text file
Each line of text in log file I get from a device has a small rectangle at the begining, how do I get rid of it? Here is what it looks like, just imagine the zero's are small rectangles:
0SeqNum: 57
0Display: 1
0Point: 9
0CurrentState: Alarm
I have chomped, choped, tried to strip out with regexs, and have had no success.
0SeqNum: 57
0Display: 1
0Point: 9
0CurrentState: Alarm
I have chomped, choped, tried to strip out with regexs, and have had no success.
ASKER
That gave me this:
\x0dSeqNum: 81\x0d
\x0dDisplay: 1\x0d
\x0dPoint: 9\x0d
\x0dCurrentState: Alarm\x0d
\x0dSeqNum: 81\x0d
\x0dDisplay: 1\x0d
\x0dPoint: 9\x0d
\x0dCurrentState: Alarm\x0d
"\0xd" is a carriage return, was the log file generated in DOS?
perl -i.bak -pe 's/\xd//g' logfile
perl -i.bak -pe 's/\xd//g' logfile
ASKER
generated in unix.
Can you put "perl -i.bak -pe 's/\xd//g' logfile" into a file format instead of command line?
Can you put "perl -i.bak -pe 's/\xd//g' logfile" into a file format instead of command line?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you dump the file so we can identify the strange character?
od -s logfile
od -x logfile
If you are on unix, thr tr utility may be simpler. The first example removes all control characters. The second, all characters with octal value of 015
cat logfile | tr -d '[:control:]' > new_logfile
cat logfile | tr -d '\015' > new_logfile
od -s logfile
od -x logfile
If you are on unix, thr tr utility may be simpler. The first example removes all control characters. The second, all characters with octal value of 015
cat logfile | tr -d '[:control:]' > new_logfile
cat logfile | tr -d '\015' > new_logfile
ASKER
That did it. Thanks.
ASKER
Did not see your post before I commented last time teraplane, but ozo's suggestion cleaned it up, and my scripts are now working. Thanks.
while( <> ){
s/([^\n -z])/sprintf'\x%02x',ord $1/eg;
print;
}