HPE Community read-only access December 15, 2018
This is a maintenance upgrade. You will be able to read articles and posts, but not post or reply.
Hours:
Dec 15, 4:00 am to 10:00 am UTC
Dec 14, 10:00 pm CST to Dec 15, 4:00 am CST
Dec 14, 8:00 pm PST to Dec 15, 2:00 am PST
cancel
Showing results for 
Search instead for 
Did you mean: 

uncommneted Lines in files

 
SOLVED
Go to solution
Trng
Super Advisor

uncommneted Lines in files

Hi Team,

i have a file in hpux 11.23 and having 500 Lines in that file ,some of the lines are commented and some of the lines are uncommented .what is the simple command to show only commented /uncommented lines in this file ?


rgds,trng
administrator
7 REPLIES
Doug O'Leary
Honored Contributor
Solution

Re: uncommneted Lines in files

hey;

grep -v ^# ${file}

or

grep ^# ${file}

hth;

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Steven Schweda
Honored Contributor

Re: uncommneted Lines in files

> [...] a file [...]

Not a very detailed description of anything.

Define "commented". Commented as in a shell
script? Commented as in C code? C++?
Fortran? Pascal? DCL?
James R. Ferguson
Acclaimed Contributor

Re: uncommneted Lines in files

Hi :

If by "commented' you mean with the '#' then this will find lines where the first non-whitespace character is a '#':

# grep ^[[:space:]]*# file

...or those that are non commented-out:

# grep -v ^[[:space:]]*# file

Notice that leading whitespace before the '#' is optional.

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: uncommneted Lines in files

As Steven says, a commented line is quite vague. Lets assume that you are referring to the fairly common Unix technique to place a # on lines that are comments. Doug's solution will work fine -- as long as your files ALWAYS have # in column 1. But many files only require that a # be the first non-whitespace character, so there may be tabs and/or spaces in front of the #. Also, a comment line may be a blank line, or a line with tabs and/or spaces (ie, whitespace) but nothing else:

So here are techniques for handling each condition. It is assumed that each line has been assigned to the variable TEXT:

Skip blank lines:
[[ "$(echo $TEXTLINE | awk NF)" = "" ]] &&
continue

Skip "#" in column 1 only:
[[ $(echo "$TEXTLINE" | grep -cv "^#") -gt 0 ]] &&
continue

Skip "#" even if preceded by whitespace:
echo "$TEXTLINE" | read WORD1 REST
[[ "$(echo "$WORD1" | cut -c1 )" = "#" ]] &&
continue

I have attached a script called NOcomment which will list files without leading "#" characters, or if you run NOcomment with -1 (minus one), it will only look for # in column 1 (much faster). NOcomment accepts stdin or filename(s) on the command line.


Bill Hassell, sysadmin
James R. Ferguson
Acclaimed Contributor

Re: uncommneted Lines in files

Hi:

> Bill: Doug's solution will work fine -- as long as your files ALWAYS have # in column 1.

And that was the whole point of my suggestion to use:

# grep ^[[:space:]]*# file

...JRF...
Bill Hassell
Honored Contributor

Re: uncommneted Lines in files

So with James' excellent use of regular expressions, my NOcomment script (attached) has been rewritten to encapsulate this filter:

awk NF filename | grep -v ^[[:space:]]*#

This will remove blank lines and any line with a # as the first non-whitespace character.


Bill Hassell, sysadmin
James R. Ferguson
Acclaimed Contributor

Re: uncommneted Lines in files

Hi (again):

In the spirit of TMTOWTDI, if we want to treat as "comments" both blank lines and those whose first non-whitespace character is "#", then we could do:

# awk '/^[[:space:]]*(#|$)/' file

...or to find lines that are _not_ comments by this definition:

# awk '! /^[[:space:]]*(#|$)/' file

Regards!

...JRF...