Vonton
Vonton

Reputation: 3334

If one string in column is equal, print specific text wit AWK

I have problem, could you help me pls?

INPUT:

LIS
LOP
LOP
LAT

If one or more string in input will be "LIS" print one row "THERE IS LIS" if There will be combination of "LOP" or "LAT", without "LIS", print "THERE IS LOP" and if there will be all strings "LAT" print "THERE IS ONLY LAT". Thank you

Upvotes: 0

Views: 53

Answers (1)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

awk solution:

check_column.awk script:

#!/bin/awk -f

/LIS/{ print "THERE IS LIS"; exit }
$0!~/(LOP|LAT)/{ print "No matches"; exit }
/LOP/{ lop++ }/LAT/{ lat++ }
END{ 
    if (lop || lat) { 
        printf "%s\n", (lat==NR)? "THERE IS ONLY LAT":"THERE IS LOP" 
    } 
}

Usage:

Sample file file1:

LIS
LOP
LOP
LAT

awk -f check_column.awk file1
THERE IS LIS

----------

Sample file file2:

LOP
LOP
LAT
LAT

awk -f check_column.awk file2
THERE IS LOP

----------

Sample file file3:

LAT
LAT
LAT

awk -f check_column.awk file3
THERE IS ONLY LAT

Upvotes: 2

Related Questions