How to use same variable value inside as well as outside of the awk command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use same variable value inside as well as outside of the awk command?
# 8  
Old 10-05-2010
Thanks for Reply,But i need something different

Sorry All to disturb you again.
May be i m checking wrong. i need somthing like that.

I am using propety file as HandofValidation.property file which contains following data like

--The propety file has some constant which denotes that for EUR region 57 ~ count now from this the script should check whether in count.txt in row number 1 for EUR region ~ count are 57 are not if yes then check next row and so on.....

--Actually now my script gives proper result like in which line has more tilda than first row and gives an error line also .. But my boss want if first line has wrong ~ then my script will fail. So he want to keep all the constants in property file so that that count for perticular region can be take from property file.
--

--Below i am giving how i am using all the files you can give your idea on that.

--HandoffValidation.property
INTELLECT_EUR_DEAL_FLD_COUNT=57
INTELLECT_EUR_CASHFLOW_FLD_COUNT=14
INTELLECT_TOK_DEAL_FLD_COUNT=57
INTELLECT_TOK_CASHFLOW_FLD_COUNT=14
INTELLECT_NY_DEAL_FLD_COUNT=51
INTELLECT_NY_CASHFLOW_FLD_COUNT=14
INTELLECT_CA_DEAL_FLD_COUNT=51
INTELLECT_CA_CASHFLOW_FLD_COUNT=14


-count.txt
OPICS~CA~EOD~888~2010-10-01 01:01:29~20100930~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
OPICS~CA~EOD~888~2010-10-01 01:01:29~20100930~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
OPICS~CA~EOD~888~2010-10-01 01:01:29~20100930~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


--my script
-new.sh
Code:
#!/bin/bash
WORK_DIR=/export/opt/rtrupld/autosys/scripts_old
DATA_DIR=/cdirect/rtrupld/incoming/intellect
#LOG=/export/opt/rtrupld/autosys/logs/impIntEurCshFlw.log
data=`ls $WORK_DIR/count.txt`
propfile=$WORK_DIR/HandoffValidation.properties

  awk ' END {print NR}' $data
  
    awk ' {          sum=0; 
  
       print"::INT:::::",var
 print "Total lines",NR
 for(i=1; i<= length($0); i++) {if (substr($0,i,1)=="~" ) {sum++} }  
        if (NR !=1)
 {
  print "Total lines:::",NR
         print "oldsum :::",oldsum
         print "sum :::",sum
        if (oldsum==sum){
        print "oldsum",oldsum
 }
        else
        {
           if(oldsum!=sum) 
              {print "error in ", $data," at line   ",NR,  " tilde count=",sum}
       exit;
        } } else{
 oldsum=sum
 print "oldsum at last",oldsum
 }
   
  }'  $data

--OPUTPUT
3
::INT:::::
Total lines 1
oldsum at last 52
::INT:::::
Total lines 2
Total lines::: 2
oldsum ::: 52
sum ::: 52
oldsum 52
::INT:::::
Total lines 3
Total lines::: 3
oldsum ::: 52
sum ::: 59
error in OPICS~CA~EOD~888~2010-10-01 01:01:29~20100930~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ at line 3 tilde count= 59


I need output like

above output but it should check variable value.
please suggest on my property file also. whether it is wrong or correct.
--Waiting for your reply.

Thanks & Regards
Ganesh

---------- Post updated at 02:34 PM ---------- Previous update was at 02:15 PM ----------

Hi pravin,
i am gettin following error after using your solution

grep: can't open .properties
awk: syntax error near line 1
awk: bailing out near line 1


--im using like
Code:
#!/bin/bash
WORK_DIR=/export/opt/rtrupld/autosys/scripts_old
DATA_DIR=/cdirect/rtrupld/incoming/intellect
#LOG=/export/opt/rtrupld/autosys/logs/impIntEurCshFlw.log
data=`ls $WORK_DIR/count.txt`
propfile=$WORK_DIR/HandoffValidation.properties
 
awk -v v1=`grep "INTELLECT_EUR_DEAL_FLD_COUNT"  $propfile | cut -d"=" -f2` -F'~' '{if (NF-1 != v1) {print "Error in " $0 " at line number "NR " tilde count= " NF-1 }}' $data


---------- Post updated at 03:56 PM ---------- Previous update was at 02:34 PM ----------

Hi Franklin,
this is not double post of my thread.The reply which i got according to that i replied on that comments.I thought My problem has not getting cleared so i posted in detail what i actually need. in the last thread i explained everything in detail so anybady can understand the proble and give a exact solution.Excuse me if i broke the ruls .Please keep my thread open so i can get correct solution.
Thanks & regards,
Ganesh

Last edited by vbe; 10-05-2010 at 09:58 AM.. Reason: please use code tags: in forum rules Additional helpful guidelines for all posters -point B
# 9  
Old 10-05-2010
HandoffValidation.property OR HandoffValidation.properties

is the below file file exist ?
Code:
/export/opt/rtrupld/autosys/scripts_old/HandoffValidation.properties

If yes then try this,

Code:
#!/bin/sh
WORK_DIR=/export/opt/rtrupld/autosys/scripts_old
DATA_DIR=/cdirect/rtrupld/incoming/intellect
#LOG=/export/opt/rtrupld/autosys/logs/impIntEurCshFlw.log
data=$WORK_DIR/count.txt
propfile=$WORK_DIR/HandoffValidation.properties

awk -v v1=`grep "INTELLECT_EUR_DEAL_FLD_COUNT" $propfile | cut -d"=" -f2` -F'~' '{if (NF-1 != v1) {print "Error in " $0 " at line number "NR " tilde count= " NF-1 }}' $data

# 10  
Old 10-05-2010
Its giving following error

Hi Pravin,
Its giving an error like

awk: syntax error near line 1
awk: bailing out near line 1

And what about my awk script no need to use.Also is this script scan 50 thousand records in 1 or 2 mins.

I used all the file name properly also correctd according to your comments and then tested.Giving error.Could you please check this.

Thanks & Regards,
Ganesh
# 11  
Old 10-05-2010
Hi, Its working fine at my end. See the below code and o/p. So post the command which your typing

Code:
[root@powerbroker ~]# echo $data
count.txt
[root@powerbroker ~]# echo $propfile
HandoffValidation.properties
[root@powerbroker ~]# cat HandoffValidation.properties
INTELLECT_EUR_DEAL_FLD_COUNT=21
INTELLECT_EUR_CASHFLOW_FLD_COUNT=14
INTELLECT_TOK_DEAL_FLD_COUNT=57
INTELLECT_TOK_CASHFLOW_FLD_COUNT=14
INTELLECT_NY_DEAL_FLD_COUNT=51
INTELLECT_NY_CASHFLOW_FLD_COUNT=14
INTELLECT_CA_DEAL_FLD_COUNT=51
INTELLECT_CA_CASHFLOW_FLD_COUNT=14
[root@powerbroker ~]# cat count.txt
INTELLECT~JP~111~0~A~2jp10212991~RR~~~~~~~~~~~~~~~
INTELLECT~JP~111~0~A~2jp10212991~RR~~~~~~~~~~~~~~~
INTELLECT~JP~111~0~A~2jp10212991~RR~~~~~~~~~~~~~~~
INTELLECT~JP~111~0~A~2jp10212991~RR~~~~~~~~~~~~~~~~
INTELLECT~JP~111~0~A~2jp10212991~RR~~~~~~~~~~~~~~~
INTELLECT~JP~111~0~A~2jp10212991~RR~~~~~~~~~~~~~~~
INTELLECT~JP~111~0~A~2jp10212991~RR~~~~~~~~~~~~~~~

Invocation

Code:
[root@powerbroker ~]# awk -v v1=`grep "INTELLECT_EUR_DEAL_FLD_COUNT" $propfile | cut -d"=" -f2` -F'~' '{if (NF-1 != v1) {print "Error in " $0 " at line number "NR " tilde count= " NF-1 }}' $data

O/P
Code:
Error in INTELLECT~JP~111~0~A~2jp10212991~RR~~~~~~~~~~~~~~~~ at line number 4 tilde count= 22

# 12  
Old 10-05-2010
Workin if i use nawk

Hi Pravin,
Thanks for quick reply,
yes its working if i use "nawk" insted of "awk" but i have one more problem , As you see in properties file around 8 constants are there.
example.
INTELLECT_EUR_DEAL_FLD_COUNT=51
INTELLECT_EUR_CASHFLOW_FLD_COUNT=14
INTELLECT_TOK_DEAL_FLD_COUNT=57
INTELLECT_TOK_CASHFLOW_FLD_COUNT=14
INTELLECT_NY_DEAL_FLD_COUNT=51
INTELLECT_NY_CASHFLOW_FLD_COUNT=14
INTELLECT_CA_DEAL_FLD_COUNT=51
INTELLECT_CA_CASHFLOW_FLD_COUNT=14


then how to use these multiple constants in grep command so that no need to chang variable every tine in script , can we use variable over ther?

Thanks & Regards,
Ganesh
# 13  
Old 10-05-2010
You want to verify 8 constant against count.txt ?
then try this,
Code:
#!/bin/sh
WORK_DIR=/export/opt/rtrupld/autosys/scripts_old
DATA_DIR=/cdirect/rtrupld/incoming/intellect
#LOG=/export/opt/rtrupld/autosys/logs/impIntEurCshFlw.log
data=$WORK_DIR/count.txt
propfile=$WORK_DIR/HandoffValidation.properties

while read line
do
field_name=`echo $line | cut -d"=" -f1`
cnt=`echo $line | cut -d"=" -f2`
echo "----------------------------------------------------------------"
echo "$line"
echo "----------------------------------------------------------------"
awk -v v1=$cnt -F'~' '{if (NF-1 != v1) {print "Error in " $0 " at line number "NR " tilde count= " NF-1 }}' $data
done < $propfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Variable and awk inside for loop

Thanks all for taking time out and reading this thread and big Thanks to all who have come forward for rescue. Background: I have a variable "nbrofcols" that has number of columns from a data file. Now, using this count in for loop, I am trying to get the maximum length of each column present... (7 Replies)
Discussion started by: svks1985
7 Replies

2. Shell Programming and Scripting

Variable assignment inside awk

Hi, Was hoping someone could help with the following: while read line; do pntadm -P $line | awk '{if (( $2 == 00 && $1 != 00 ) || ( $2 == 04 )) print $3,$5}'; done < /tmp/subnet_list Anyone know if it is possible to assign $3 and $5 to separate variables within the {} brackets? Thanks... (14 Replies)
Discussion started by: CiCa
14 Replies

3. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

5. Shell Programming and Scripting

Problem using variable inside awk

HI, This is the code I am using: awk -v aaa="connect" 'BEGIN {IGNORECASE} /aaa/,/!/ {print NR}' bb This does not throw any error but it does not work. Pls help Thanks. (4 Replies)
Discussion started by: sudvishw
4 Replies

6. Shell Programming and Scripting

Using variable inside awk

Hi, Please help me how to use variables inside awk in code below: ll | awk -v "yr=`date '+%Y'`" -v "mnth=`date '+%m'`" -v Jan=1 -v Feb=2 -v Mar=3 -v Apr=4 -v May=5 -v Jun=6 -v Jul=7 -v Aug=8 ' !/^d/ { if(NR>1) {printf "%-29s\t\t%s\t%5s\t\t%s %s,", $9,$1,$5,$`$6`,$7} }' Thanks. (10 Replies)
Discussion started by: manubatham20
10 Replies

7. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

8. Shell Programming and Scripting

Using variable inside awk

I am trying to print the lines with pattern and my pattern is set to a variable express awk '/$express/{where=NR;print}' test2.log I am not getting any data even though i have the data with the pattern. Can seomeone correct me with the awk command above? (20 Replies)
Discussion started by: rdhanek
20 Replies

9. Shell Programming and Scripting

getting variable inside awk

Hi All, I have awk script for replacing the nth ocurance of a string in an xml file... My code is like this FILETYPE=xml TAGNAME=type OCCURANCE=$1 TAGVALUE=valueur echo OCCURANCE:$OCCURANCE echo TAGNAME:$TAGNAME echo TAGVALUE:$TAGVALUE awk -v n=$OCCURANCE -v... (1 Reply)
Discussion started by: subin_bala
1 Replies

10. Shell Programming and Scripting

variable inside awk '{print $c}'

i'm trying to do this (in bash darwin); echo "give me some words: " read a c=2 # this is get by other ways echo $a | awk '{print $c}' # i want to print the column given # by de $c variable if there is someone understand what i pretend... (3 Replies)
Discussion started by: Tártaro
3 Replies
Login or Register to Ask a Question