1

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
2
  • Why is your file named with a .json extension if its format isn't actually JSON? Commented Aug 14, 2018 at 16:16
  • Please avoid "Give me the codez" questions. Instead show the script you are working on and state where the problem is. Also see How much research effort is expected of Stack Overflow users? Commented Aug 15, 2018 at 1:37

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0
#!/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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.