Awk for multiple templates

Hello guys quick question

my file looks like this:

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 with awk print $ 0 for specific lines using $ 1

For example,

 awk '{if ($1!="C") {print $0} else if ($1!="D") {print $0}}' 

But there must be a way to combine ā€œCā€ and ā€œDā€ into one IF statement ... right?

For example, if I want to search for == L, H, S, that is ... NOT C or D, how would I be right?

Just curious: D

thanks

Jonathan

+7
source share
3 answers

Your current state is incorrect, since both $1!="C" and $1!="D" cannot be false at the same time. Therefore, it will always print the entire file.

This will be done as you described:

 awk '{if ($1!="C" && $1!="D") {print $0}}' file 
+8
source

Using awk, you can provide rules for specific patterns with syntax

 awk 'pattern {action}' file 

see the awk manual page for a template definition. In your case, you can use regex as a template with syntax

 awk'/regular expression/ {action}' file 

and a basic regex that would suit your needs could be

 awk '/^[^CD]/ {print $0}' file 

which you can really shorten in

 awk '/^[^CD]/' file 

since {print $0} is the default action as suggested in the comments.

+5
source
 awk '$1 ~ /[CD]/' file awk '$1 ~ /[LHS]/' file awk '$1 ~ /[^LHS]/' file awk '$1 !~ /[LHS]/' file 
+3
source

All Articles