LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-12-2013, 02:59 AM   #1
xeechou
Member
 
Registered: Jun 2013
Location: China
Distribution: Archlinux
Posts: 67

Rep: Reputation: Disabled
linux windows end of line


I'm learning regular expression, and I feel quite confused about it, every body knows that in grep I can use '^[0-9]' to match a line start with number, and '[0-9]$' to much a end.
The book I read said that Windows use ^M$ for his end of line, so I guess if I write some txt file in win then copy to linux,I must can see the ^M, but when I use vim to open it, Oops, IT IS JUST AS SAME AS A REGULAR LINE. but how?
Thanks for your advice!
 
Old 07-12-2013, 03:10 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
If you run "file <myfilename.txt>" then it'll tell you what format it's written in. Vi can show windows format files "correctly" like you may be seeing, but it's not usually a default, you can tune the behaviour though:

http://vim.wikia.com/wiki/File_format
 
Old 07-12-2013, 03:11 AM   #3
shm0
Member
 
Registered: Aug 2012
Location: Bahrain
Distribution: Slackware
Posts: 58

Rep: Reputation: 16
Vim can deal with those file formats according to http://vim.wikia.com/wiki/File_format.
 
Old 07-12-2013, 03:27 AM   #4
xeechou
Member
 
Registered: Jun 2013
Location: China
Distribution: Archlinux
Posts: 67

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by acid_kewpie View Post
If you run "file <myfilename.txt>" then it'll tell you what format it's written in. Vi can show windows format files "correctly" like you may be seeing, but it's not usually a default, you can tune the behaviour though:

http://vim.wikia.com/wiki/File_format
It dose tell me the file is ASCII text,with CRLF line terminators
Does cat show windows format too?

Well, as I use :e ++ff=unix, I saw the ^M at the end of line, but I start wondering that if there is truly a '$' at end of line, If I just type '$' there, does it mean VIM will automatically convert it to '\$'

Last edited by xeechou; 07-12-2013 at 03:36 AM.
 
Old 07-12-2013, 03:34 AM   #5
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,404

Rep: Reputation: Disabled
Quote:
Originally Posted by xeechou View Post
It dose tell me the file is ASCII text,with CRLF line terminators
Does cat show windows format too?

Anyway how can I see the difference between unix and windows?
The difference is the line terminators. Windows uses two characters, Carriage Return + Line feed (CRLF), while Unix uses Line Feed (LF).

The file command reports the type of line terminators used in the file. If you want to actually see the difference, use an editor that doesn't try to adapt to the format, like joe on Linux or Notepad on Windows. joe shows the CR control character as a "^M" at the end of every line. Notepad shows LF line terminated files as a single line.

I don't think cat-ing a file with CRLF line terminators would show the difference. After all, CR is not a visible character.
 
Old 07-12-2013, 03:55 AM   #6
xeechou
Member
 
Registered: Jun 2013
Location: China
Distribution: Archlinux
Posts: 67

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Ser Olmy View Post
The difference is the line terminators. Windows uses two characters, Carriage Return + Line feed (CRLF), while Unix uses Line Feed (LF).

The file command reports the type of line terminators used in the file. If you want to actually see the difference, use an editor that doesn't try to adapt to the format, like joe on Linux or Notepad on Windows. joe shows the CR control character as a "^M" at the end of every line. Notepad shows LF line terminated files as a single line.

I don't think cat-ing a file with CRLF line terminators would show the difference. After all, CR is not a visible character.
Another test I just did was that I used ":e ++ff=mac" in vim, then I see ^J at the head of line, what dose ^J Means?
As ^M means \r, LF means \n.

I remember that in C programs, you just use \n to put an end, so what is that ^J?
 
Old 07-12-2013, 04:04 AM   #7
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,404

Rep: Reputation: Disabled
The ^ prefix means it's a control character. After the circumflex you see an upper case character corresponding to the actual number of the control character, where @ is 0, A is 1 and so on.

^J would be ASCII control character 10, which is Line Feed. It seems that the mode you selected interprets CR as the line terminator, and the LF ended up as the first character of the next line.
 
Old 07-12-2013, 04:12 AM   #8
xeechou
Member
 
Registered: Jun 2013
Location: China
Distribution: Archlinux
Posts: 67

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Ser Olmy View Post
The ^ prefix means it's a control character. After the circumflex you see an upper case character corresponding to the actual number of the control character, where @ is 0, A is 1 and so on.

^J would be ASCII control character 10, which is Line Feed. It seems that the mode you selected interprets CR as the line terminator, and the LF ended up as the first character of the next line.
Damn it, I should realize that.
 
  


Reply

Tags
regular expression


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
End-of-line Characters missing from last line of md5 file. Md5sum fails mehorter Linux - General 5 06-29-2009 09:56 PM
Attempting to append a line of text to the end of the previous line market_garden Linux - General 4 12-11-2008 12:37 PM
LXer: Linux Distributor Interactive Ideas To Deliver Xandros' End-to-End Windows Alternatives To UK LXer Syndicated Linux News 0 09-16-2006 01:33 PM


All times are GMT -5. The time now is 04:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration