11
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
1
  • 1
    Why is the +ASM2 line expected not to be part of the output? Commented Dec 17, 2014 at 16:41

4 Answers 4

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

1
  • this needs some explanation for it to make sense to visitors.
    – qodeninja
    Commented Sep 2, 2019 at 7:50
13

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

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 
-2
sed 's/#.*//'

This gets rid of comments, even if they don't start at the first column.

1
  • 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. Commented Dec 17, 2014 at 17:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .