What is wrong with this awk statement?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting What is wrong with this awk statement?
# 1  
Old 03-20-2013
What is wrong with this awk statement?

Code:
echo $line | awk -F, "BEGIN {if(NF==10) && /'"${MONTH}"' '"${DAY}"' '"${TIMEH}:"'/,0 {print $10"-"$4} else {print $13"-"$4}}"

if the fields in the line is equal to 10, print the values of this specified fields "$10 and $4".

if the fields in the line is anything but 10, print the values of field "$13 and $4".
# 2  
Old 03-20-2013
Why is it inside BEGIN block?

A BEGIN rule is executed once only, before the first input record is read.
# 3  
Old 03-20-2013
Quote:
Originally Posted by Yoda
Why is it inside BEGIN block?

A BEGIN rule is executed once only, before the first input record is read.

initially, this is the command i was using:

Code:
echo $line | awk -F, '(NF==10) && /'"${MONTH}"' '"${DAY}"' '"${TIMEH}:"'/,0 {print $10"-"$4}'

but i needed to include the "else" condition in there.
# 4  
Old 03-20-2013
So, why is it inside the BEGIN block? Smilie

You could have something like:
Code:
awk -F, '/'"${MONTH}"' '"${DAY}"' '"${TIMEH}:"'/,0 {if(NF==10) {print $10"-"$4} else {print $13"-"$4}}'

This User Gave Thanks to CarloM For This Post:
# 5  
Old 03-20-2013
In the BEGIN section, no file is read yet, so NF is not defined. Try
Code:
echo $line | awk -F -v DateTime="${MONTH} ${DAY} ${TIMEH}" \
    'match ($0, DateTime) {InRange=1}
     InRange && NF==10    {print $10"-"$4; next}
     InRange              {print $13"-"$4}
    '

This User Gave Thanks to RudiC For This Post:
# 6  
Old 03-20-2013
Quote:
Originally Posted by CarloM
So, why is it inside the BEGIN block? Smilie

You could have something like:
Code:
awk -F, '/'"${MONTH}"' '"${DAY}"' '"${TIMEH}:"'/,0 {if(NF==10) {print $10"-"$4} else {print $13"-"$4}}'

this did the trick. Thank you!!!!!!!!

---------- Post updated at 12:28 PM ---------- Previous update was at 12:24 PM ----------

Quote:
Originally Posted by RudiC
In the BEGIN section, no file is read yet, so NF is not defined. Try
Code:
echo $line | awk -F -v DateTime="${MONTH} ${DAY} ${TIMEH}" \
    'match ($0, DateTime) {InRange=1}
     InRange && NF==10    {print $10"-"$4; next}
     InRange              {print $13"-"$4}
    '

this too, thanks so much!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Whats wrong with this If statement?

Hi I am pretty new to bash scripting.I am trying to write the if statement in bash and it give me error. Can you please help me what I am doing wrong in If statement? if && && then fector=$kk; divide=$DB_SIZE/$kk; echo "factor value:$fector" echo"divide value:$divide"... (4 Replies)
Discussion started by: Gevni
4 Replies

2. UNIX for Beginners Questions & Answers

Whats wrong with this If statement?

Hi I am pretty new to bash scripting.I am trying to write the if statement in bash and it give me error. Can you please help me what I am doing wrong in If statement? if && && then fector=$kk; divide=$DB_SIZE/$kk; echo "factor value:$fector" echo"divide value:$divide"... (1 Reply)
Discussion started by: Gevni
1 Replies

3. Programming

Whats wrong with this If statement?

Hi I am pretty new to bash scripting.I am trying to write the if statement in bash and it give me error. Can you please help me what I am doing wrong in If statement? Code: if && && then fector=$kk; divide=$DB_SIZE/$kk; echo "factor value:$fector" echo"divide... (1 Reply)
Discussion started by: Gevni
1 Replies

4. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

5. Shell Programming and Scripting

What's wrong with my awk

file1: maximum_delay time: 102.794 ms maximum_delay time: 92.977 ms maximum_delay time: 98.895 ms maximum_delay time: 96.891 ms maximum_delay time: 86.966 ms maximum_delay time: 95.91 ms maximum_delay time: 98.921 ms maximum_delay time: 89.881 ms maximum_delay time: 92.931 ms... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

6. Shell Programming and Scripting

Why result is wrong here ? whether break statement is wrong ?

Hi ! all I am just trying to check range in my datafile pls tell me why its resulting wrong admin@IEEE:~/Desktop$ cat test.txt 0 28.4 5 28.4 10 28.4 15 28.5 20 28.5 25 28.6 30 28.6 35 28.7 40 28.7 45 28.7 50 28.8 55 28.8 60 28.8 65 28.1... (2 Replies)
Discussion started by: Akshay Hegde
2 Replies

7. Shell Programming and Scripting

What's wrong with the do statement?

hi guys, here's the code $ cat getsums.sh #!/usr/bin/sh FILENAME=$1 DELIMITER=$2 FIRST_COL=$3 SECOND_COL=$4 SALESDATE_COL=$5 STOREID=$6 UPC=$7 GTIN=$8 PROMOID=$9 echo "" echo ".:Summation Tool:." for FILE in ${FILENAME} do (7 Replies)
Discussion started by: ramneim
7 Replies

8. Shell Programming and Scripting

Something is wrong with this switch( case statement.

I started writing a script to save the files from a camera I got the other day, which mounts in /Volumes , and I got into it and started building this menu. The only problem is that the switch case is coming up as a syntax error at the parenthesis after a case. Here is the code: while : do ... (2 Replies)
Discussion started by: snakemasterAK
2 Replies

9. Shell Programming and Scripting

whats wrong with this find statement ?

cmd="find /a/technologies -name '*.jar' | grep \"Tuning/specificloader/lib\"" echo $cmd for index in `$cmd` do SL_JARS="${SL_JARS}:${index}" done gives error ==> find: paths must precede expression Usage: find but for index in... (2 Replies)
Discussion started by: crackthehit007
2 Replies

10. Shell Programming and Scripting

What's wrong with this simple statement?

I know that this is a ridiculously simple statement, but I am getting an error when I execute it, and I can't figure out what it is. Can anyone point me in the right direction? #!/bin/ksh integer dateMonth=0 integer intZero=0 if then dateMonth = 1 fi echo $dateMonth (7 Replies)
Discussion started by: mharley
7 Replies
Login or Register to Ask a Question