LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-04-2024, 12:26 AM   #1
blumenwesen
Member
 
Registered: Jan 2022
Posts: 53

Rep: Reputation: 0
AWK Filter Items from 3 Lists


Hello, my experiment with AWK The versed lines from the comparison of List2 and List1 1 again by filtering exceptions from List 3 gives me the following:

Code:
awk 'NR==FNR{z[$0]++; next} !($0 in z) || !($0 in y)' y="$(echo -e "x\ny")" <(echo -e "a\nb") <(echo -e "a\nb\nc\nx\ny\nz")

awk: (FILENAME=/dev/fd/62 FNR=1) fatal: attempt to use a scalar value as field
This is what the result should look like:
Quote:
c
z
 
Old 02-04-2024, 02:13 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,311
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
Is the variable y going to keep changing with each pass through the loop or will it stay the same for the duration of the script? If it stays the same then you can use --assign to read it in and split() to break it into an array.
 
Old 02-04-2024, 04:05 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,855

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
Code:
awk -v y="$(....)" 'NR==......'
If I understand it well -v is missing
 
Old 02-04-2024, 04:30 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Setting the variable is fine, but as the error message has advised, 'y' is not an array and hence you cannot use the 'in' test on a scalar variable
 
1 members found this post helpful.
Old 02-04-2024, 07:22 PM   #5
blumenwesen
Member
 
Registered: Jan 2022
Posts: 53

Original Poster
Rep: Reputation: 0
I misrepresented it yesterday before sleeping, meant that the lines from list3 must not be added but subsequently from the result of the deduction of list2 from list1.
Is there an easier way within a single awk call?

Code:
awk 'NR==FNR{z[$0]++; next} !($0 in z)' <(echo -e "c\nf") <(awk 'NR==FNR{z[$0]++; next} !($0 in z)' <(echo -e "a\nb\nc\nd") <(echo -e "a\nb\nc\nd\ne\nf"))
>>d
>>e

Last edited by blumenwesen; 02-04-2024 at 08:23 PM.
 
Old 02-04-2024, 09:53 PM   #6
blumenwesen
Member
 
Registered: Jan 2022
Posts: 53

Original Poster
Rep: Reputation: 0
Now the chaos could no longer withstand my superior mind. =D
Thanks for help

Code:
awk -v z="$(echo -e "a\nf")" 'BEGIN{y=split(z, x, "\n"); for(w=1; w<=y; w++){v[x[w]]}} NR==FNR{u[$0]++; next} {if(!($0 in u) && !($0 in v)){print $0}}' <(echo -e "a\nb\ne\nf") <(echo -e "a\nb\nc\nd\ne\nf")
>>c
>>d
 
Old 02-06-2024, 02:19 PM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,796

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
A[$0]=1 or A[$0]++ assign a value to the array member. awk allows to omit the value, just define the index: A[$0]

awk has no means to pass an array as an argument. Passing a string then BEGIN split into an array - ok.
The following works with files only.
Also it reads the total list first, then eliminates from the following files. (So you could easily add more files.)
Code:
awk 'NR == FNR { A[$0]; next } { delete A[$0] } END { for (a in A) print a }' <(echo -e "a\nb\nc\nd\ne\nf") <(echo -e "a\nf") <(echo -e "a\nb\ne\nf")
c
d
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
FireStarter lists IP blocked that etherape lists as connections??? theKbStockpiler Linux - Security 1 09-26-2017 09:24 PM
LXer: Python - Sorting Lists inside of Lists LXer Syndicated Linux News 0 06-06-2013 01:00 PM
How are opt-in e-mail lists distinguished from spam lists? Travis86 Programming 2 01-29-2012 08:55 PM
LXer: Unique Sorting Of Lists And Lists Of Lists With Perl For Linux Or Unix LXer Syndicated Linux News 0 09-05-2008 01:50 PM
Add items to GDM Session lists mattdev121 Linux - General 1 11-30-2004 02:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration