Compare the string in different files

Hi,
There is a file A.txt which contains a list of strings.

A.txt
QASSBRK1
QASSBRK2

And in B.txt, command is written to generate output of dspmq with AWK script

B.txt
QMGR=`dspmq | awk '{print $1}' | cut -f2 -d "(" |  cut -f1 -d ")"`


Output of B.txt is
QASSBRK1
QASSBRK2
QASSBRK3

The requirement is to compare A.txt output with the above B.txt output and get the matched strings. Pass those strings as a argument to another command.

Please help to get this fixed.

Thanks.

Hello Anusha,

Following may help you in same.

cat A.txt
QASSBRK1
QASSBRK2
 
cat B.txt
QASSBRK1
QASSBRK2
QASSBRK3

awk 'FNR==NR{X[$1]=$0;next} ($1 in X){print X[$1]}' A.txt B.txt
QASSBRK1
QASSBRK2

Not sure about passing arguments, if you can give us more information on same it will be helpful for us to guide/help you.

Thanks,
R. Singh

If B.txt contains the command you specified above, there will be no output at all (unless there are diagnostic messages produced by dspmq ). Any output printed to standard output by that pipeline is stored in the variable QMGR , but since that variable is never referenced after it is set, that script does not produce any output on the standard output file descriptor.

To get what you seem to want, you could try something like:

another_ccommand "$(dspmq | awk -F '[()]' 'FNR == NR {a[$2]; next} $1 in a' A.txt FS='[ \t]' -)"
dspmq | cut -f2 -d "(" | cut -f1 -d ")" > B.txt
fgrep -f A.txt B.txt
rm B.txt A.txt

Why would you remove A.txt and B.txt ? I didn't see anything in the OP's posts indicating that either file would no longer be needed after one run of this script.

Why invoke cut twice and fgrep once when a single invocation of awk easily replaces all three.

Of course we are both assuming that the output from dspmq contains a parenthesized string before any spaces or tabs. Since we don't know what that output actually contains,we're both flying blind.