LinuxQuestions.org
Register a domain and help support LQ
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 05-16-2008, 12:49 AM   #1
wearetherock
LQ Newbie
 
Registered: Jan 2008
Posts: 9

Rep: Reputation: 0
How to show only uncomment line in the file.


Can anyone tell my, How to list file content and that line not contain # begin, thank.
 
Old 05-16-2008, 01:03 AM   #2
helgiks
LQ Newbie
 
Registered: May 2008
Posts: 13

Rep: Reputation: 0
$ cat <file> | grep -v "^ *#"
or
$ grep -v "^ *#" <file>

this should do the trick, or sed ...

$ sed '/^ *#/d' <file>

This also ignores leading spaces, removes a line that starts with *any* number of spaces than a comment. If you don't want that you can remove the star from all those line.

The "^" means a start of line, the star means zero or more characters, so " *#" means a start of line with zero or more spaces followed by "#". The "-v" in grep tells grep to remove those lines, and the d in sed tells sed to delete those lines. So if you only like to see those line you could leave out the "-v" in grep.

Hope that helps.

Last edited by helgiks; 05-16-2008 at 01:19 AM.
 
Old 05-16-2008, 01:09 AM   #3
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 74
Maybe something like grep may help e.g.
Code:
$grep -i ^[A-Z] somefile
 
Old 05-16-2008, 01:37 AM   #4
wearetherock
LQ Newbie
 
Registered: Jan 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Thank, but not useful in this case.

My file test.txt
Code:
post-proxy {
        #  If you want to have a log of replies from a home server,
        #  un-comment the following line, and the 'detail post_proxy_log'
        #  section, above.
#       post_proxy_log
#       attr_rewrite
        #  Uncomment the following line if you want to filter replies from
        #  remote proxies based on the rules defined in the 'attrs' file.
#       attr_filter
        #
        #  If you are proxying LEAP, you MUST configure the EAP
        #  module, and you MUST list it here, in the post-proxy
        #  stage.
        #
        #  You MUST also use the 'nostrip' option in the 'realm'
        #  configuration.  Otherwise, the User-Name attribute
        #  in the proxied request will not match the user name
        #  hidden inside of the EAP packet, and the end server will
        #  reject the EAP request.
        #
        eap
}
I like to see something like this.
Code:
post-proxy {
	eap
}
@helgiks
Code:
[root@hotspot ~]# cat test.txt | grep -v "^ *#"
post-proxy {
        #  If you want to have a log of replies from a home server,
        #  un-comment the following line, and the 'detail post_proxy_log'
        #  section, above.
        #  Uncomment the following line if you want to filter replies from
        #  remote proxies based on the rules defined in the 'attrs' file.
        #
        #  If you are proxying LEAP, you MUST configure the EAP
        #  module, and you MUST list it here, in the post-proxy
        #  stage.
        #
        #  You MUST also use the 'nostrip' option in the 'realm'
        #  configuration.  Otherwise, the User-Name attribute
        #  in the proxied request will not match the user name
        #  hidden inside of the EAP packet, and the end server will
        #  reject the EAP request.
        #
        eap
}

[root@hotspot ~]#
Code:
[root@hotspot ~]# grep -v "^ *#" test.txt 
post-proxy {
        #  If you want to have a log of replies from a home server,
        #  un-comment the following line, and the 'detail post_proxy_log'
        #  section, above.
        #  Uncomment the following line if you want to filter replies from
	#  remote proxies based on the rules defined in the 'attrs' file.
	#
	#  If you are proxying LEAP, you MUST configure the EAP
	#  module, and you MUST list it here, in the post-proxy
	#  stage.
	#
	#  You MUST also use the 'nostrip' option in the 'realm'
	#  configuration.  Otherwise, the User-Name attribute
	#  in the proxied request will not match the user name
	#  hidden inside of the EAP packet, and the end server will
	#  reject the EAP request.
	#
	eap
}

[root@hotspot ~]#
Code:
[root@hotspot ~]# sed '/^ *#/d' test.txt 
post-proxy {
	#  If you want to have a log of replies from a home server,
	#  un-comment the following line, and the 'detail post_proxy_log'
	#  section, above.
	#  Uncomment the following line if you want to filter replies from
	#  remote proxies based on the rules defined in the 'attrs' file.
	#
	#  If you are proxying LEAP, you MUST configure the EAP
	#  module, and you MUST list it here, in the post-proxy
	#  stage.
	#
	#  You MUST also use the 'nostrip' option in the 'realm'
	#  configuration.  Otherwise, the User-Name attribute
	#  in the proxied request will not match the user name
	#  hidden inside of the EAP packet, and the end server will
	#  reject the EAP request.
	#
	eap
}

[root@hotspot ~]#
@reddazz
Code:
[root@hotspot ~]# grep -i ^[A-Z] test.txt 
post-proxy {
[root@hotspot ~]#
 
Old 05-16-2008, 01:48 AM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
grep -v "#" test.txt works..
 
Old 05-16-2008, 02:09 AM   #6
helgiks
LQ Newbie
 
Registered: May 2008
Posts: 13

Rep: Reputation: 0
Hmm, can't see why those commands I posted are not working for you.

Code:
helgi@desktop:~$> cat > somefile << "EOF"
> post-proxy {
>         #  If you want to have a log of replies from a home server,
>         #  un-comment the following line, and the 'detail post_proxy_log'
>         #  section, above.
> #       post_proxy_log
> #       attr_rewrite
>         #  Uncomment the following line if you want to filter replies from
>         #  remote proxies based on the rules defined in the 'attrs' file.
> #       attr_filter
>         #
>         #  If you are proxying LEAP, you MUST configure the EAP
>         #  module, and you MUST list it here, in the post-proxy
>         #  stage.
>         #
>         #  You MUST also use the 'nostrip' option in the 'realm'
>         #  configuration.  Otherwise, the User-Name attribute
>         #  in the proxied request will not match the user name
>         #  hidden inside of the EAP packet, and the end server will
>         #  reject the EAP request.
>         #
>         eap
> }
> EOF
helgi@desktop:~$> grep -v "^ *#" somefile 
post-proxy {
        eap
}
helgi@desktop:~$>
Works like this on both my Slackware and my Ubuntu :P

EDIT: Is it possible that those comments are just some long lines that don't start with a newline?

Try $ sed 's/^/_/g' <somefile> - and you should see a "_" in the beginning of every line, I think this is your problem, this command should verify that

Last edited by helgiks; 05-16-2008 at 02:18 AM. Reason: typo
 
Old 05-16-2008, 03:54 AM   #7
wearetherock
LQ Newbie
 
Registered: Jan 2008
Posts: 9

Original Poster
Rep: Reputation: 0
@helgiks, what's this

Code:
[root@hotspot ~]# cp test.txt test2.txt
[root@hotspot ~]# sed 's/^/_/g' test2.txt 
_post-proxy {
_       #  If you want to have a log of replies from a home server,
_       #  un-comment the following line, and the 'detail post_proxy_log'
_       #  section, above.
_#      post_proxy_log
_#      attr_rewrite
_       #  Uncomment the following line if you want to filter replies from
_       #  remote proxies based on the rules defined in the 'attrs' file.
_#      attr_filter
_       #
_       #  If you are proxying LEAP, you MUST configure the EAP
_       #  module, and you MUST list it here, in the post-proxy
_       #  stage.
_       #
_       #  You MUST also use the 'nostrip' option in the 'realm'
_       #  configuration.  Otherwise, the User-Name attribute
_       #  in the proxied request will not match the user name
_       #  hidden inside of the EAP packet, and the end server will
_       #  reject the EAP request.
_       #
_       eap
_}
[root@hotspot ~]#
 
Old 05-16-2008, 05:16 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Maybe there are tabs at the beginning of comment lines. You can try this
Code:
 sed '/^[\t ]*#/d' test.txt
 
Old 05-16-2008, 05:38 AM   #9
wearetherock
LQ Newbie
 
Registered: Jan 2008
Posts: 9

Original Poster
Rep: Reputation: 0
It's work, thank you very much.

Code:
[root@hotspot ~]# sed '/^[\t ]*#/d' test.txt
post-proxy {
	eap
}
[root@hotspot ~]#
 
Old 05-16-2008, 08:50 AM   #10
helgiks
LQ Newbie
 
Registered: May 2008
Posts: 13

Rep: Reputation: 0
Ah, there it is...

Than this should do it for you
Code:
$ grep -v "^[[:space:]]*#" <file>
Didn't think about the "other" kind of "space", this will include every kind of space, tab...
 
  


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
Show a certain line in file shahgols Linux - Newbie 4 03-22-2007 10:47 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 02:43 PM
Shell script - how to show a specific line of a text file davi_cabral Linux - Software 3 09-28-2004 02:39 PM
could u show me the last line of your /etc/inittab file? C++freak Linux - Software 2 07-30-2002 09:27 AM


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