Reputation: 967
How can I exclude lines starting with a space character, and that have nothing else on the line? With awk, I want to print the line Need to print
, but it's also printing the blank line. How can I exclude it?
$0 !~/^start|^#/ {
print "Result : %s",$0
}
# test
start
Need to print
Result : %s
Result : %s Need to print
Upvotes: 1
Views: 2979
Reputation: 157947
You can use:
awk '/^[^[:space:]]/{print "Result : " $0}'
The use of [^[:space:]]
ensures that there is at least a single non space character in every line which get's printed.
Upvotes: 3
Reputation: 84343
You aren't really asking about lines that start with a space, you're asking about how to discard blank lines. Pragmatically speaking, blank lines have no fields, so you can use the built-in NF variable to discard lines which don't have at least one field. For example:
$ awk 'NF > 0 && !/^(start|#)/ {print "Result: " $0}' /tmp/corpus
Result: Need to print
Upvotes: 4