Need help in scripting

Hi,

I am trying to find a script that will help me in replacing ^M if present in file with new line if new line is not present after ^M else leave the file as it is.

Can someone help me with the scripts.

I know it can be done using sed command but it is even changing the files which does not have ^M.

So what have you tried so far?

If "^M" does not exist how can sed change anything? We need to see what you used to understand ...

sed 's/'"$(printf '\015')"'/\
/g' $1 > $2

I am using above sed command.

The above command is changing the file which does not have ^M and putting a blank entry in between rows for all dat athat is present in file

That's a bit difficult to believe - sed wouldn't s ubstitute anything if no ^M char were present. At least it doesn't on my linux nor FreeBSD systems.
So - please post your OS and sed version, and a representative sample of your input file.
And, be aware of

  • a ^M can be used in e.g. spread sheet programs to signify an intra-cell line break.
  • a ^M can be used as a line terminator in certain systems.
  • there are easier / more straightforward ways to remove the ^M char in text files.

HI, My file data is as below:-

ID,Status,parentid,status_new,Locations,date
4373,Complete,41,Active,,
4374,Complete,41,Active,,
435,Complete,41,Active,,

when i use the command that i mentioned it is changing the file into like below: - One additional space is getting added at between each row

ID,Status,parentid,status_new,Locations,date

4373,Complete,41,Active,,

4374,Complete,41,Active,,

435,Complete,41,Active,,

Well, I strongly believe that every single line has a ^M at line end, then. Your script is changing the ^M^J ( <CR><LF> ) sequence to ^J^J ( <LF><LF> ) causing the extra line feed...

Try sed 's/\015//' file or tr -d '\r' <file instead - if your system (which you fail to mention) provides either.

that is not the case.
if my file had ^M in all csv files, i culd see that in UNIX when i use vi command but that is not the case.

The file data i gave earlier does not have any ^M characters.
I have other file which has data like below

ID,Status,Name^M10000026,Active,INC^M10000177,Inactive,APCONINC^M

so the command i gave works fine for this but not for the one i gave earlier.

FYI.. the command you gave is not working for the file data that i am giving in this response