Sponsored Content
Top Forums Shell Programming and Scripting Awk+colpos+negative if for 20 values Post 302908637 by rajubollas on Wednesday 9th of July 2014 07:57:08 AM
Old 07-09-2014
Hi Don,

Thanks for looking into my requirement let me summerize what i am trying achieve.

i have source data in Germany_Source_1.txt, Germany_Source_2.txt ...nth.txt files which contains business keys.
Obligor_ID is business key. using given 18 Obligor_ID i am dividing Germay_Source files for Greece and Germany when Given Obligor_ID not found in each row then i need that row/line as Greece_Source_1.txt .*_2.txt....*_nth.txt file found Obligor_ID information i am keeping Greece_Source_1 ...*_2.txt....*_nth.txt.
Note: Obligor_ID column position is not a fixed position on every file

i would like to achieve based on source file i have to check Obligor_ID coulm position on the column then i have to write out put to another file whenever list of 19 Obligors not found on the row/line.

Source Data sample
Code:
$ Grermany_Source_1.txt file data
1|ACTIVE|FINANCIAL_RI|3456-89-12|456789|RTNG_DLR|RI_FIN_STMT|234
67|ACTIVE|FINANCIAL_RI|9012-45-78|012345|RTNG_DLR|RI_FIN_STMT|890
2|ACTIVE|FINANCIAL_RI|4567-90-23|567890|RTNG_DLR|RI_FIN_STMT|345
78|ACTIVE|FINANCIAL_RI|0123-56-89|123456|RTNG_DLR|RI_FIN_STMT|901
3|ACTIVE|FINANCIAL_RI|5678-01-34|678901|RTNG_DLR|RI_FIN_STMT|456
89|ACTIVE|FINANCIAL_RI|1234-67-90|234567|RTNG_DLR|RI_FIN_STMT|012
4|ACTIVE|FINANCIAL_RI|6789-12-45|789012|RTNG_DLR|RI_FIN_STMT|567
9|ACTIVE|FINANCIAL_RI|1234-67-90|234567|RTNG_DLR|RI_FIN_STMT|012
45|ACTIVE|FINANCIAL_RI|7890-23-56|890123|RTNG_DLR|RI_FIN_STMT|678
01|ACTIVE|FINANCIAL_RI|3456-89-12|456789|RTNG_DLR|RI_FIN_STMT|234
6|ACTIVE|FINANCIAL_RI|8901-34-67|901234|RTNG_DLR|RI_FIN_STMT|789
1|ACTIVE|FINANCIAL_RI|3456-89-12|456789|RTNG_DLR|RI_FIN_STMT|234
6|ACTIVE|FINANCIAL_RI|8901-34-67|901234|RTNG_DLR|RI_FIN_STMT|789
1|ACTIVE|FINANCIAL_RI|3456-89-12|456789|RTNG_DLR|RI_FIN_STMT|234
67|ACTIVE|FINANCIAL_RI|9012-45-78|012345|RTNG_DLR|RI_FIN_STMT|890
23|ACTIVE|FINANCIAL_RI|5678-01-34|678901|RTNG_DLR|RI_FIN_STMT|456
8|ACTIVE|FINANCIAL_RI|0123-56-89|123456|RTNG_DLR|RI_FIN_STMT|901
3|ACTIVE|FINANCIAL_RI|5678-01-34|678901|RTNG_DLR|RI_FIN_STMT|456
8|ACTIVE|FINANCIAL_RI|0123-56-89|123456|RTNG_DLR|RI_FIN_STMT|901
34|ACTIVE|FINANCIAL_RI|6789-12-45|789012|RTNG_DLR|RI_FIN_STMT|567
9|ACTIVE|FINANCIAL_RI|1234-67-90|234567|RTNG_DLR|RI_FIN_STMT|012
4|ACTIVE|FINANCIAL_RI|6789-12-45|789012|RTNG_DLR|RI_FIN_STMT|567
9|ACTIVE|FINANCIAL_RI|1234-67-90|234567|RTNG_DLR|RI_FIN_STMT|012

Code:
$$Obligor Test filter file
GERMANY_Obligor_filter_ctl file data

Code:
##Obligor information file
##For new obligor for search contion add "&& $colpos !=xxxxxx" in format
##make sure all condition values in single line
$colpos != 228070 && $colpos != 228071 && $colpos != 228072 && $colpos != 228073 && $colpos != 228074 && $colpos != 228075 && $colpos != 228076 && $colpos != 228077 && $colpos != 228078 && $colpos != 228079 && $colpos != 228080 && $colpos != 228081 && $colpos != 228082 && $colpos != 228083 && $colpos != 228084 && $colpos != 228085 && $colpos != 228086 && $colpos != 228196 && $colpos != 228299

Code:
##Files Structure on which position we do get Obligor/GSW ID for each file :
crt_extracts_branch_gsw_cntl file data
#################################################
#Carrs Extract File                                         GSWID Column Possition
#################################################
Grermany_Source_1.txt						3
Grermany_Source_2.txt						3
Grermany_Source_3.txt						6			
Grermany_Source_4.txt						1
Grermany_Source_5.txt						1
Grermany_Source_6.txt						4


Code:
Script Part:

if [[ -f $config_file_path/crt_extracts_branch_gswid_cntl ]]
then
        set -A FILENAME   $( cat $config_file_path/crt_extracts_branch_gswid_cntl | grep -v "^#" | awk '{ print $1 }')
        set -A COLPOS     $( cat $config_file_path/crt_extracts_branch_gswid_cntl | grep -v "^#" | awk '{ print $4 }')
        set -A CTLCOLPOS  $( cat $config_file_path/crt_extracts_branch_gswid_cntl | grep -v "^#" | awk '{ print $3 }')

	SOURCE_DIR=$( echo "/tgrrappkg3/DataStage/CONSUMER_BI/datap5/WSLGER")
	x=0
        while (( $x <  ${#FILENAME[*]}  ))
        do
                if [[ -f $SOURCE_DIR/${FILENAME[$x]} ]]
                then
                        MSG="Processing file $SOURCE_DIR/${FILENAME[$x]}:INFO"
                        echo $MSG;

                        ##DISTINCT_BRNCH_NBRS=$( cat $SOURCE_DIR/${FILENAME[$x]} | awk -F"|" -v colpos="${COLPOS[$x]}" '{ print $colpos }' | sort -u )
			##DISTINCT_OBLIGOR_NBRS=$( cat $config_file_path/Greece_Obligor_filter.txt	| grep -v "^#"| awk '{ print $1 }' | sort -u )

                        ##for OBLIGOR_NBRS in $( echo $DISTINCT_OBLIGOR_NBRS )
                        ##do
                                ##MSG="Processing branch $OBLIGOR_NBRS:$INFO"
                                ##echo $MSG;
                                PART1FILENAME=$( echo ${FILENAME[$x]} | awk -F"." '{ print $1 }')
                                PART2FILENAME=$( echo ${FILENAME[$x]} | awk -F"." '{ print $2 }')
                                NEWFILENAME=$( echo "$SOURCE_DIR/GERMANY_$PART1FILENAME.$PART2FILENAME")
				touch $NEWFILENAME
                                ##cat $SOURCE_DIR/${FILENAME[$x]}                                                                |
                                ##awk -F"|" -v colpos="${COLPOS[$x]}" -v obligor_nbr="${OBLIGOR_NBRS}" '
                                ##          ( $colpos==obligor_nbr ) { print $0 }'                                          > $NEWFILENAME
				##Reading GERMANY_Obligor_filter_ctl file
				##set -x
				if [[ -f $config_file_path/GERMANY_Obligor_filter_ctl ]]
				then 
					##this an if then else condition values for command_construction variable
					ifcondition_val=$( grep -v "^#" $config_file_path/GERMANY_Obligor_filter_ctl)
					command_construction=$( echo "cat $SOURCE_DIR/${FILENAME[$x]}  | awk -F\"|\" -v colpos=\"${COLPOS[$x]}\"  '{if ("$ifcondition_val") { print \$0 }}'  > $NEWFILENAME")
					##Executing cunstructed command with EVAL command
					eval $command_construction
                                	RC=$?
                                	if [[ $RC -ne 0 ]]
                                	then
                                        MSG="Error in creating file $NEWFILENAME:ALERT"
                                        echo $MSG;
                                        return -1
                                	else

                                        MSG="$NEWFILENAME file created:INFO"
                                        echo $MSG;
                                	fi
				fi
				##set +x
                         ##done
			MSG="Completed processing file ${FILENAME[$x]}"
                        echo $MSG;
                else
                        MSG="File not found $SOURCE_DIR/${FILENAME[$x]}.     Proceeding to next file:WARN"
                        echo $MSG;
                fi
                x=$(echo "$x+1" | bc)
        done
else

###

I have corrected command_construction variable properly, now my script is working fine. but i am still looking for better solution on below part of script to maintain it easily.

Can you please suggest with good solution in below part of script how maintain it easy.
Code:
					##this an if then else condition values for command_construction variable
					ifcondition_val=$( grep -v "^#" $config_file_path/GERMANY_Obligor_filter_ctl)
					command_construction=$( echo "cat $SOURCE_DIR/${FILENAME[$x]}  | awk -F\"|\" -v colpos=\"${COLPOS[$x]}\"  '{if ("$ifcondition_val") { print \$0 }}'  > $NEWFILENAME")
					##Executing cunstructed command with EVAL command
					eval $command_construction

Moderator's Comments:
Mod Comment Please use CODE tags for sample input and output as well as for code segments.

Last edited by Corona688; 09-08-2016 at 12:24 PM.. Reason: Replaced private data with gibberish
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help in adding positive & negative values in a column

Hi Gurus, In my file I have an amount field from position 74 to 87, which contains values starting with '+' as well as '-'. I want to add all positive values in a varible called "CREDIT" and all negative values in a variable "DEBIT". I know, we can use grep to identify values with positive and... (4 Replies)
Discussion started by: berlin_germany
4 Replies

2. Shell Programming and Scripting

printf in bash shell not printing negative values

hi i am using printf in a script and it is not printing negative values..i have to use printf to get rid of the newline..here is my code: fin=`echo $a - $b | bc` printf "${fin}," >> test these statements are in a loop. here is what i get when i try to subtract 4 from 8: ./scr1: line... (2 Replies)
Discussion started by: npatwardhan
2 Replies

3. Shell Programming and Scripting

Sorting positive and negative values

Hello, I have a list like this : 1 2 -4 0 -3 -7 5 6 etc. Is there a way to remove all the positive values and print only the negative values, without using grep, sed or awk? Thanks, Prasanna (4 Replies)
Discussion started by: prasanna1157
4 Replies

4. Shell Programming and Scripting

Find Where Values Change From Positive To Negative and viceversa

Hi all, I have a file that looks like shown below. I want to find places where the value in column 2 change from negative to positive and vice versa and return the value on column 1 at that point. I wonder if this is possible in shell script or awk .. please help! Here is the original data ... (6 Replies)
Discussion started by: malandisa
6 Replies

5. UNIX for Dummies Questions & Answers

Snmp Disk Problem = Negative Values

Ok, so i monitor disk space on remote machines using snmp. Works great for me. But whenever a particular partition happens to have Terabytes of data, snmp starts reporting negative values. Can someone please tell me how to get around this problem? The AllocationUnit is 512 bytes. Weird... (0 Replies)
Discussion started by: SkySmart
0 Replies

6. Shell Programming and Scripting

error- multiplying negative decimal values

a=10.02 pattern=-11.01 b=$(echo | awk '{ print $a*$pattern}') echo $b its not working even ALso tried `expr $a \* $pattern` No LUCK (3 Replies)
Discussion started by: saluja.deepak
3 Replies

7. Shell Programming and Scripting

replacing negative values in a column with zero

Hi, i need help on replacing negative values in a column with 0. any quick fix on this? thanks much. for instance, input: 1 2.3 -0.4 -25 12 13 45 -12 desired output 1 2.3 0 0 12 13 45 (4 Replies)
Discussion started by: ida1215
4 Replies

8. Shell Programming and Scripting

Conavert negative values to Zeros

Can anyone please assist me? Please find the attached input and output file for ur reference. a)Incase if i get negative value (ex:-000100) in the 11th column then i have to convert the value to 0000000(7 zeros-length is 7) and then print the entire record. b)Incase if there is no... (2 Replies)
Discussion started by: vinus
2 Replies

9. Shell Programming and Scripting

How to sum the value with negative values?

Hi Gurus, I have requirement need to sum the value, the logic is if the value is negative then time -1, I tried below two ways. one is failed, another one doesn't work. awk -F"," '{if($8< 0 $8*-1 else $8) sum+=$8}{print sum, $8} END{printf("%.2f\n",sum)}' awk -F","... (4 Replies)
Discussion started by: ken6503
4 Replies
All times are GMT -4. The time now is 08:31 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy