Group and search using awk


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Group and search using awk
# 1  
Old 05-11-2018
Group and search using awk

file:
Quote:
A1,B4
A1,C2
A1,D1
A1,A1
A2,B2
A2,C2
A3,A3
A3,D1
Data has to be grouped on 1st field; And in each group, print 2nd field IF 1st field doesn't match with any of the values in 2nd field..
o/p to be:
Quote:
B2
C2
I ended up writing as below, any help ??
Code:
awk -F, '!($1 in a){a[$2]++;}END{for (i in a)print i}' file


Last edited by JSKOBS; 05-11-2018 at 07:50 AM..
# 2  
Old 05-11-2018
I'm not quite getting your requirement. but your code will not exclude the way you seem to want. You have to read the entire file into an array first, then loop back through it, set your exclusions in the second loop, i.e., set the array element "count" from 1 to zero. Now your array will work for a simple read through it.

Why? because encountering later records in the file invalidates your first choice of "yes, print this" based on an incomplete data set. ...assuming I understood your request.
# 3  
Old 05-11-2018
Code:
awk -F, '{a[$0]=$0} $1==$2 {b[$1]=$2} END {for (i in a) {$0=a[i]; if (!($1 in b)) print $2}}' file


Last edited by rdrtx1; 05-11-2018 at 01:34 PM..
This User Gave Thanks to rdrtx1 For This Post:
# 4  
Old 05-11-2018
Try also
Code:
awk -F, '{T[$1] = T[$1] ORS $2} END {for (t in T) if (!(T[t] ~ t)) print substr(T[t],2)}' file
B2
C2

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search for Multiple strings in a given date range and print the Group if they exists

Hi, I am Searching for Multiple strings in a given date range and print the Group if they exists. the below is the format: ------------------------------------------------------------------------------------------------------------------------- ID: FIRST ID MESSAGE: Event Message... (5 Replies)
Discussion started by: linuxuser999
5 Replies

2. Shell Programming and Scripting

Search for files owned by particular owner and group

I am searchingfor files owned by particular owner and group in a particular directory including its sub-directories. I use find <dir> -user <user> -group <group> -exec ls -l {} \; It does not work completely. In the sense is a subdirectory is owned by 'user' and group 'group' then all... (9 Replies)
Discussion started by: Soham
9 Replies

3. Shell Programming and Scripting

need a one liner to grep a group info from /etc/group and use that result to search passwd file

/etc/group tiadm::345:mk789,po312,jo343,ju454,ko453,yx879,iy345,hn453 bin::2:root,daemon sys::3:root,bin,adm adm::4:root,daemon uucp::5:root /etc/passwd mk789:x:234:1::/export/home/dummy:/bin/sh po312:x:234:1::/export/home/dummy:/bin/sh ju454:x:234:1::/export/home/dummy:/bin/sh... (6 Replies)
Discussion started by: chidori
6 Replies

4. Shell Programming and Scripting

Search, group , print count

hi All, need help. have a file like below A, error in 123 B, log files are present A, error in 23444 B, log files are present A, move to next line C, matching messages -- expected output-- A , count =2 , error in * A , count =1 , move to next line B , count =2 , log files are present... (2 Replies)
Discussion started by: arun1401
2 Replies

5. Shell Programming and Scripting

Split, Search and Reformat by Data Group

Hi, I am writing just to share my appreciation for help I have received from this site in the past. In a previous post Split File by Data Group I received a lot of help with a troublesome awk script to reformat some complicated data blocks. What I learned really came in hand recently when I... (1 Reply)
Discussion started by: mkastin
1 Replies

6. Shell Programming and Scripting

search file and group values with problematic tab

hi people; the similar topic is being opened in here and here but i have confused with following condition. so i wanted to open a seperate topic. from my file.txt:... ... ... 110105-16:04:04 192.168.1.1 7.1j Port_NODE_MODEL_M_1_8 stopfile=/tmp/10544... (0 Replies)
Discussion started by: gc_sw
0 Replies

7. Shell Programming and Scripting

Group search (multiple data points) in Linux

Hi All I have a data set like this tab delimited: weft fgr-1 345 -1 fgrythdgd weft fgr-3 456 -2 ghjdklflllff weft fgr-11 456 -3 ghtjuffl weft fgr-1 213 -2 ghtyjdkl weft fgr-34 567 -5 fghytkflf frgt fgr-36 567 -1 ghrjufjf frgt fgr-45 678 -2 ghjruir frgt fgr-34 546 -5 gjjjgkldlld frgt... (4 Replies)
Discussion started by: Lucky Ali
4 Replies

8. Shell Programming and Scripting

Using find command to search a pattern in group of files

Hi i am having a group of *.csh files under parent directory. Now i want to search a particular pattern in these group of *.csh files(suppose i need to search a pattern ABC - proj ). Can anyone please tell me how to do it using find command. Thanks in advance sarbjit (4 Replies)
Discussion started by: sarbjit
4 Replies

9. Shell Programming and Scripting

awk help required to group output and print a part of group line and original line

Hi, Need awk help to group and print lines to format the output as shown below INPUT FORMAT set echo on set heading on set spool on /* SCHEMA1 */ CREATE TABLE T1; /* SCHEMA1 */ CREATE TABLE T2; /* SCHEMA1 */ CREATE TABLE T3; /* SCHEMA1 */ CREATE TABLE T4; /* SCHEMA1 */ CREATE TABLE T5;... (5 Replies)
Discussion started by: rajan_san
5 Replies

10. Cybersecurity

Recursive search for group or other writeable 'dot' files

I'm looking for a easy way to do a recursive search through a directory structure for all files that begin with '.' that are group or other writable. Any suggestions? (5 Replies)
Discussion started by: maficdan
5 Replies
Login or Register to Ask a Question