Modifying awk code to be inside condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Modifying awk code to be inside condition
# 1  
Old 11-10-2011
Modifying awk code to be inside condition

I have the following awk script and I want to change it to be inside a condition for the file extension.

Code:
################################################################################

#  abs: Returns the absolute value of a number

function abs(val) {

return val > 0 ? val  \
               : -val

}

################################################################################

BEGIN {

  ARGV[ARGC++] = ARGV[ARGC-1]                                  # Read file twice

}

# --Read file: First time round-------------------------------------------------

FNR == NR {
  />/ && idx[FNR] = ++i
  $2 || val[i] = $1                                 # Set source location ($2=0)
  ++j                                                   # Store last line number
  next
}

# --Read file: Second time round------------------------------------------------

FNR in idx { v = val[idx[FNR]] }                           # Get source location

{ srdist = abs($1 - v) }                       # Set source to receiver distance

/^[0-9]+/ && srdist > dsrmx {                                      # Print error
  print "  FNR="FNR", dist("v","$1")="srdist
}

################################################################################

Thus I have rewritten the code as

Code:
################################################################################

#  abs: Calculates the absolute value of val

function abs(val) {

  return val > 0 ? val  \
                 : -val

}

################################################################################

# Change record Separator and Field Separator.

BEGIN {

  split(ARGV[1], a, ".")
  fext = a[2]

  if (fext == "xt") {

    ARGV[ARGC++] = ARGV[ARGC-1]                                # Read file twice

  }

}

#-------------------------------------------------------------------------------

fext == "xt" {

  # Read file first time

  if (FNR == NR) {
      if ($0 ~ />/) { idx[FNR] = ++i }
      $2 || val[i] = $1                             # Set source location ($2=0)
      ++j                                               # Store last line number
      next
  }

  # Read file second time

  if (FNR in idx) { v = val[idx[FNR]] }                    # Get source location

  { srdist = abs($1 - v) }                     # Set source to receiver distance

  if ($0 ~ /^[0-9]+/ && srdist > dsrmx ) {
      print "  FNR="FNR", dist("v","$1")="srdist
  }

}

################################################################################

I just have problem with the line

Code:
  { srdist = abs($1 - v) }                     # Set source to receiver distance

How can I include it in the code?

I am thinking of just removing the {}, thus having

Code:
################################################################################

#  abs: Calculates the absolute value of val

function abs(val) {

  return val > 0 ? val  \
                 : -val

}

################################################################################

# Change record Separator and Field Separator.

BEGIN {

  split(ARGV[1], a, ".")
  fext = a[2]

  if (fext == "xt") {

    ARGV[ARGC++] = ARGV[ARGC-1]                                # Read file twice

  }

}

#-------------------------------------------------------------------------------

fext == "xt" {

  # Read file first time

  if (FNR == NR) {
      if ($0 ~ />/) { idx[FNR] = ++i }
      $2 || val[i] = $1                             # Set source location ($2=0)
      ++j                                               # Store last line number
      next
  }

  # Read file second time

  if (FNR in idx) { v = val[idx[FNR]] }                    # Get source location

  srdist = abs($1 - v)                         # Set source to receiver distance

  if ($0 ~ /^[0-9]+/ && srdist > dsrmx ) {
      print "  FNR="FNR", dist("v","$1")="srdist
  }

}

################################################################################


Last edited by kristinu; 11-10-2011 at 12:48 AM..
# 2  
Old 11-12-2011
Your approach isn't working because fext is being set before any input lines are seen. Thus, fext is true if and only if the first argument matches a file with the extension "st". It wont matter what the rest of the arguments are. Further, the FNR==NR mechanism works only to distinguish the very first file from the rest. If you add files to the argument list, they will all be processed as if they were the first file read the second time.
# 3  
Old 11-14-2011
My plan is that I pass only one file. but I need to check that the extension is .xt
# 4  
Old 11-14-2011
The smarter thing is to wrap this in a shell script:

Code:
infile=$1
extension=${infile##*.}

if [ $# != 1 -o ."$extension" != ".xt" ]; then
   echo "Usage: $0 input.xt"
   exit 1
fi
exec awk -f yourawkscript "$infile"

If you do not have access to bash or ksh for some reason, then you can use this line to get the extension:
Code:
extension="`echo $infile| sed 's/.*\.//'`"

But if you need awk to do it then in your awk script, modify the BEGIN { ... } block to this:
Code:
BEGIN {

  if ( !match(ARGV[ARGC],/\.xt$/) ) { 
           print "Usage: awk -f thisfile input.xt"; exit(1); 
  }

  ARGV[ARGC+1] = ARGV[ARGC]                                  # Read file twice
  ++ARGC
}

# 5  
Old 11-14-2011
Thanks a lot Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If else condition inside for loop of awk command in UNIX shell scripting

Hi , Please excuse me for opening a new thread i am unable to find out the syntax error in my if else condition inside for loop in awk command , my actual aim is to print formatted html td tag when if condition (True) having string as "failed", could anyone please advise what is the right... (2 Replies)
Discussion started by: karthikram
2 Replies

2. 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

3. Shell Programming and Scripting

matching patterns inside a condition in awk

I have the following in an awk script. I want to do them on condition that: fext == "xt" FNR == NR { />/ && idx = ++i $2 || val = $1 next } FNR in idx { v = val] } { !/>/ && srdist = abs($1 - v) } />/ || NF == 2 && srdist < dsrmx {... (1 Reply)
Discussion started by: kristinu
1 Replies

4. Shell Programming and Scripting

Check condition inside the loop

Hi, I am in trouble. I can get inside my condition test inside a loop : I am in ksh (solaris) while read file do <commande to retrieve file> >> ${LOG_RETRIEVE_FILE.log} msg_err=$(cat ${LOG_RETRIEVE_FILE.log} | grep "error retrieve") if ; then <sendmail> exit 1 fi done I tried... (6 Replies)
Discussion started by: Aswex
6 Replies

5. Shell Programming and Scripting

grep inside if condition - need help

hi i need help with below code. if ] then log "Exiting the script as ID= NULL" log "Please run script first." fi i am calling grep inside this but its not running any ideas why ?? input file is like this -- Msg 102, Level 20, State 1: Server... (4 Replies)
Discussion started by: dazdseg
4 Replies

6. Shell Programming and Scripting

FTP Command inside If condition

Hi All, I am facing a alien problem in unix shell script. i have a requirement where i have to check first whether a particular file has been created or not if that file has been created then that file needs to be ftped to a particular dir. My Code is like that Success='code for file... (3 Replies)
Discussion started by: aryan_styles
3 Replies

7. Shell Programming and Scripting

If condition inside while???

Hi source is text file named alpha.text containing entries ABC DEF XYZ YYY BBB I need a small shell script which should do read the entries in alpha.text one by one and check it with the current date. If the date is Mon (monday) and entry read from the file is 'DEF' then this should... (4 Replies)
Discussion started by: PrasannaKS
4 Replies

8. Shell Programming and Scripting

using flag inside a for loop to check condition

I have a logic like this It initializes the flag variable as "T" at the beginning of the loop everytime Inside each loop it checks for two conditions and updates the flag variable as "A" or "B" In the end of the loop it checks for the value of the variable flag for "A" or "B" and execute... (4 Replies)
Discussion started by: codeman007
4 Replies

9. Shell Programming and Scripting

condition inside a for loop

I have a for loop in my script as shown below. for file_path in $file_list ; do ........my code .......... ...... done Can i restrict the number of files parsing to the variable file_path as 50? That is, even if I have some 100 files in file_list, I need to take only 50 files for... (7 Replies)
Discussion started by: Vijay06
7 Replies

10. Shell Programming and Scripting

Checking condition inside the loop

Hi all, I have one clarification i am using the loop which will process for each record .suppose there is f ailure in the first record it need to send mail and process the next .my code: defcount=`cat <filename>|wc -l` while ] do if <some condiotion> then echo "mail" fi done so... (1 Reply)
Discussion started by: ithirak17
1 Replies
Login or Register to Ask a Question