Add event


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Add event
# 1  
Old 03-31-2015
Add event

Hi all,

Need to solve this problem
file1.txt
Code:
0912804166|RR
0912804171|YY

file2.txt
Code:
0912804175|AXE|510100475741082
0912804166|NODE|0

I need to add event on file1.txt if column 1 in file1.txt is also found in file2.txt, the event is 1, but in contrary if column 1 in file1.txt is not found in file2.txt then the event added to the third column with 0

my expected output
Code:
0912804166|RR|1
0912804171|YY|0

I did this
Code:
awk -F'|' 'NR==FNR {h[$1] = $2; next} {FS=OFS="|";print $0,h[$1]}' file2.txt file1.txt | awk-F'|' '{if($3>0) print $0,"1"}{print $0,0}'

# 2  
Old 03-31-2015
You were aiming in the right direction. Try
Code:
awk -F'|' 'NR==FNR {h[$1]=1; next} {print $0, h[$1]+0}' OFS="|" file2 file1
0912804166|RR|1
0912804171|YY|0

These 2 Users Gave Thanks to RudiC For This Post:
# 3  
Old 03-31-2015
what does this mean
Code:
{h[$1]=1

and
Code:
h[$1]+0}

?
any way, big thanks
# 4  
Old 03-31-2015
Quote:
Originally Posted by radius
what does this mean
Code:
{h[$1]=1       # this is the "1" you want printed if $1 is found in file2

and
Code:
h[$1]+0}       # this "+0" converts the empty h element to 0 if $1 is not found; it doesn't modify the 1 if $1 is found

?
. . .
 
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question