Operating System - HP-UX
1752800 Members
5823 Online
108789 Solutions
New Discussion юеВ

sed to remove control M characfer

 
rhansen
Frequent Advisor

sed to remove control M characfer

Hello,
I am using the below sed code to remove the control M character from a file, however I am getting an error.

sed -e 's///g' $filename > $TMP/$filename
sed: 0602-410 The first regular expression cannot be null.

Any help would be appreciated.

Thanks.
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: sed to remove control M characfer

Hi:

You can use 'dos2ux' or Perl:

# dos2ux < file > file.new

# perl -pe 's/\r$//g' file > file.new

...or with an "inplace" update:

# perl -pi.old -e 's/\r$//g' file

Regards!

...JRF...

James R. Ferguson
Acclaimed Contributor

Re: sed to remove control M characfer

Hi (again):

In reality we can drop the 'g'lobal action from the one-liner Perl above, since the substitution is performed if there is a carriage-return ('\r') anchored to the end of a line. Thus there will only be one.

In HP's 'sed' you are relegated to something like:

# sed -e 's/^M//' file > file.new

...where the ^M is typed at the command line by pressing the "CONTROL v m" key sequence.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: sed to remove control M characfer

>JRF: where the ^M is typed at the command line by pressing the "CONTROL v m" key sequence.

Unfortunately you can't do that for ksh, I get control-J. You can do it in vi.

You can also use vi directly on the file or use tr(1).
James R. Ferguson
Acclaimed Contributor

Re: sed to remove control M characfer

Hi (again):

> Dennis: Unfortunately you can't do that for ksh, I get control-J.

That is interesting. I see this too if I switch to the 'ksh' shell. With the standard Posix shell the control+v+m sequence works fine at the command line.

I agree, rhansen could also do:

# tr -d "\015" < file > file.new

Regards!

...JRF...
Steven Schweda
Honored Contributor

Re: sed to remove control M characfer

> sed: 0602-410 [...]

It might be helpful if you would reveal the
particular AIX version where you're trying to
do the work. "uname -a" should say enough.

> You can use 'dos2ux' [...]

Perhaps not on AIX:

blue# man dos2ux
Manual entry for dos2ux not found or not installed.

blue# type dos2ux
-bash: type: dos2ux: not found