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?
# 1  
Old 10-03-2010
How to use same variable value inside as well as outside of the awk command?

Hi Jim,

The following script is in working state. But i m having one more problem with awk cmd. Could you tell me how to use any variable inside awk or how to take any variable value outside awk.
My problem is i want to maintain one property file in which i am declaring variable value into that property file and want to use that variable into awk script.
example.
file name:validate.property
INT_EUR=14
INT_TOK=54

now these two variable i want to use it into my shell script. new.sh which i wrote below.
Also one more thing .How to find scane arount 50000 records with minimum time. Actually the following script scan 42000 records in 6 min, i need to scan in 1 min is there any method in shell script.
i Hope u understand my problem. Could you please help me out.

Waiting for you reply.

Code:
#!/bin/bash
DATA_DIR=/export/opt/rtrupld/autosys/scripts_old
data=`ls $DATA_DIR/count.txt`


  awk ' END {print NR}' $data
  awk ' {          sum=0;

       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


Last edited by Franklin52; 10-03-2010 at 09:35 AM.. Reason: Please use code tags
# 2  
Old 10-03-2010
Please provide an example of the data that is in the $DATA_DIR/count.txt data file.

Why the need to invoke awk twice? Print the number of records at the top of the output?

Why do you need so many print statements? Why not just output the lines which are regarded as "errors"? Each print statement adds to the execution time.
# 3  
Old 10-05-2010
Data in count.txt file

Hi fpmurphy ,
Thanks for your response,

count.txt file contains following data,

Code:
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~~~~~~~~~~~~~~~

Above record each line contains 21 ~ chars in line number 4th number o ~'s are 22 so my script should give line number 4 has an 22 ~'s also my script should take a static value from property file like validate.property
contains records like
Code:
INT_EUR=21
INT_TOK=21
INT_CA=54

so on
so that INT_EUR has value =21 so script will check is row 1 have ~=21 or not and so on every line should get scanned and does not match gives n error .
Could you please solve this problem.

Now your questions regarding why multiple awk commands. i am not be able to take any variable inside the awk command.Please guide me what to do.

Thanks & Regards,
Ganesh


---------- Post updated at 06:28 PM ---------- Previous update was at 12:11 AM ----------

Thanks,
And sorry for that. but i was placed queary again in details as someon asked me regading the problem in details.
Thanks for giving me this worning.

---------- Post updated 10-05-10 at 10:42 AM ---------- Previous update was 10-04-10 at 06:28 PM ----------

Hi ,
Could you please reply me on my thread.Its really helpfuul for me.

Thanks & Regards,
Ganesh


Quote:
Originally Posted by fpmurphy
Please provide an example of the data that is in the $DATA_DIR/count.txt data file.

Why the need to invoke awk twice? Print the number of records at the top of the output?

Why do you need so many print statements? Why not just output the lines which are regarded as "errors"? Each print statement adds to the execution time.

Last edited by Scott; 10-04-2010 at 11:19 AM.. Reason: Please use code tags. Removed what looked like a telephone number.
# 4  
Old 10-05-2010
Something like this,

Code:
awk -v v1=`grep "INT_EUR" validate.property | cut -d"=" -f2` -F'~' '{if (NF-1 != v1) {print "line number "NR " has " NF-1 " ~"}}' count.txt

# 5  
Old 10-05-2010
Thanks but required more specific script which can be use in my awk script.

Hi Pravin,
Could you check my script i put it into forum. according to your solution i used above the my awk command. but it wont work. If you are online could you please send me that hardcodede values how to use inside in my awk command.

Thank & Regards
Ganesh

# 6  
Old 10-05-2010
If you run the below command from your command prompt,

Code:
awk -v v1=`grep "INT_EUR" validate.property | cut -d"=" -f2` -F'~' '{if (NF-1 != v1) {print "Error in " $0 " at line number "NR " tilde count= " NF-1 }}' count.txt

You will get the o/p as below.
o/p
Code:
Error in INTELLECT~JP~111~0~A~2jp10212991~RR~~~~~~~~~~~~~~~~ at line number 4 tilde count= 22

Is it your expected o/p, If no then post your expected o/p.
# 7  
Old 10-05-2010
Assuming the value of the first line of your property file is determinative:
Code:
awk -F~ '
BEGIN{getline p < "property_file"; split(p,a,"=")} 
NF-1 != a[2] {print "Line " NR " has " NF " fields."}' file

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