[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

regex to remove strange character from text file

Posted on 2006-03-31
8
Medium Priority
?
490 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:omcr
  • 4
  • 3
8 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 16345426
what do you see if you do
while( <> ){
  s/([^\n -z])/sprintf'\x%02x',ord $1/eg;
  print;
}
0
 

Author Comment

by:omcr
ID: 16346192
That gave me this:
\x0dSeqNum: 81\x0d
\x0dDisplay: 1\x0d
\x0dPoint: 9\x0d
\x0dCurrentState: Alarm\x0d
0
 
LVL 85

Expert Comment

by:ozo
ID: 16346263
"\0xd" is a carriage return, was the log file generated in DOS?
perl -i.bak -pe 's/\xd//g' logfile
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:omcr
ID: 16346755
generated in unix.
Can you put "perl -i.bak -pe 's/\xd//g' logfile" into a file format instead of command line?
0
 
LVL 85

Accepted Solution

by:
ozo earned 240 total points
ID: 16346799
#!/usr/bin/perl
{local @ARGV=<*.log>;
 local $I^='';
 while( <> ){
    s/\xd//g;
    print;
 }
}
0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 16346870
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
0
 

Author Comment

by:omcr
ID: 16346875
That did it. Thanks.
0
 

Author Comment

by:omcr
ID: 16346899
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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question