Compare the string in different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare the string in different files
# 1  
Old 10-29-2014
Compare the string in different files

Hi,
There is a file A.txt which contains a list of strings.
Code:
A.txt
QASSBRK1
QASSBRK2

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

Code:
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.
# 2  
Old 10-29-2014
Hello Anusha,

Following may help you in same.

Code:
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
# 3  
Old 10-29-2014
Quote:
Originally Posted by Anusha M
Hi,
There is a file A.txt which contains a list of strings.
Code:
A.txt
QASSBRK1
QASSBRK2

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

Code:
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.
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:
Code:
another_ccommand "$(dspmq | awk -F '[()]' 'FNR == NR {a[$2]; next} $1 in a' A.txt FS='[ \t]' -)"

# 4  
Old 10-29-2014
Code:
dspmq | cut -f2 -d "(" | cut -f1 -d ")" > B.txt
fgrep -f A.txt B.txt
rm B.txt A.txt

# 5  
Old 10-29-2014
Quote:
Originally Posted by ghostdog74
Code:
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 fromdspmq contains a parenthesized string before any spaces or tabs. Since we don't know what that output actually contains,we're both flying blind.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. Shell Programming and Scripting

Two files String compare

file1.txt col1 col2 col3 b100:07-24-2018:test1 b102:04-24-2017:test2 b103:04-24-2017:test3 b104:04-24-2017:test3 file2.txt col4 col5 b100:name1@email.com b102:name2@email.com b103:name3@email.com Comparing string from file1.txt(col1) each record with... (3 Replies)
Discussion started by: ramgk5
3 Replies

3. UNIX for Dummies Questions & Answers

Search for string in a file then compare it with excel files entry

All, i have a file text.log: cover6 cover3 cover2 cover4 other file is abc.log as : 0 0 1 0 Then I have a excel file result.xls that contains: Name Path Pass cover2 cover3 cover6 cover4 (1 Reply)
Discussion started by: Anamika08
1 Replies

4. Shell Programming and Scripting

Compare columns of multiple files and print those unique string from File1 in an output file.

Hi, I have multiple files that each contain one column of strings: File1: 123abc 456def 789ghi File2: 123abc 456def 891jkl File3: 234mno 123abc 456def In total I have 25 of these type of file. (5 Replies)
Discussion started by: owwow14
5 Replies

5. Shell Programming and Scripting

Compare different String in Log Files

Hi Guys , sorry for my first post but a newbie here need some help on my simple scripts. I have some scripts below that count the job started and the job finished and is the job started and job finished equal ..then all job was successfully run and finished on that day. but sometime the was... (3 Replies)
Discussion started by: thermometer
3 Replies

6. UNIX for Dummies Questions & Answers

Compare 2 files print the lines of file 2 that contain a string from file 1

Hello I am a new unix user, and I have a work related task to compare 2 files and print all of the lines in file 2 that contain a string from file 1 Note: the fields are in different columns in the files. I suspect the is a good use for awk? Thanks for your time & help File 1 123 232 W343... (6 Replies)
Discussion started by: KevinRidley
6 Replies

7. Shell Programming and Scripting

Require compare command to compare 4 files

I have four files, I need to compare these files together. As such i know "sdiff and comm" commands but these commands compare 2 files together. If I use sdiff command then i have to compare each file with other which will increase the codes. Please suggest if you know some commands whcih can... (6 Replies)
Discussion started by: nehashine
6 Replies

8. Shell Programming and Scripting

String compare in multiple files

Hi I have a requirement to process number of files matching a criteria. The resulted file would be processed indivdually looking for a particular string until another one found lines afterwards. Then look for the occurrence of another string in the result count and display/return the result. ... (13 Replies)
Discussion started by: arungn
13 Replies

9. UNIX for Dummies Questions & Answers

How to compare a string with IP

Hi, I have a variable with value tmp2=123.45.175.243, I am taking this value from a network file. In the script I need to check whether the variable has only numerals and .(dot). if ..." ] then printf "SUCCESS\n" else printf "FAILED\n" fi doesnt work, is there a alternate... (1 Reply)
Discussion started by: happyrain
1 Replies

10. Shell Programming and Scripting

compare string in two files

i have two files: file1.txt AA BB DD EE file2.txt AA,AAA BB,BBB CC,CCC DD,DDD EE,EEE FF,FFF i want an output file: file3.txt AA,AAA BB,BBB (2 Replies)
Discussion started by: MiLKTea
2 Replies
Login or Register to Ask a Question