Awk_ printing non-match in the array


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Awk_ printing non-match in the array
# 1  
Old 11-30-2019
Awk_ printing non-match in the array

Dear developpers,

I intend to compare the file1 to the file2 based on the values of column 1 in file1. However, the current code - that I modified from the forum- only print matches. I am wondering if there is a solution to have both matches and non matches being printed.
Many thanks in advance and sorry for the basic question

File1
Code:
abc    
ddd     
cnn

File2
Code:
abc  zzz     
A	zzz    
A	zzz     
A	zzz

Current code output :
Code:
awk -F"\t" 'FILENAME=="file1"{A[$1]=$1}
FILENAME=="file2" {if(A[$1]){print$0} else {????????"\t" "0"}}' file1 file2
abc zzz 
         0
         0
         0

The intended output ideally is similar to:
Code:
abc  zzz 
ddd   0
cnn   0

Moderator's Comments:
Mod Comment Please do wrap your samples with CODE TAGS as per forum rules.

Last edited by RavinderSingh13; 11-30-2019 at 03:10 PM..
# 2  
Old 11-30-2019
Hi, try:
Code:
awk 'NR==FNR{A[$1]=$2; next} {print $1, $1 in A? A[$1]:0}' file2 file1

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 11-30-2019
Thanks ! There is only a minor issue that is prints 'zero' in the front of the match as well :
Code:
abc 0
ddd 0
cnn 0

Ideally it should be
Code:
abc 
ddd 0
cnn 0

--- Post updated at 09:18 PM ---

Solved, Thank you!

Last edited by vbe; 11-30-2019 at 04:32 PM.. Reason: code tags please
# 4  
Old 11-30-2019
Quote:
Originally Posted by arsalane
Thanks ! There is only a minor issue that is prints 'zero' in the front of the match as well :
Code:
abc 0
ddd 0
cnn 0

Ideally it should be
Code:
abc 
ddd 0
cnn 0

--- Post updated at 09:18 PM ---
Solved, Thank you!
Hello arsalane,

Good that you have solved the problem. I would like to request you to please do add that fix in this thread too, do that all users who are referring this thread in future could be benefitted too from that.

Keep up the learning and sharing on this great site Unix.com cheers.

Thanks,
R. Singh
# 5  
Old 12-03-2019
Thanks! The code is absolutely fine. The issue was the corrupted input file format.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Array not printing values if used in a loop

Hello! I'm making an English to Morse Code translator and I was able to mostly get it all working by looking through older posts here; however, I have one small problem. When I run it it's just printing spaces for where the characters should be. It runs the right amount of times, and if I try... (3 Replies)
Discussion started by: arcoleman10
3 Replies

2. Shell Programming and Scripting

Printing array elements in reverse

Hello Experts, I am trying to print an array in reverse. Input : 1. Number of array elements 2. The array. Eg: 4 1 2 3 4 Expected Output (elements separated by a space) : 4 3 2 1 My Code : (6 Replies)
Discussion started by: H squared
6 Replies

3. Shell Programming and Scripting

Variable substitution in array printing

Hi folks, A really dumb question as I've wasted far too long trying to get this to work.... (on RH bash) I have an array: m0='<hello>' m0='<there>' m0='<fred>' v0='<goodbye>' v0='<again>' v0='<john>' in my code I calculate the value of the variable to output and if I echo it, I... (2 Replies)
Discussion started by: say170
2 Replies

4. Shell Programming and Scripting

Printing next 6 lines from of pattern match

Hi, i have a big file having many opcodes. if (opcode="01110000000100000000" ) then --fadd result.opcode := "01110000000100000000"; result.s0 := '1'; result.s1 := '1'; result.s2 := '0'; result.inst := '0'; result.scalar := '1';... (7 Replies)
Discussion started by: twistedpair
7 Replies

5. UNIX for Dummies Questions & Answers

printing array elements

Is there a way to print multiple array elements without iterating through the array using bash? Can you do something like... echo ${array}and get all those separate elements from the array? (2 Replies)
Discussion started by: jrymer
2 Replies

6. UNIX for Dummies Questions & Answers

Help with printing sorted array of numbers

Dear All, I am trying to sort an array of numbers to retrieve the mimimum and maximum values of numbers in that array, by printing the first and last elements of the sorted array. My code is @sorted_numbers = sort (@numbers); print "@sorted_numbers\n"; print "$sorted_numbers,... (0 Replies)
Discussion started by: pawannoel
0 Replies

7. Shell Programming and Scripting

printing words based on column match

pls help Input: file1 word1 text1 word2 text2 word3 text3 file2 word1 text11 word3 text13 can u pls help in getting the same output: file1 text1 text2 text3 (1 Reply)
Discussion started by: bha148
1 Replies

8. Shell Programming and Scripting

Perl: Printing Multiple Lines after pattern match

Hello People, Need some assistance/guidance. OUTLINE: Two files (File1 and File2) File1 has some ids such as 009463_3922_1827 897654_8764_5432 File2 has things along the lines of: Query= 009463_3922_1827 length=252 (252 letters) More stufff here ... (5 Replies)
Discussion started by: Deep9000
5 Replies

9. Shell Programming and Scripting

Array Printing Inline

Dear friends , The output file of below script Pls#!/bin/sh awk '{ bo = substr($0,13,3) slm = substr($0,150,8) slo = substr($0,175,7) inc = substr($0,97,10)/100 busi = substr($0,83,10) mth = substr($0,39,2) yer = substr($0,35,4) ... (2 Replies)
Discussion started by: vakharia Mahesh
2 Replies

10. Programming

printing all array values using dbx 7.2.1

how do we print the entire contents of arrays in dbx ? Ive tried using print x to print values 1 to 5 of the array x, however dbx complains and doesnt allow this help is much appreciated (1 Reply)
Discussion started by: JamesGoh
1 Replies
Login or Register to Ask a Question