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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to remove embedded ^M from print jobs?

Status
Not open for further replies.

gbell

MIS
Feb 19, 2002
86
AU
Hi,

We are experiencing problems with embedded control characters in our EAM application Ellipse. It is not causing a problem with the online system but it is with CSV extracts and print jobs. I have been able to get around the problem on the CSV extracts using the UNIX function 'tr' before unblocking the file but do not know how to handle the print jobs. Printing from the application is handled by a Perl script, see part of code below. The problem is print jobs have a ^M at the end of the line so stripping out control characters will remove the valid ^M at eol. Can someone with more Perl experience than myself please suggest a way of stripping out embedded control character while maintaing the ^M at eol. The line length can be 80 or 132 chartacters.

Many thanks in advance.

if ($m_no == 0 ) { # very first report line
$m_line =~ s/^L//g;
if ($m_line !~ /^M\n/) {
$m_line =~ s/\n/^M\n/g;
}
print F_OUTFILE $m_line;
}
else { # subsequent lines of report
if ($m_line !~ /^M\n/) {
$m_line =~ s/\n/^M\n/g;
}
print F_OUTFILE $m_line;
};
 
The ^ is a metacharacter in regular expressions, which means "at the beginning of the string being matched" (or, inside character classes, a negation).

So $m_line =~ /^L//g; will only remove a character "L" from the very beginning of $m_line. If you want it to remove literally "^L", then escape it; $m_line =~ /\^\L//g;

So

Code:
if ($m_line !~ /\^M\n$/s) {
   $m_line =~ s/\n$/^M\n/g;
}

(note that ^ doesn't need to be escaped on the second part of the regexp, because that part is taken more literally and metacharacters don't affect it).

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Thanks for your help Kirsle. I applied the changes you suggested but it didn't make any difference. Perhaps on my initial post I didn't explain the situation clearly.

Control character, mainly ^M are getting into our EAM application Ellipse through the front end screens. A work order was raised with the vendor Mincom who were reluctant to do anything about it. As explaned earlier we can get around the problem with eft extracts using the UNIX 'tr' funtion to strip control characters.

Print jobs have ^M at the end of the line as expected, but some of the data that is printed has embedded ^M's in the data, i.e. there can be more than one ^M in a line.

How do I remove the extraneous ^M's whilst maintaining the ^M at the end of the line?
 
When you're reading through the file, does perl correctly read each line? Could you remove all the ^M characters and just add one back at the end of each line?

I believe ^M is the carrage return character, so it might be hard to identify a valid vs. a non-valid new line. You mentioned the lines either being 80 or 132 characters - is there any way of identifying which it is based on the data on that line?
 
Hi rharsh,

There is a line of code in the script to get the line length - $m_len = length($m_line);

Your idea of removing all the ^M's then adding one back at the end of the line sounds promising but I don't know how to do it - my Perl skills are limited. Any suggestions on how to do this would be welcome,
 
Code:
  if($m_line=~/\^M$/){
    chomp$m_line;
    $m_line=~s/\^M//g;
    $m_line.="^M\n";
  }else{
    $m_line=~s/\^M//g;
  }
If you know that all the lines have an ending ^M char, then remove the if retaining only the first condition.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top