awk script problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script problem
# 1  
Old 10-28-2010
awk script problem

Hi,
How to check status of the files in the awk script
i am having two awk script in one bash script.Like
-BELOW is the exact structure of the script
script name: new.sh
Code:
{
awk -v v1=`.....`
{
if (){
  exit -1;
    }
  }
}
}'$file name1
 
{
awk -v  v2=`.....`
{
   if (){ 
       exit -1;
        } 
    }
}'$file name2
 
}

--> Now in the above script i need use exit -1; for checkin the status
--> What happans while testing 2nd awk gives the final status always.
-->For example new.sh script use to check error records , if first awk means v1 has error status should be -1
if 2nd awk v2 has error then status should -1
--> If v1 has error and v2 doesnt have the status is 0 that means its not taking exit -1;
--> if v2 has error and v1 doesnt have then status is -1
--> I want to check the if any of the awk check error then status should be give as -1; if no error then status hould be 0

Thanks & Regards,
Ganesh

Moderator's Comments:
Mod Comment Use code tags, please...

Last edited by Scott; 10-28-2010 at 10:22 AM..
# 2  
Old 10-28-2010
You would need to check the exit status code $? in the script after each awk program has run. It only has a range of 0 to 255, so -1 isn't a valid value.
# 3  
Old 10-28-2010
Do you need to execute the 2nd awk if the 1st fails?

if not use exit 1 for errors, and try:
Code:
 awk -v v1='....' { ... exit 1 ...}' $file_name1 && awk -v v2='....' { ... exit 1 ...}' $file_name2

if you still want to execute 2nd awk if the 1st fails:
Code:
awk -v v1='...' $file_name
exit_code1=$?
awk -v v2='...' $file_name2
let exit_code=$exit_code+$?

exit_code will be 1 if A or B fails and 2 if both fail (adjust logic as you need).

Last edited by Chubler_XL; 10-28-2010 at 11:35 PM.. Reason: Typos
# 4  
Old 10-29-2010
Sorry its wont work,noeed something else

Hi, As you said its ok ,but this is only one script . i am using two awk commands in one bash shell script. i am running script like
./script name.sh param1 param2

Here i am executing single script.
Now in this awk will work individually , after 1st awk over 2 nd will start to execute. Now i just wanted to know can i check status usinf if () condition.

like in 1st awk if error persent it will print error msg in 2nd awk also,

Now if 1st awk has error and 2nd awk dont then it should return staus as fail thant means it if i use echo $? on unix box it should give some value if no error present then after using echo $? it returns 0.


could you please give some solution for this.
or Shall put whole script for you referance.
Please reply,,

Thanks,
Ganesh
# 5  
Old 10-29-2010
Try...
Code:
awk ' ... ' #--awk program # 1
if [ $? -ne 0 ]
then
    echo "awk # 1 not okay"
else
    echo "awk # 1 okay"
    awk ' ... ' #--awk program # 2
    if [ $? -ne 0 ]
    then
        echo "awk # 2 not okay"
    else
        echo "awk # 2 okay"
    fi
fi

# 6  
Old 10-29-2010
Ok for the following script need status

Hi,
I dont need message to be print over there, need to exit the script if the file has error. i am putting my script below.
-> if i execute the script if the file has error it will go to if(n!=v1) condition now i need whenever it will go to if condtion after process all it should exit.
-> for checking status i'll use echo $? if error in file then it will return some value if not then returns 0


my script
Code:
{
nawk -v v1=`grep "$1_$2_CASHFLOW_FLD_COUNT" $propfile | cut -d"=" -f2` -F\" '{
      for(i=1;i<=NF;i+=2){
      n+=gsub("~","",$i)
  }
  if(n!=v1){
    print "Error in CASHFLOW_HANDOFF at line number " NR ", present  tilde count= " n
    print "Total Number Of Fields   = "v1+1
    print "Actual Tilde count= " v1
    print"cashflow_status=Fail"
}
   n=0
   
   }'  $va
}

--> i need to use exit -1; in the script to abord the script .if i use exit -1 in if (n!=v1) it will work for one record only if in file more than 2 rows having error then it wont work. Could you plz reply on that.
Thanks,
Ganesh.

Last edited by Franklin52; 10-29-2010 at 09:00 AM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script problem

Hello guys i have following problem. I'm trying to copy content of one file and paste this content in all .txt files in directory, but at line 15. My script copy the content at first line, not 15. I'm confused how to do this. Thank you in advance for your help! This is my script: ARGS=2 ... (9 Replies)
Discussion started by: r00ty
9 Replies

2. Shell Programming and Scripting

Awk Script Problem

Can someone please explain to me what is wrong with this awk script? echo 74 85 | awk '{ if ( $1 > $2 ) PRESULTS = ( $1 - $2 ); print $0,"=>","P"PRESULTS ; else if ( $1 > $2 ) NRESULTS = ( $2 - $1... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. Shell Programming and Scripting

problem with awk script

Hi, I have two files Hi, I have two files file1 :> Code: val="10" port="localhost:8080"httpadd="http:\\192.168.0.239" file2 :> Code: val=${val} val="pdssx" port=${port}port="1324"httpadd=${httpadd}httpadd="raamraav"fileloc=${fileloc} file3(or file2) should have following... (1 Reply)
Discussion started by: nitin.pathak
1 Replies

4. Shell Programming and Scripting

Awk script Problem

Hi , I am having two files FILE1 and FILE2 as shown below I need to search each and every element of Coulumn1 in the FILE1 in FILE2 and Globally replace with the Corresponding element of the Column2 in the FILE2 , For example and1 which is the first element of COl 1 of the FILE1 should be... (4 Replies)
Discussion started by: jaita
4 Replies

5. Shell Programming and Scripting

Problem with awk script

Hi, I have one csv file with 3 fileds like tmp1.csv 2079|2010Aug|cardilogy 2349|2010Aug|numerology 2213|2010Aug|immunlogy another csv file with code for those specialities spec.csv cardiology|CRD numerology|NMY immunology|IMY i want to replace the contents of file 1 with codes... (2 Replies)
Discussion started by: Man83Nagesh
2 Replies

6. Shell Programming and Scripting

Problem with an awk Script

hello, first, yes i searched the forum , google and read many tutorials but still have a problem with my script. I have great Problems, because i haven't worked with regular expressions before and never had anything to do with shellscripts. i am a complete Newby in this sort of theme. I have... (8 Replies)
Discussion started by: Crashvogel
8 Replies

7. Shell Programming and Scripting

awk script problem

Hi All, I have the following input data: That I'd like to look like this ($2 is the column I'd like it to appear in) where the entries are grouped by date: The code I have at present is: awk 'BEGIN {} { dt = $1 if (dt == dt_prev) { pp = $3 ... (7 Replies)
Discussion started by: pondlife
7 Replies

8. Shell Programming and Scripting

Problem with a AWK Script

Hi I am having some contents in my file like this file1 ########################## pin (PIN1) { direction : input ; capacitance : 121 ; max_transition : 231 ; } pin (PIN2) { direction : input ; capacitance : 124 ; max_transition : 421 ;... (8 Replies)
Discussion started by: kshitij
8 Replies

9. Shell Programming and Scripting

Problem with awk script

Hi Can anyone help me in this Problem File1 ######################### HOLI 123 AND ONE TWO THREE AMITABH SAMSUNG POLI AND TWO SENSE CRYING WING PPIN TBFLAG I B AND OROLE TB_HOT=" DCT" TB_CAT=" CAT" TC_NOT=" AND" +PIN TB=" HOT" TB_GATE=" KOT" TB_LATE=" MAT" TC=LOT MAT DAT SAT... (5 Replies)
Discussion started by: kshitij
5 Replies

10. Shell Programming and Scripting

awk script Problem

I wrote a awk but doesnt work as expected. The Input File attached input file My awk Script /^.......*EXEC CICS /,/END-EXEC/ { if ( $0 ~ / LINK / ) { tsflag=1 } if ( $0 ~ /EXEC CICS/ && tsflag == 1 ) ... (6 Replies)
Discussion started by: pbsrinivas
6 Replies
Login or Register to Ask a Question