LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-04-2011, 11:54 AM   #1
Stephanie Seney
LQ Newbie
 
Registered: Aug 2011
Posts: 6

Rep: Reputation: Disabled
Question Weird behavior with grep - getting ^M in output when they are not in the file


Every once in a while a script we run to check a file for errors via grep will yield a ^M at the end of the grep output.

This is a portion of the script that runs:
# If error log exists move to olderr
if [ -a $LOG/handylog.errors ]
then mv -f $LOG/handylog.errors $LOG/handylog.olderr
fi
# Look for errors in log file
grep -i 'ERROR' $DB/handy.lg > $LOG/handylog.errors
grep 'flush' $DB/handy.lg >> $LOG/handylog.errors
grep 'core' $DB/handy.lg >> $LOG/handylog.errors
grep 'errno' $DB/handy.lg >> $LOG/handylog.errors
grep 'dbDelUsrctl' $DB/handy.lg >> $LOG/handylog.errors
grep 'SemLockLog' $DB/handy.lg >> $LOG/handylog.errors
grep 'disk space' $DB/handy.lg >> $LOG/handylog.errors
grep 'ABNORMAL' $DB/handy.lg >> $LOG/handylog.errors
#grep 'HANGUP' $DB/handy.lg >> $LOG/handylog.errors
grep "(1154)" $DB/handy.lg|grep "No servers are available." >> $LOG/handylog.errors
grep -i 'timeout' $DB/handy.lg >> $LOG/handylog.errors
#
for i in handylog
do
if [ -s $LOG/$i.errors ]
then
sort -u -o $LOG/$i.errors $LOG/$i.errors
if [ -s $LOG/$i.olderr ]
then
comm -13 $LOG/$i.olderr $LOG/$i.errors > $LOG/$i.txt
else
cat $LOG/$i.errors > $LOG/$i.txt
fi
fi
if [ -s $LOG/$i.txt ]
then
cp -p $LOG/$i.txt $LOG/$i.txt$J
pro -b -p DBAmailf.p -param "HANDY errors,See attached,$LOG/$i.txt,$LOG/$i.txt" > $LOG/checklogs.out 2> $LOG/cklogs.err
fi
done
...

The problem is that there is no ^M in the log file of which we grep...but the ^M appears only once in a while. Once it appears, then the ^M starts appending to the same line output each time the script runs. (This runs multiple times a day). Basically this script will search a DB log file for specific errors/warnings that we want to be aware of before they become problems. Each time it runs the same errors from the past grep could be found again - so we compare what was last found to what we find now - and only email the differences. So we only get new errors. However, when ^M starts appearing - the same error is sent over and over because the diff of the 2 files shows ^M on one line and ^M^M on the new file.

The odd thing is that if we delete the .txt file that has the ^M, and run the script again, no ^M appears...

We are running SuSe 11 on Linux. We've not been able to duplicate this issue at will...any thoughts as to why the ^M would appear when it's not in the original log file?
 
Old 10-04-2011, 12:21 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

1. Seeing "^M" is common in a file that was generated on DOS or Windows. Perhaps you have a Windows share, or are copying files from a Windows PC?

2. Many programs, by default, will "hide" ^M from you. Perhaps it's there, but you're just not seeing it (at least some of the time)?

3. If it's not causing any harm, you might simply choose to ignore the ^M.

Otherwise, there are many ways to convert your files from DOS text to Linux. For example:

* dos2unix

* http://www.cyberciti.biz/faq/howto-u...x-text-format/

* AbiWord, Open Office, Libre Office, Kate, vi, etc. etc.

'Hope that helps .. PSM
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using grep to output non-matches into output file binny959 Linux - General 5 12-24-2010 11:20 PM
$$ variable as GREP pattern: "weird" behavior courteous Linux - Newbie 4 12-12-2010 09:29 AM
[SOLVED] CVS + grep output to file Myiagros Linux - Newbie 2 05-20-2010 10:30 AM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM
output to a file - cat? grep? Godsmacker777 Linux - Newbie 6 12-08-2004 10:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration