Sponsored Content
Top Forums Shell Programming and Scripting Compare values in two files. For matching rows print corresponding values from File 1 in File2. Post 302642417 by Santoshbn on Thursday 17th of May 2012 11:21:20 AM
Old 05-17-2012
Hello Jack,

This is what I am trying to do :

1) Running the below query against the DB2 database.

Code:
db2 "SELECT SUBSTR(T1.AGENT_ID,1,8) AS APPL_HANDLE,
        CASE APPL_STATUS
        WHEN 'AUTONOMOUS_WAIT' THEN 'AUTONOMOUS_WAIT'
        WHEN 'BACKUP' THEN 'DB_BACKUP'
        WHEN 'COMMIT_ACT' THEN 'COMMIT'
        WHEN 'COMP' THEN 'COMPILING'
        WHEN 'CONNECTED' THEN 'CONNECTED'
        WHEN 'CONNECTPEND' THEN 'CONNECT_PENDING'
        WHEN 'CREATE_DB' THEN 'CREATE_DB'
        WHEN 'DECOUPLED' THEN 'DECOUPLED'
        WHEN 'DISCONNECTPEND' THEN 'DISCONNECT_PENDING'
        WHEN 'INTR' THEN 'REQUEST_INTERRUPTED'
        WHEN 'IOERROR_WAIT' THEN 'TBLSPCE_DISABLE_WAIT'
        WHEN 'LOAD' THEN 'LOAD'
        WHEN 'LOCKWAIT' THEN 'LOCKWAIT'
        WHEN 'QUIESCE_TABLESPACE' THEN 'QUIESCE_TBLSPCE'
        WHEN 'RECOMP' THEN 'RECOMPILING'
        WHEN 'REMOTE_RQST' THEN 'FEDRATD_RQST_PENDING'
        WHEN 'RESTART' THEN 'DB_RESTART'
        WHEN 'RESTORE' THEN 'DB_RESTORE'
        WHEN 'ROLLBACK_ACT' THEN 'ROLLBACK'
        WHEN 'ROLLBACK_TO_SAVEPOINT' THEN 'ROLLBACK_SAVEPOINT'
        WHEN 'TEND' THEN 'TRANSACTION_ENDED'
        WHEN 'THABRT' THEN 'GLOBAL_TRANSN_ROLLBK'
        WHEN 'THCOMT' THEN 'GLOBAL_TRANSN_COMMIT'
        WHEN 'TPREP' THEN 'TRANSACTION_PREPARED'
        WHEN 'UNLOAD' THEN 'DATA_FAST_UNLOAD'
        WHEN 'UOWEXEC' THEN 'EXECUTING'
        WHEN 'UOWQUEUED' THEN 'QUEUED'
        WHEN 'UOWWAIT' THEN 'WAITING'
        WHEN 'WAITFOR_REMOTE' THEN 'PENDING_REMOTE_RQST'
        ELSE ''
        END AS APPL_STATUS,
        CAST(T1.STATUS_CHANGE_TIME AS DATE)||'-'||CAST(T1.STATUS_CHANGE_TIME AS TIME) as STATUS_CHANGE_TIME,
        SUBSTR(PRIMARY_AUTH_ID,1,15) AS AUTH_ID,
        SUBSTR(T1.TPMON_CLIENT_USERID,1,15) as CLIENT_USERID,
        T2.STMT_OPERATION,
        SUBSTR(T2.STMT_TEXT,1,2000) as STMT_TEXT
FROM TABLE(SNAP_GET_APPL_INFO(CAST(NULL AS VARCHAR(128)),-1)) AS T1,
     TABLE(SNAP_GET_STMT('',-1)) AS T2
WHERE T1.AGENT_ID=T2.AGENT_ID"|sed -e 's/[ \t]*$//' -e '5d' -e 'N;$!P;$!D;$d' > File1.out


2) The field "SUBSTR(T1.TPMON_CLIENT_USERID,1,15) as CLIENT_USERID" in the query is not returning any value but the rest of fields in the SELECT are.

3) Since I am not getting the value for the field - "SUBSTR(T1.TPMON_CLIENT_USERID,1,15) as CLIENT_USERID", I am running the below query to get the output. - (Checked in the DB2 Forums about it. Yet to get a response on this)

Code:
db2 "get snapshot for applications on $2 AT DBPARTITIONNUM $CORD_PART "| grep -e "Application handle                         =
Client login ID                            = "|awk -F "=" '{print $2}' > Intermediate.out

4) I am then doing a row to column transpose of the output present in the file - Intermediate.out

Code:
paste -d' ' - - < CON_AUTH_CLNT.out > File2.out

5) Comparing the value "SUBSTR(T1.AGENT_ID,1,8) AS APPL_HANDLE" in File1.out with "Application handle" value in File2.out, if there is a match printing the value "Client login ID" from File2.out in the 5th column of File1.out. (Suggested by Otheus)

Code:
awk 'NR==FNR{A[$1]=$2;next} A[$1]{$5=A[$1];print}' File2.out File1.out > Final.out

6) The script will be run 30 mins once and 24/7 in multiple database environments. A team will review the results periodically. The output can sometimes be a 1000 lines which makes it hard to read (SQL Statements can themselves run into 100's of lines).

7) Below is a portion of the output.

Code:
19296 WAITING 04/27/2012-10:05:15 DWYGSTC RPRRYM OPEN SELECT EMPID,EMPNAME,EMPADD
FROM EMPLOYEE
WHERE EMPID=101
19223 WAITING 04/27/2012-13:00:00 SARMLTE BRIJAA3 NONE -
19222 WAITING 04/27/2012-13:20:42 SMBRMTSE ARAFAY NONE -
19439 WAITING 04/27/2012-13:13:51 ZMDRTVEL LRATAG STATIC_ROLLBAK -
19445 EXECUTING 04/27/2012-13:23:06 SARMLTE - NONE -
19227 WAITING 04/27/2012-11:47:17 KKAYAL HRAEAV EXECUTE INSERT INTO ADDRESS (SELECT CITY, STATE, COUNTRY
FROM EMP_ADDRESS
WHERE BIRTHCITY='XYZ'
19154 WAITING 04/27/2012-13:22:31 SARMLTET ARADA1 NONE -
19226 WAITING 04/27/2012-13:00:00 VKAZA1 FRAJAX NONE  -
19225 CONNECTED 04/27/2012-08:20:35 BMCRTSEL SRAJAS FETCH SELECT COUNT(1) FROM COUNTRY WITH UR
19369 WAITING 04/27/2012-11:35:40 GCZILLG OCRILL STATIC_COMMIT -
19224 WAITING 04/27/2012-08:20:35 SMQRTQEL ARPJAD NONE -
82395 WAITING 04/27/2012-13:21:14 JSARTAEL IRKJAY PREP_COMMIT -

Thanks
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

perl script to print to values in rows and columns

Hi guys I want to print the values by using this script but its giving the no of rows and columns as input instead of values Would you plz help me on this FILE- chr1.txt 1981 1 1971 1 1961 1 1941 1 perl script #!/usr/bin/perl -w $infile1 = 'chr1.txt'; $outfile3 = 'out3.txt'; ... (3 Replies)
Discussion started by: nogu0001
3 Replies

2. Shell Programming and Scripting

compare two columns of different files and print the matching second file..

Hi, I have two tab separated files; file1: S.No ddi fi cu o/l t+ t- 1 0.5 0.6 o 0.1 0.2 2 0.2 0.3 l 0.3 0.4 3 0.5 0.8 l 0.1 0.6 ... (5 Replies)
Discussion started by: vasanth.vadalur
5 Replies

3. Shell Programming and Scripting

AWK: read values from file1; search for values in file2

I have read another post about this issue and am wondering how to adapt it to my own, much simpler, issue. I have a file of user IDs like so: 333333 321321 546465 ...etc I need to take each number and use it to print records wherein the 5th field matches the user ID pulled from the... (2 Replies)
Discussion started by: Bubnoff
2 Replies

4. Shell Programming and Scripting

Print rows in reverse order if values decrease along the column

Hi, Guys. Please help me to find solution to this problem using shell scripting. I have an INPUT file with 4 columns separated by tab. Each block of records is separated by ----- ----- Sample1 5402 6680 Pattern01 Sample2 2216 2368 Pattern02... (6 Replies)
Discussion started by: sam_2921
6 Replies

5. Shell Programming and Scripting

Get values from different columns from file2 when match values of file1

Hi everyone, I have file1 and file2 comma separated both. file1 is: Header1,Header2,Header3,Header4,Header5,Header6,Header7,Header8,Header9,Header10 Code7,,,,,,,,, Code5,,,,,,,,, Code3,,,,,,,,, Code9,,,,,,,,, Code2,,,,,,,,,file2... (17 Replies)
Discussion started by: cgkmal
17 Replies

6. Shell Programming and Scripting

Print columns matching to specific values

Hello Friends, I have a CDR file and i need to print out 2 columns with their field position which matches to some constant values, a part of input file CZ=1|CZA=1|DIAL=415483420001|EE=13|ESF=1|ET=|FF=0|9|MNC=99|MNP=9041|MTC=0|NID=2|NOA=international|ON=1| OutPut ... (3 Replies)
Discussion started by: EAGL€
3 Replies

7. Shell Programming and Scripting

How to print in awk matching $1 values ,to $1,$4 example given.?

Hi Experts, I am trying to get the output from a matching pattern but unable to construct the awk command: file : aa bb cc 11 dd aa cc 33 cc 22 45 68 aa 33 44 44 dd aa cc 37 aa 33 44 67 I want the output to be : ( if $1 match to "aa" start of the line,then print $4 of that line, and... (3 Replies)
Discussion started by: rveri
3 Replies

8. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

9. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

10. UNIX for Beginners Questions & Answers

Compare values in multiple rows in one column using awk

I would like to compare values in column 8, and grep the ones where the different is > 1, columns 1 and 2 are the key for array. Every 4 rows the records values in columns 1 and 2 changed. Then, the comparison in the column 8 need to be done for the 4 rows everytime columns 1 and 2 changed ... (4 Replies)
Discussion started by: jiam912
4 Replies
All times are GMT -4. The time now is 08:22 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy