Reputation:
I have two files file1.txt and file2.txt.
file1.txt
[email protected]
[email protected]
[email protected]
[email protected]
file2.txt
Anil=Active
Amal=Active
Ajith=Inactive
Aravind=Active
Midhun=Active
I need to add an extra column in file1.txt from file2.txt mentioning whether each of them is active or inactive and also remove lines from file2.txt which are not present in file1.txt.(for example, Midhun is not present in file1.txt. So i need to remove midhun from file2.txt)
My output file should be
output.txt
Amal=123=Active
Anil=342=Active
Ajith=548=Inactive
Aravind=998=Active
I tried the following. But it is not working.
while IFS= read -r line
do
key=`echo $line | awk -F "=" '{print $1}'` < file1.txt
key2=`echo $line | awk -F "=" '{print $2}'` < file1.txt
value=`echo $line | awk -F "=" '{print $2}'` < file2.txt
echo "$key=$key2=$value"
done
Upvotes: 1
Views: 68
Reputation: 8972
No need for scripting. Sort the files and then it's a simple join.
join -t= <(sort file1.txt) <(sort file2.txt)
To comply with the OP's update, let's cut only the first two fields of file1:
join -t= <(sort file1.txt | cut -d= -f-2) <(sort file2.txt)
Upvotes: 1
Reputation: 133428
EDIT: Since OP changed his requirement so adding this solution now.
awk '
BEGIN{
FS=OFS="="
}
FNR==NR{
a[$1]=$2
next
}
($1 in a){
$3=""
sub(/=$/,"")
print $0,a[$1]
}
' Input_file2 Input_file1
This should be a simple task for awk
, please try following.
awk 'BEGIN{FS=OFS="="} FNR==NR{a[$1]=$2;next} ($1 in a){print $0,a[$1]}' file2 file1
Explanation: Adding detailed explanation for above code here.
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section for this program from here.
FS=OFS="=" ##Setting FS and OFS value as = here for all lines.
} ##Closing BLOCK for BEGIN here.
FNR==NR{ ##Checking condition FNR==NR which will be TRUE when first Input_file is being read.
a[$1]=$2 ##Creating array a with index $1 and value $2.
next ##next will skip all further statements from here.
}
($1 in a){ ##Checking condition ig $1 of current line(from file1) is present in array a then do following.
print $0,a[$1] ##Printing current line and value of array a with index $1 of current line here.
}
' file2 file1 ##Mentioning Input_file names here.
Upvotes: 1