PDA

View Full Version : removing stuff from the end of a line


Danger Fan
07-24-2002, 07:28 PM
I do some of my developing on windows machines. When I move the files over to the unix machine, no matter what editor I use, there's always a '^M' at the end of every line. I wrote a quick perl script, first using s///, then chomp to try to remove it. But it doesn't work.

Now, that's ctrl-M at the end of the line, correct? Not the characters '^''M', right? If anyone could give me a bit of help, I would appreciate it. Thanks.

inkedmn
07-24-2002, 07:49 PM
i've heard of that, i think java does the same thing...

not sure how to get rid of it though :(

Strike
07-24-2002, 08:43 PM
Well, the characters don't affect the files any except for when you are viewing them in an editor, usually. If you use vim for your editor, simply do :set fileformat=unix and it will fix them (assuming it automatically detected that it was a dos fileformat to begin with.

Danger Fan
07-24-2002, 08:51 PM
Originally posted by Strike
Well, the characters don't affect the files any except for when you are viewing them in an editor, usually. If you use vim for your editor, simply do :set fileformat=unix and it will fix them (assuming it automatically detected that it was a dos fileformat to begin with.

I tried it, but it didn't work :( . The only reason I care, is because my professor will be looking at the files, and I want them to be as neat as possible.

Bradmont
07-24-2002, 09:29 PM
again in vim, try:

:%s/^M//g

to get the ^M, press ctrl+v, <return>

that should do it

bmoyles
07-24-2002, 09:37 PM
The ^M is a Windows EOL terminator
UNIX uses a LF char
Macs use a CR
Windows uses CRLF
When you search for the chars in the file, you don't want to search for ^M, but \r\n (in unix, LF is \n, CR is \r)
so something like s/\r\n/\n/g should do it.
Or, most unix systems have dos2unix (unix2dos) installed. See if that's around.

Danger Fan
07-24-2002, 11:18 PM
Thanks guys. Bradmont: worked like a charm :D

me does happy dance

sans-hubris
07-25-2002, 03:14 AM
TMTOWTDI

From the command line:
tr -d '\r' < filename | tee filename

TheLinuxDuck
08-07-2002, 11:28 AM
We had this problem on CCAE when letting people upload files.

l2kashe
08-21-2002, 02:57 PM
you could also chop the line...

while(<HANDLE>) {
chop($_);
push(@new,$_);
}

close(HANDLE);
open(HANDLE ,">some_file");

foreach (@new) {
print HANDLE "$_\n";
}
close(HANDLE);