mahendra rathod
mahendra rathod

Reputation: 1638

shell script match data from another text file

i have a shell script

#/bin/bash
var1=`cat log.json | grep "accountnumber" | awk -F ' ' '{print $1}'`
echo $var

output of shell script is :-

23466
283483
324932
87374

I want match the above number which is already store in another file (below is the file format ) and print its value .

23466=account-1
283483=account-2
324932=account-3
87374=account-4
127632=account-5
1324237=account-6
73642=account-7
324993284=account-8
.
.
4543454=account-200

exapected output

account-1
account-2
account-3
account-4

Upvotes: 1

Views: 233

Answers (2)

Inder
Inder

Reputation: 3816

a Compact one line solution can be:

join -t "="  <(sort bf) <(sort fa) | cut -d '=' -f 2

here fa is a file containing out-put of your bash script and bf is the file that has 23466=account-1 format

the output is:

account-1
account-2
account-3
account-4

Upvotes: 1

keithpjolley
keithpjolley

Reputation: 2263

#!/bin/bash

for var1 in $(awk -F ' ' '/accountnumber/{print $1}' log.json)
do
  awk -F= '$1=="'"$var1"'"{print $2}' anotherfile
done

For a moment there was another answer that almost worked that I think is much slicker than what I wrote. Probably faster / more efficient on large files too. Here it is fixed.

awk -F ' ' '/accountnumber/{print $1}' log.json \
| sort -n \
| join -t= - accountfile \
| cut -d= -f2

Upvotes: 0

Related Questions