LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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-27-2010, 06:24 AM   #1
sumeet inani
Member
 
Registered: Oct 2008
Posts: 895
Blog Entries: 26

Rep: Reputation: 48
hung up vim editor by searching \n


hi
I had opened a file in vim editor in my ubuntu 8.04 & wanted to see if line ends with \r\n (like DOS) or \r (in UNIX)
I tried
Code:
:%s/\n//gn
& vi editor got hung with processor nearing 50%
Then I pressed ctrl+c & vim resumed with message
Code:
147416605 matches on 1 line
So my question is
(1)why this reaction from vim ?
(2)I want to see al control characters in file . What should i do ?
I used notepad++ in windows & it had option to differentiate tabs,spaces,return etc.
 
Old 07-27-2010, 06:26 AM   #2
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
Quote:
Originally Posted by sumeet inani View Post
hi
I had opened a file in vim editor in my ubuntu 8.04 & wanted to see if line ends with \r\n (like DOS) or \r (in UNIX)
Well, this is a "side note", not a solution:
\r is MacOS line-ending, UNIX is \n.

Oh and vim already knows about these line-endings, you can query it with
Code:
:set fileformat?
edit: type ":help fileformat" if you want to know more

Last edited by zirias; 07-27-2010 at 06:29 AM.
 
Old 07-27-2010, 06:45 AM   #3
sumeet inani
Member
 
Registered: Oct 2008
Posts: 895
Blog Entries: 26

Original Poster
Rep: Reputation: 48
Thanks zirias
I ran
Code:
:set fileformat?
it said the file was dos & for another different file it said unix

the question that remains is
(1)vim hangs on searching ?
(2)Is it possible to tell vim to show all characters i.e tab should be apparent without navigating to that position ?
 
Old 07-27-2010, 06:56 AM   #4
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
Looking at your idea of "searching" .. you are applying a "replace" command and replace a line ending metacharacter with nothing. AFAIK, the replace command in vim will not change multiple lines into one, so this replacement is just without effect. You probably created an infinite loop, because it matches ALL THE TIME on the first line, not doing anything and starting again.

To just search (not replace), use the '/' command instead of ':s'.

For your question (2) -- no idea, never needed that
 
Old 07-27-2010, 07:00 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

This: :%s/\n//gn is incorrect and makes vim "hang". The g is also not needed. Correct would be: :%s/\n//

Using the fileformat option works as follows:
set ff=unix, set ff=dos or set ff=mac

After that command you save the file and the fileformat is set.

To get help for a specific option in vim do the following: :help fileformat

Searching for tabs: /<ctrl-v><tab> you end up with this: /^I (which is the tab).

But why use vi(m) to check the filetype, this would also work: file <infile>.

Hope this helps.

Last edited by druuna; 07-27-2010 at 08:39 AM. Reason: fixed a typo
 
Old 07-27-2010, 08:06 AM   #6
sumeet inani
Member
 
Registered: Oct 2008
Posts: 895
Blog Entries: 26

Original Poster
Rep: Reputation: 48
:%s/\n//gn
means % for all lines
g for counting multiple occurence in single line
'n' flag means no replacement just count matches
's' for substitution (occurs if 'n' flag absent)
 
Old 07-27-2010, 08:39 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,
Quote:
Originally Posted by sumeet inani View Post
:%s/\n//gn
'n' flag means no replacement just count matches
No it does not!!

From the OReilly Sed&AWK book (only the relevant part):
Quote:
s [address1[,address2]]s/pattern/replacement/[flags]
The following flags can be specified:
n
Replace nth instance of /pattern/ on each addressed line. n is any number in the range 1 to 512, and the default is 1.
Normally a end-of-line character (\n, \n\r etc) only occurs once in a line (at the very end). Looking for it globally (the g switch) in that line is generally not needed.

BTW: if you only want to see if a certain string exists, why not use: /<string> ?

Last edited by druuna; 07-27-2010 at 09:02 AM.
 
Old 07-27-2010, 09:01 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Been testing a bit with your problem.

First of all: the n option is not sed specific, this flag has a different meaning when used with vim (as you mentioned in post #6). This n flag works when not using the end-of-line characters.

But, like I stated in my previous post, if you are only interested in seeing if these eol chars exists, why not use /\n (a normal search, which does work)?

Hope this helps.
 
Old 07-27-2010, 09:47 AM   #9
sumeet inani
Member
 
Registered: Oct 2008
Posts: 895
Blog Entries: 26

Original Poster
Rep: Reputation: 48
I was actually counting number of occurences of \n in whole file.
You are right /\n will search the next newline but to count ex command is generally used

Last edited by sumeet inani; 07-27-2010 at 09:51 AM.
 
Old 07-27-2010, 09:57 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

The n flag (as seen from vim) is used with the sm command (not the s command).

This should work: :%sm/\n//n (no g!).

Hope this helps.
 
Old 07-28-2010, 01:19 AM   #11
sumeet inani
Member
 
Registered: Oct 2008
Posts: 895
Blog Entries: 26

Original Poster
Rep: Reputation: 48
i also tried
Code:
:%s/\n//n no global, no sm either(though that would also work)
it did as required so seems 'g' caused trouble.

i saw :help sm
Quote:
m stands for magic.
if magic is set then . and * have special meaning when used alone.
Thanks druuna for pointing out (1)futility of g option
(2):set ff=type_you_want

Man , you have to admit you quoted sed & awk while vim editor is different. Finally 'n' flag was what I said.

QUESTION that remains
is how to see all control characters in file like tab,\r,\n,etc.

Last edited by sumeet inani; 07-28-2010 at 01:43 AM.
 
Old 07-28-2010, 02:22 AM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
QUESTION that remains
is how to see all control characters in file like tab,\r,\n,etc.
man od



Cheers,
Tink
 
Old 07-28-2010, 02:31 AM   #13
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
Quote:
Originally Posted by sumeet inani View Post
is how to see all control characters in file like tab,\r,\n,etc.
You're probably looking for
Code:
:set list
To get back to normal, use
Code:
:set nolist
If you don't like the display, read
Code:
:help listchars
 
Old 07-28-2010, 04:12 AM   #14
sumeet inani
Member
 
Registered: Oct 2008
Posts: 895
Blog Entries: 26

Original Poster
Rep: Reputation: 48
Thanks zirias
i was looking for this.
 
Old 07-28-2010, 04:25 AM   #15
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,
Quote:
Originally Posted by sumeet inani View Post
Man , you have to admit you quoted sed & awk while vim editor is different. Finally 'n' flag was what I said.
Yep I did, sorry about that. I do hope you read my follow up post (#8).
Quote:
how to see all control characters in file like tab,\r,\n,etc.
Post #13 shows how that is done.

Hope this helps.
EDIT
Too late, you already replied
/EDIT
 
  


Reply


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
LXer: Personalize and Optimize Vim editor using Packtís new Vim 7.2 book LXer Syndicated Linux News 0 05-20-2010 11:20 PM
searching multiple patterns through Vi editor barunparichha Linux - Software 10 05-12-2009 09:39 PM
Editor comparison: vim VS vim-lite, Cleaning vim Ruler2112 *BSD 4 04-13-2009 05:26 PM
vim editor kosys Linux - General 6 12-31-2007 11:48 AM
vim editor shabbirhai Linux - General 5 03-02-2006 03:01 AM


All times are GMT -5. The time now is 01:45 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