How to ignore the lines starts with # using grep / awk

36,464

Solution 1

awk -F: '/^[^#]/ { print $2 }' /etc/oratab | uniq

/^[^#]/ matches every line the first character of which is not a #; [^ means "none of the charaters before the next (or rather: closing) ].

As only the part between the first two colons is needed -F:' makesawksplit the line at colons, andprint $2` prints the second part.

Solution 2

Using grep:
grep -vE "^#" or grep -E "^[^#]"

Solution 3

The next awk statement will skip the current line, that is useful if you have to match multiple blocks in your script.

awk '
/^#/ {next}
/ pattern 1 / {    }
/ pattern 2 / {    } '  filename 
Share:
36,464

Related videos on Youtube

ABUL KASHIM
Author by

ABUL KASHIM

Updated on September 18, 2022

Comments

  • ABUL KASHIM
    ABUL KASHIM over 1 year
    cat /etc/oratab
    #test1:/opt/oracle/app/oracle/product/11.2.0.4:N
    +ASM2:/grid/oracle/app/oracle/product/11.2.0.4:N         # line added by Agent
    test2:/opt/oracle/app/oracle/product/11.2.0.4:N          # line added by Agent
    test3:/opt/oracle/app/oracle/product/11.2.0.4:N          # line added by Agent
    
    oracle@node1 [/home/oracle]
    cat /etc/oratab | grep -v "agent" | awk -F: '{print $2 }' | awk NF | uniq
    

    awk NF is to omit blank lines in the output.

    Only lines starts with # needs to be ignored. Expected output:

    /grid/oracle/app/oracle/product/11.2.0.4
    /opt/oracle/app/oracle/product/11.2.0.4
    
    • Hauke Laging
      Hauke Laging over 9 years
      Why is the +ASM2 line expected not to be part of the output?
  • Hauke Laging
    Hauke Laging over 9 years
    That doesn't skip the line though (and would even destroy/change non-comment lines that contain a #) but prints an empty line. `sed -n '/^ *[^#]/p' would do the job.
  • qodeninja
    qodeninja over 4 years
    this needs some explanation for it to make sense to visitors.