LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-15-2008, 10:53 AM   #1
dina3e
Member
 
Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Rep: Reputation: 16
View the Selected lines of the file ??


How to view the selected line number of the file. Mean i have the see the line numbers 1,4,8,12,16.....so on .
i can do with sed but can i do with cat or other simple command .
how can i select this thing in vi /vim editor.

Last edited by dina3e; 10-15-2008 at 10:55 AM. Reason: Spell Mistake
 
Old 10-15-2008, 11:02 AM   #2
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Why do want to use some "simpler command" ?
IMHO you may end up using many cat/for/do..while loops when it could be easily done with sed/awk.

In the end, you may find using all these "simpler commands" quite complicated.

This is my opinion.

Regards,
Vikas
 
Old 10-15-2008, 11:33 AM   #3
McSlack
Member
 
Registered: Jul 2006
Location: Grand Rapids, MI
Distribution: Slackware
Posts: 44

Rep: Reputation: 15
Are you asking how to select a line number in vi? If so just type ":<line number> example ":10" displays line 10.
 
Old 10-15-2008, 11:40 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You may be interested in the nl command.
 
Old 10-17-2008, 10:20 AM   #5
dina3e
Member
 
Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by McSlack View Post
Are you asking how to select a line number in vi? If so just type ":<line number> example ":10" displays line 10.
:10 works fine in vi editor , But i need to see the alternate lines or some line sequence like line No.1,3,6,9,12,15,...etc Then is there any option for this in vi/vim editor.
 
Old 10-18-2008, 08:02 AM   #6
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
By "line sequence", do mean arithmetic progression? (Your example would then be 0, 3, 6, 9, 12, 15, ... or 1, 4, 7, 10, 13, 16 ...)

If so, GNU sed can do it -- see the "first~step" syntax at http://www.gnu.org/software/sed/manu...html#Addresses.
 
Old 10-18-2008, 09:38 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
You can certainly do such things with awk, which has a programming language built-in.

In this case... the "pattern" that would be used is a dummy, that matches all lines. You use the awk-maintained record counter variable, and the "modulo" operator, to determine whether to print the current record.

It's well worth remembering that Unix/Linux is loaded with tools like this, and programming-languages like perl. It pays to fool around with them.
 
Old 10-18-2008, 03:29 PM   #8
dina3e
Member
 
Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by archtoad6 View Post
By "line sequence", do mean arithmetic progression? (Your example would then be 0, 3, 6, 9, 12, 15, ... or 1, 4, 7, 10, 13, 16 ...)

If so, GNU sed can do it -- see the "first~step" syntax at http://www.gnu.org/software/sed/manu...tml#Addresses.
Thank's for the Above links.

As we can do this things in sed . Can i do same things in vi/vim without using sed?
 
Old 10-18-2008, 06:34 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
What exactly are you trying to achieve and why. It would make it easier for us to suggest appropriate answers/tools that way.
I suspect(!) that what you want is not possible in vi, but its not clear from your qn.
vi can only show you the contents of the file(s) you've opened. As said, you can go to any line, but if you only want to be able to see certain lines, you'd have to extract those lines and put them in another file.
 
Old 10-19-2008, 06:26 AM   #10
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
You're welcome, dina3e, it's always nice to be appreciated here (or elsewhere); BUT you still didn't answer my Q about what you mean by "line sequence". Also, please do answer chrism01's Q about your goal -- he is right about us helping you better if we know where you want to go.


For those those who are wondering, sed -n '0~3p' & sed -n '1~3p' would display the arithmetic progression line sequences mentioned above.
 
Old 10-19-2008, 08:34 AM   #11
dina3e
Member
 
Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Original Poster
Rep: Reputation: 16
As i like to work in vi/vim , so that i think is there any method available in vim that may process the filter's in the vim .
So that i asked such question & happy to get the answer.


I really Thank's to this forum from where i get lot of knowledge from other sites.
 
Old 10-19-2008, 10:28 AM   #12
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
OP-

In vim...
Code:
: set nu
And to turn off...
Code:
: set nonu
The 'nu' can be substituted with the full-word 'number'

If you want to go to a particular line number:
Code:
: <line-num>
If you want to delete (or perform other operation on a range of numbers)...
Code:
: <op> <line-begin>,<line-end>
I hope this answers your question.
 
Old 10-22-2008, 05:53 AM   #13
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
dwhitney67,
Can you do the every nth (arithmetic progression) thing in vim?

dina3e,
You still haven't answered my Q about whether it's really an arithmetic progression you are asking about. This is crucial to properly answering your Q.

2nd clarification: If all you want want to do is "view the selected line number of the file", why does it matter if you use sed or vim? Familiarity w/ one is not normally a good enough reason to shun the other. Is this part of a larger problem that you haven't mentioned? Please explain.
 
Old 10-22-2008, 06:40 AM   #14
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
Well, the vi command sequence "r! whatever" might prove useful.
 
Old 10-22-2008, 03:27 PM   #15
dina3e
Member
 
Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Original Poster
Rep: Reputation: 16
dina3e,
You still haven't answered my Q about whether it's really an arithmetic progression you are asking about. This is crucial to properly answering your Q.

2nd clarification: If all you want want to do is "view the selected line number of the file", why does it matter if you use sed or vim? Familiarity w/ one is not normally a good enough reason to shun the other. Is this part of a larger problem that you haven't mentioned? Please explain.[/quote]

you are correct . That does not matter if you use the vim/sed .

The arithmetic progression , mean let's i want to see the odd lines in vim . And there is no such problem also if i use sed .

Thank's for all this discussion.
 
  


Reply



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
Hw to clear the selected lines from History? linuxxx123 Linux - Newbie 6 01-08-2009 03:56 PM
getting selected lines in a file viveksnv Programming 9 02-28-2008 10:27 PM
LXer: XWiki vies for top spot in enterprise market LXer Syndicated Linux News 0 11-15-2007 09:00 PM
Copy selected with specified file extensions dickb Linux - Newbie 4 06-25-2005 02:43 PM
How to copy a selected content from a .PDF file satimis Linux - General 3 07-03-2003 04:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:20 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
Open Source Consulting | Domain Registration