Visit Our UNIX and Linux User Community

Top Forums Shell Programming and Scripting Problem with awk and if statement Post 302255529 by manmeet on Thursday 6th of November 2008 03:33:54 PM
Old 11-06-2008
Problem with awk and if statement

Hi,
I have a task where i need to compare columns of two files.

First file is $REG_InputFileName:

"UPDATE","1010021126","1-01-01","USD"
"UPDATE","1013000101","1-01-01","THB"
"UPDATE","1013010107","1-01-01","THB"
"UPDATE","1110011122","1-01-01","USD"
"UPDATE","1110020111","1-01-01","THB"
"UPDATE","1110020111","1-01-01","USD"
"UPDATE","1110020113","1-01-01","THB"
"UPDATE","1620020409","1-01-01","USD"
"UPDATE","1620020612","1-01-01","USD"
"UPDATE","1620021126","1-01-01","USD"
"UPDATE","1910020411","1-01-01","USD"
"UPDATE","1910020722","1-01-01","USD"
"UPDATE","1910030021","1-01-01","THB"
"UPDATE","1910031191","1-01-01","THB"
"UPDATE","1910031221","1-01-01","THB"
"UPDATE","2620021162","1-01-01","USD"


Second file is $TargetSeqPath/Ref.tmp:

001.,ARE
002.,ARE
003.,ARE
011.,ARE
012.,ARE
021.,BHR
031.,BHR
041.,QAT
043.,QAT
051.,QAT
061.,OMN
071.,OMN
081.,LBN
091.,LBN
101.,JOR
111.,JOR
121.,EGY
131.,EGY
141.,MUS
151.,MUS
162.,IRQ
181.,PAK
191.,PAK


In this, i need to compare first three characters of 2nd column of $REG_InputFileName with first 3 characters 1st column of $TargetSeqPath/Ref.tmp

if there is match then the whole record should go to a file which is in the directory named same as second column of $TargetSeqPath/Ref.tmp

for e.g.

first record of $REG_InputFileName is

"UPDATE","1010021126","1-01-01","USD"
so first three characters will be 101 now i need to lookup this 101 into $TargetSeqPath/Ref.tmp and there is match ;
001.,ARE

then this whole record "UPDATE","1010021126","1-01-01","USD" should go to a directory named as ARE

i have following code for this :

while read i
do
echo $i > $TargetSeqPath/Ref.tmp
OutDir=`awk -F"," '{print $2}' $TargetSeqPath/Ref.tmp`
awk -F"," 'NR==FNR {col1[substr($1,1,3)]=$2} NR!=FNR { if (col1[substr($2,2,3)]=="") {next} print $0}' $TargetSeqPath/Ref.tmp $REG_InputFileName >>$Target
SeqPath/$OutDir/$OutFile-$mdt.txt
done < $RefInputFileName


this is code is working fine and i am getting everything mentioned above. But the problem is if there is match.
for e.g.

"UPDATE","2620021162","1-01-01","USD"

then this record should go to a directory named as "others"

Can anybody help me how to check this mismatch and send the record to other directory

Thanks
 
Test Your Knowledge in Computers #629
Difficulty: Easy
In PHP, False == 0;
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

if statement problem

I keep getting an error at line 21, it doesn't like my if statement. Previously I have tried using (( )), but still get errors. The current error is that server_busy is not found. This is the script: #! /bin/ksh server_busy="na" for file in $1 $2 $3 $4 $5 $6 do echo " ${file}\t\c" ... (1 Reply)
Discussion started by: coughlin74
1 Replies

2. UNIX for Dummies Questions & Answers

if statement problem

hi all. i just have a very small problem. i have a menu of 7 choices. i want an if statement so that if the user chooses anything except inside the 1 to 7 range, i can handle the error for it. i tried this: if ] then ....... fi (but it dont work) ...any suggestions? ... (4 Replies)
Discussion started by: djt0506
4 Replies

3. UNIX for Dummies Questions & Answers

if statement problem

See https://www.unix.com/shell-programming-scripting/96846-if-statement-problem.html (0 Replies)
Discussion started by: f_o_555
0 Replies

4. Shell Programming and Scripting

if statement problem

Hi I have a bash script like this if then echo "A" else echo "B" fi $1 is something like 02350 (there is always a trailing '0') and I would like to have an if based on the value of the digits after the 0. Can anybody help? Thanks, Sarah (3 Replies)
Discussion started by: f_o_555
3 Replies

5. Shell Programming and Scripting

problem with if/while statement

I'm trying to have the script check if a file has data or not, and then process it accordingly. If the file is empty, I want it to return "nothing to do", if not, I want it to process the file line by line. This is what I have so far, but it always returns "nothing to do", even if the file is not... (4 Replies)
Discussion started by: ddrew78
4 Replies

6. UNIX for Dummies Questions & Answers

Having problem with if statement

Could someone help me out with this if statement? It's supposed to get a person's website, but it isn't working when I run it. website="" echo "Would you like to enter a website? Enter Yes/No" read choice if then while do echo "Please enter a website:"; read... (4 Replies)
Discussion started by: Sotau
4 Replies

7. Shell Programming and Scripting

if statement problem

Writing my script and I'm banging my head on the desk right now ... My biggest problem is the 3rd IF statement where I check if the username exists. Doing the grep command on it's own in the shell gives me a 1 or 0 value. Running the script, it always returns a false value (runs the ELSE... (4 Replies)
Discussion started by: ADay2Long
4 Replies

8. Shell Programming and Scripting

Awk or If/statement Calculation Problem

#!/bin/sh CURRENTSTATE=2 CSVCSTATE=2 LASTSTATECHANGE=8 CSVCSTATEAGE=5 if (($CURRENTSTATE==$CSVCSTATE))&&(($LASTSTATECHANGE>=$CSVCSTATEAGE)) echo GREAT fi returns: ./aff: line 12: syntax error near unexpected token `fi' ./aff: line 12: `fi' what am i doing wrong here? (6 Replies)
Discussion started by: SkySmart
6 Replies

9. Shell Programming and Scripting

Awk/sed problem to write Db insertion statement

Hi There, I am trying to load data from a csv file into a DB during our DB migration phase. I am successfully able export all data into a .csv file but those have to rewritten in terms insert statement which will allow for further population of same data in different DB My exiting csv record... (6 Replies)
Discussion started by: bhaskar_m
6 Replies

10. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies
DBLINK_BUILD_SQL_UPDATE(3)				  PostgreSQL 9.2.7 Documentation				DBLINK_BUILD_SQL_UPDATE(3)

NAME
dblink_build_sql_update - builds an UPDATE statement using a local tuple, replacing the primary key field values with alternative supplied values SYNOPSIS
dblink_build_sql_update(text relname, int2vector primary_key_attnums, integer num_primary_key_atts, text[] src_pk_att_vals_array, text[] tgt_pk_att_vals_array) returns text DESCRIPTION
dblink_build_sql_update can be useful in doing selective replication of a local table to a remote database. It selects a row from the local table based on primary key, and then builds a SQL UPDATE command that will duplicate that row, but with the primary key values replaced by the values in the last argument. (To make an exact copy of the row, just specify the same values for the last two arguments.) The UPDATE command always assigns all fields of the row -- the main difference between this and dblink_build_sql_insert is that it's assumed that the target row already exists in the remote table. ARGUMENTS
relname Name of a local relation, for example foo or myschema.mytab. Include double quotes if the name is mixed-case or contains special characters, for example "FooBar"; without quotes, the string will be folded to lower case. primary_key_attnums Attribute numbers (1-based) of the primary key fields, for example 1 2. num_primary_key_atts The number of primary key fields. src_pk_att_vals_array Values of the primary key fields to be used to look up the local tuple. Each field is represented in text form. An error is thrown if there is no local row with these primary key values. tgt_pk_att_vals_array Values of the primary key fields to be placed in the resulting UPDATE command. Each field is represented in text form. RETURN VALUE
Returns the requested SQL statement as text. NOTES
As of PostgreSQL 9.0, the attribute numbers in primary_key_attnums are interpreted as logical column numbers, corresponding to the column's position in SELECT * FROM relname. Previous versions interpreted the numbers as physical column positions. There is a difference if any column(s) to the left of the indicated column have been dropped during the lifetime of the table. EXAMPLES
SELECT dblink_build_sql_update('foo', '1 2', 2, '{"1", "a"}', '{"1", "b"}'); dblink_build_sql_update ------------------------------------------------------------- UPDATE foo SET f1='1',f2='b',f3='1' WHERE f1='1' AND f2='b' (1 row) PostgreSQL 9.2.7 2014-02-17 DBLINK_BUILD_SQL_UPDATE(3)

Featured Tech Videos

All times are GMT -4. The time now is 08:08 AM.
Unix & Linux Forums Content Copyright 1993-2021. All Rights Reserved.
Privacy Policy