LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to show only uncomment line in the file. (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-show-only-uncomment-line-in-the-file-642543/)

wearetherock 05-15-2008 11:49 PM

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.

helgiks 05-16-2008 12:03 AM

$ 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.

reddazz 05-16-2008 12:09 AM

Maybe something like grep may help e.g.
Code:

$grep -i ^[A-Z] somefile

wearetherock 05-16-2008 12:37 AM

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 ~]#


Nylex 05-16-2008 12:48 AM

grep -v "#" test.txt works..

helgiks 05-16-2008 01:09 AM

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 :)

wearetherock 05-16-2008 02:54 AM

@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 ~]#


colucix 05-16-2008 04:16 AM

Maybe there are tabs at the beginning of comment lines. You can try this
Code:

sed '/^[\t ]*#/d' test.txt

wearetherock 05-16-2008 04:38 AM

It's work, thank you very much.

Code:

[root@hotspot ~]# sed '/^[\t ]*#/d' test.txt
post-proxy {
        eap
}
[root@hotspot ~]#


helgiks 05-16-2008 07:50 AM

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...


All times are GMT -5. The time now is 02:54 AM.