Operating System - HP-UX
1827468 Members
2874 Online
109965 Solutions
New Discussion

Re: how to view the commented line in vi file

 
SOLVED
Go to solution
rajesh73
Super Advisor

how to view the commented line in vi file

pls find the below vi file.

example:
vi sudoers
1.# Cmnd alias specification
2.STORE /usr/sbin/frestore, /usr/sbin/backup
3.UMOUNT = /usr/sbin/umount
4.MOUNT = /usr/sbin/mount
5.# Host alias specification


the above example i want to view the line number 2,3,4 .because i want to see the uncommented line in the vi file

pls help me
17 REPLIES 17
Steven Schweda
Honored Contributor

Re: how to view the commented line in vi file

> how to view the commented line in vi file

What is "vi file"? A plain-old text file
which was created by "vi"?

> [...] i want to view [...]

Can't you see them now?

Do you mean something like this?:

alp$ grep -v '^#' 1483561.txt
STORE /usr/sbin/frestore, /usr/sbin/backup
UMOUNT = /usr/sbin/umount
MOUNT = /usr/sbin/mount

man grep
rajesh73
Super Advisor

Re: how to view the commented line in vi file

hi thanks for your replay .

my request is

in this sudoers file ,i want to view only uncommneted line, how to view
g3jza
Esteemed Contributor
Solution

Re: how to view the commented line in vi file

#grep -v '#' /etc/sudoers
rajesh73
Super Advisor

Re: how to view the commented line in vi file

thanks for replay.

Steven Schweda
Honored Contributor

Re: how to view the commented line in vi file

> #grep -v '#' [...]

Ok, if you want to reject any line with a "#"
in it anywhere. '^#' is a little more
selective.

> [...] only uncommneted line [...]

Define "uncommneted line". Is
/util/search.sh '###' > /tmp/fred.out
an "uncommneted line"?

Everything's complicated, and a vague problem
statement can lead to a sloppy "solution".


> Jun 10, 2011 04:42:59 GMT 8 pts

What? Did I use the wrong file name?
g3jza
Esteemed Contributor

Re: how to view the commented line in vi file

Steven:
I haven't seen any standard unix configuration files, which would NOT have the '#' at the beginning of the line :) , that's why I'm always using -v '#' .

Steven Schweda
Honored Contributor

Re: how to view the commented line in vi file

> I haven't seen any standard unix
> configuration files, which would NOT have
> the '#' at the beginning of the line :) ,
> that's why I'm always using -v '#' .

The original question said "vi file"
(whatever that means), not "standard unix
configuration files". However, let's look at
the example of "sudoers". I'm not a big sudo
user, so I know nothing, but a quick Google
search found some documentation which
suggests that "sudoers" might contain
notation like, say,
#include /etc/sudoers.local
which would make this task a little more
chancy. Also, it appears that one is allowed
to use "#" to add a comment to the end of a
normal line, and it seems to be a part of the
"sudoers" syntax, as in "#uid" or "%#gid".

So, discarding any line which contains "#"
anywhere would seem in this example to risk
losing considerable useful information.

If you would like to expand "^#" to include
white space before "#" (and/or "#include" at
the beginning of a line), then I wouldn't
complain.

> Everything's complicated, [...]

A simple solution to a problem is normally
better than a more complicated one, unless
that simpler "solution" doesn't actually
solve the problem. (Of course, having a
clear and complete definition of the problem
is helpful, but this is often difficult to
obtain.)
點燃
Valued Contributor

Re: how to view the commented line in vi file

saludos,

Do a grep -v for all lines starts with # which can be identified using "^#" in the output of the cat command.

cat /etc/sudoers |grep -v "^#"

All lines starts with a # will be eliminated in the output.

todo lo mejor
Man's mind, once stretched by a new idea, never regains its original dimensions
Steven Schweda
Honored Contributor

Re: how to view the commented line in vi file

> cat /etc/sudoers |grep -v "^#"

Why didn't _I_ think of that? No, wait...

Note that the "cat" is not needed.
Hiren N Dave
Valued Contributor

Re: how to view the commented line in vi file

#grep -v '#' /etc/sudoers
I am an HP Employee

Was this post useful? - You may click the KUDOS! star to say thank you.
Steven Schweda
Honored Contributor

Re: how to view the commented line in vi file

Hiren N Dave> #grep -v '#' /etc/sudoers

Have you considered the benefits of reading
the previous replies before duplicating one
of them?
Hiren N Dave
Valued Contributor

Re: how to view the commented line in vi file

Yes, you are right. The credit should go to g3jza.
I am an HP Employee

Was this post useful? - You may click the KUDOS! star to say thank you.
g3jza
Esteemed Contributor

Re: how to view the commented line in vi file

well, actually the credit should go to Steven, he got the most complete solution
Bill Hassell
Honored Contributor

Re: how to view the commented line in vi file

This is probably the most complete and simplest answer:

awk NF /etc/sudoers | grep -v ^[[:space:]]*#

This removes all blank or space-only lines and any line that has # as the first non-space character on the line. (non-space really means white space which includes tabs).

Since it is very common to want non-comment lines filtered out of config files, I wrote the NOcomment script (attached).


Bill Hassell, sysadmin
Steven Schweda
Honored Contributor

Re: how to view the commented line in vi file

> This is probably the most complete and
> simplest answer:
> [...]

Define "probably" (or "simplest").

With one program ("grep") instead of two:

alp$ grep -E -v '^[[:space:]]*#|^[[:space:]]*$' 1483561.txt
STORE /usr/sbin/frestore, /usr/sbin/backup
UMOUNT = /usr/sbin/umount
MOUNT = /usr/sbin/mount

alp$ cat 1483561.txt
# Cmnd alias specification
STORE /usr/sbin/frestore, /usr/sbin/backup
UMOUNT = /usr/sbin/umount

# Comment with leading space.
MOUNT = /usr/sbin/mount

# Host alias specification
James R. Ferguson
Acclaimed Contributor

Re: how to view the commented line in vi file

Hi:

Instead of *two* processes as in:

# awk NF /etc/sudoers | grep -v ^[[:space:]]*#

...do it all with *one* process:

# awk 'NF && ! /^[[:space:]]*#/' /etc/sudoers

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: how to view the commented line in vi file

Or create an alias so you have a new command:

alias NOcomment="awk 'NF && ! /^[[:space:]]*#/'"

Works with one or more files as well as stdin.
Thanks JRF


Bill Hassell, sysadmin