LinuxQuestions.org
Help answer threads with 0 replies.
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, 11: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 11:55 AM. Reason: Spell Mistake
 
Old 10-15-2008, 12:02 PM   #2
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,300

Rep: Reputation: 102Reputation: 102
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, 12:33 PM   #3
McSlack
Member
 
Registered: Jul 2006
Location: Grand Rapids, MI
Distribution: Slackware
Posts: 42

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, 12:40 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You may be interested in the nl command.
 
Old 10-17-2008, 11: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, 09: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, 10:38 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 8,606
Blog Entries: 4

Rep: Reputation: 2997Reputation: 2997Reputation: 2997Reputation: 2997Reputation: 2997Reputation: 2997Reputation: 2997Reputation: 2997Reputation: 2997Reputation: 2997Reputation: 2997
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, 04: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, 07:34 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
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, 07: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, 09: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, 11:28 AM   #12
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

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, 06: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, 07: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, 04: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


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

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

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