1

I'd like to combine those awk statements:

awk -F'\t' 'NF>1 && $NF!=""' #prints every row with more than two fields 
awk '$1 ~ /^[0-9]/'        #prints every row starting with a number 
awk -F'\t' 'NF==1'           #prints every row with one only field
awk '$1 ~ /^[A-Z]/'        #prints every row starting with a capital letter 

in order to get a statement printing every row that begins with a capital letter AND contains one only field, and every row with more than two fields, each of them starting with an integer.

Sample input:

NAME_1
1   
11  11
key 11
KEY KEY

Expected output:

NAME_1
11  11
1

You can combine them using awk logical && and || (See more awk boolean operators):

$ awk '(NF > 1 && $1 ~ /^[0-9]/) || (NF == 1 && $1 ~ /^[[:upper:]]/)' file
NAME_1
11  11

You should use [[:upper:]] to match capital letter because [A-Z] only work in C locale.

  • What if I would like to pick the corresponding line only if each field (of the line starting with an integer) start with an integer? – dovah Jul 27 '14 at 19:18
  • Use a for loop over all the fields, if a field does not start with an integer, skip to next input, otherwise print. Something like (NF > 1 && $1 ~ /^[0-9]/) {for(i=2;i<=NF;i++){...}} – cuonglm Jul 27 '14 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.