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

Problem with sed command

Status
Not open for further replies.

ganeshmb

Programmer
Dec 5, 2001
32
US
I am using the following sed command to knock off <Ctl> M characters(ASCII 13 characters)
in text files.

cat input.txt | sed s/^M//g > output.txt

where ^M character is generated by typing <Ctl>V followed by a <Enter>.

It is removing Ctl M characters successfully, but the only
problem is that it is also removing the last character in the text file
if there are no new line characters after it.


For example, the following text where } is the last character
WITHOUT a new line after it,

Code:
package com.test.mediator;^M
^M
public abstract class AbstractMediator implements MediatorIf {^M
}


gets converted into,


Code:
package com.test.mediator;

public abstract class AbstractMediator implements MediatorIf {


Note that not only ^M characters, but also the last character { has been removed.

Any idea why this is happening?
 
sed get confused with '\n',
if you only need to cut ^M out of your file
on Solaris: man dos2unix

by the way, you do not need cat
sed -e sed-cmds input >output
sed -f sed-script input >output
is enougth
 
Thank you very much jamaisar!
dos2unix -ascii <filename>
did the job for me.

Please note that a simple dos2unix <filename> has the same problem, i.e. it is knocking off the last character in the file.
Any explanation for this weird behaviour? Just curious!
 
if you definitively want solve the problem,
--------------------------
vi filename
:%s/^M$//g
:x
-------------------------

to enter ^M: press ^V^M


or put in cutctrlm.c
---------------------------------------cut here

#include <stdio.h>
#define MaxBuff 256

int main(int argc,char **argv)
{
int dothejob(FILE *,FILE *,char *);
char buff[MaxBuff+1];
FILE *in = stdin, *out = stdout;

if(!--argc) exit(dothejob(in,out,buff));

for(++argv; *argv; ++argv){
if(!(in = fopen(*argv,&quot;r&quot;)) continue;
sprintf(buff,&quot;%s.new&quot;,*argv);
if(!(out = fopen(buff,&quot;w&quot;)) exit(printf(&quot;cannot write %s, exiting\n&quot;,buff));
dothejob(in,out,buff);
fclose(in); fclose(out);
}

exit(0);
}
int dothejob(FILE *in, FILE *out, char *buff)
{
/* ^M has ascii value 10 */
char *ppp, *qqq;
while(fgets(buff,MaxBuff,in)){
for(ppp = qqq = buff; *ppp; ++ppp) if(*ppp != 10 && *ppp != '\n') *qqq++ = *ppp;
*qqq = 0;
fprintf(out,&quot;%s\n&quot;,buff);
}
return(0);
}
-----------------------------
compile this code: (cc or gcc) -o cutctrlm cutctrlm.c
then run: cutctrlm filename
you get a filename.new




 
*note to SCO users, man dtox in the same situation ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top