Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find and match an exact string in a column using AWK?

im having trouble on matching an exact string that i want to find in a file using awk.

I have the file called "sup_groups.txt" that contains:
(the structure is: "group_name:pw:group_id:user1<,user2>…")

adm:x:4:syslog,adm1
admins:x:1006:adm2,adm12,manuel
ssl-cert:x:122:postgres
ala2:x:1009:aceto,salvemini
conda:x:1011:giovannelli,galise,aceto,caputo,haymele,salvemini,scala,adm2,adm12
adm1Group:x:1022:adm2,adm1,adm3
docker:x:998:manuel

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

now, i want to extract the records that have in the user list the user "adm1" and print the first column (the group name), but you can see that there is a user called "adm12", so when i do this:

awk -F: ‘$4 ~ "adm1" {print $1}’ sup_groups.txt

the output is:

adm
admins
conda
adm1Group

the command of course also prints those records that contain the string "adm12", but i don’t want these lines because i’m interested only on the user "adm1".

So, How can I change this command so that it just prints the lines 1 and 6 (excluding 2 and 5)?

thank you so much and sorry for my bad english

EDIT: thank you for the answers, u gave me inspiration for the solution, i think this might work as well as your solutions but more simplified:

awk -F: ‘$4 ~ "adm,|adm1$|:adm1," {print $1}’ sup_groups.txt

basically im using ORs covering all the cases and excluding the "adm12"
let me know if you think this is correct

>Solution :

1st solution: Using split function of awk. With your shown samples, please try following awk code.

awk -F':' '
{
  num=split($4,arr,",")
  for(i=1;i<=num;i++){
    if(arr[i]=="adm1"){
      print
    }
  }
}
' Input_file

Explanation: Adding detailed explanation for above.

awk -F':' '               ##Starting awk program from here setting field separator as : here.
{
  num=split($4,arr,",")   ##Using split to split 4th field into array arr with delimiter of ,
  for(i=1;i<=num;i++){    ##Running for loop till value of num(total elements of array arr).
    if(arr[i]=="adm1"){   ##Checking condition if arr[i] value is equal to adm1 then do following.
      print               ##printing current line here.
    }
  }
}
' Input_file              ##Mentioning Input_file name here.


2nd solution: Using regex and conditions in awk.

awk -F':' '$4~/^adm1,/ || $4~/,adm1,/ || $4~/,adm1$/' Input_file

Explanation: Making field separator as : and checking condition if 4th field is either equal to ^adm1,(starting adm1,) OR its equal to ,adm1, OR its equal to ,adm1$(ending with ,adm1) then print that line.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading