My file looks like:
L 0 256 * * * * *
H 0 307 100.0 + 0 0
S 30 351 * * * * *
D 8 27 * * * * 99.3
C 11 1 * * * * *
for my script I would like to start by awk print $0 for certain lines using $1
Such as
awk '{if ($1!="C") {print $0} else if ($1!="D") {print $0}}'
But, there has to be a way to combine "C" and "D" into one IF statement... right?
For example if I want to search for == L,H,S ie... NOT C or D how would I right this?
Any awk expression is valid as an awk pattern. The pattern matches if the expression's value is nonzero (if a number) or non-null (if a string). The expression is reevaluated each time the rule is tested against a new input record.
The next statement forces awk to immediately stop processing the current record and go on to the next record. This means that no further rules are executed for the current record, and the rest of the current rule's action isn't executed.
Awk is mostly used for pattern scanning and processing. It searches one or more files to see if they contain lines that matches with the specified patterns and then perform the associated actions. Awk is abbreviated from the names of the developers – Aho, Weinberger, and Kernighan.
Your present condition is not correct as both $1!="C"
and $1!="D"
can't be false at the same time. Hence, it will always print the whole file.
This will do as you described:
awk '{if ($1!="C" && $1!="D") {print $0}}' file
Using awk, you can provide rules for specific patterns with the syntax
awk 'pattern {action}' file
see the awk manual page for the definition of a pattern. In your case, you could use a regular expression as a pattern with the syntax
awk'/regular expression/ {action}' file
and a basic regular expression which would suit your needs could be
awk '/^[^CD]/ {print $0}' file
which you can actually shorten into
awk '/^[^CD]/' file
since {print $0}
is the default action, as suggested in the comments.
awk '$1 ~ /[CD]/' file
awk '$1 ~ /[LHS]/' file
awk '$1 ~ /[^LHS]/' file
awk '$1 !~ /[LHS]/' file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With