if statement is not giving correct op


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if statement is not giving correct op
# 1  
Old 03-07-2011
if statement is not giving correct op

Hi I am using a awk command but not getting required o/p.
input file
Code:
a.txt 
2 ak
3 cb
4 de
5 gh
6 ij

awk program
Code:
 
BEGIN { x=0 }
{if ($1>3) {x=x+1}{print $0} }
END   {  print "I found " x " line have value more than 3" }

output
Code:
2 ak
3 cb
4 de
5 gh
6 ij
I found 3 line have value more than 3

required o/p
Code:
4 de
5 gh
6 ij
I found 3 line have value more than 3

How can i use two operation in single if statement like here i want to print the line and also increment counter.
# 2  
Old 03-07-2011
Code:
{if ($1>3) {x=x+1;print $0} }

# 3  
Old 03-07-2011
Thanks for reply.
Currently I am writing a awk program and running that by using
Code:
 
nawk -F" " -f b.awk file

Can I give complete command in a single line
like
Code:
 
nawk -F" " BEGIN '{ x=0 } {if ($1>3) {x=x+1;print $0} }' END   '{  print "I found " x " line have value more than 3" }' file

but in this case it is giving error
# 4  
Old 03-07-2011
Quote:
Originally Posted by aaysa123
Can I give complete command in a single line
like
Code:
 
nawk -F" " BEGIN '{ x=0 } {if ($1>3) {x=x+1;print $0} }' END   '{  print "I found " x " line have value more than 3" }' file

but in this case it is giving error
Code:
nawk  '$1>3 {x=x+1;print $0}END{  print "I found " x " line have value more than 3" }' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

CUT command not giving correct result inside loop

Hi, i have a source file and have 3 columns and separated by "|" .i want to split this 3 columns in different variable.When i am executing this values indivisually giving correct result but when the same execute inside a for loop,it's giving issues. Src file(jjj.txt) -------... (8 Replies)
Discussion started by: raju2016
8 Replies

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

3. What is on Your Mind?

Happy Thanks Giving

Hello All, I am opening this thread to so that we can THANK ALL moderators/forum advisors/Active users in this forum who really make this place to learn/share/advice :b:. I would like to THANK some people by name too, a very very special THANKS to Neo, Don, Scrutnizer, Corona688, Bartus,... (1 Reply)
Discussion started by: RavinderSingh13
1 Replies

4. Shell Programming and Scripting

$0 not giving script name

Why does $0 return the word usage rather than the script name when used in a function? Baffeled on this one, any help appreciated. usage() { echo "$0 -cs <number of batches>\n" echo "$0 -c 4" echo "$0 -s 4" # echo "-c = Create" # echo "-s = Submit\n" exit 1 } $... (1 Reply)
Discussion started by: nhatch
1 Replies

5. Shell Programming and Scripting

Please help inserting the correct if-then-else statement

Hi All, I have this script below that checks rpm code version to Linux and Solaris servers. The script is run using arguement ($1) which is server_list.txt. It runs as: $ ./scriptname server_list.txt #!/bin/bash QUERY="/opt/rpm/bin/rpm -qa --queryformat '%{installtime}... (0 Replies)
Discussion started by: linuxgeek
0 Replies

6. Shell Programming and Scripting

AWK not giving me correct output.

i have a line like this in my script IP=`get_IP <hostname> | awk '{ print $1 }' echo $IP the problem is get_IP <hostname> returns data formated as follows: ip 1.1.1.1 name server_name the code above returns 1.1.1.1 server_name and i just need the 1.1.1.1 I have tried to add "|... (5 Replies)
Discussion started by: mcdef
5 Replies

7. Shell Programming and Scripting

::select statement return value with correct field size::

Hi Everyone, I am facing a problem regarding the select from sybase, the return with the incorrect size. For example, field is NAME(20). After i selected from sybase, the result is nicky. after i assign it to another declaration variable, it will be in actual name "nicky" , what i need... (10 Replies)
Discussion started by: ryanW
10 Replies

8. Shell Programming and Scripting

How is use sselect statement o/p in insert statement.

Hi All, I am using Unix ksh script. I need to insert values to a table using the o/p from a slelect statement. Can anybody Help! My script looks like tihs. ---`sqlplus -s username/password@SID << EOF set heading off set feedback off set pages 0 insert into ${TB_NAME}_D... (2 Replies)
Discussion started by: nkosaraju
2 Replies

9. Shell Programming and Scripting

How to get 07 while giving ((Preyy = yy - 01))

Hi, I need to get the previous year as 07 instead of just 7. I'm running the following for this.. yy=`date '+%y'` ((Preyy = yy - 01)) echo ${Preyy} Here, instead of 07 i'm getting 7 as result. Please Help. Thanks, RRVARMA (3 Replies)
Discussion started by: RRVARMA
3 Replies

10. Shell Programming and Scripting

If statement - How to write a null statement

In my ksh script, if the conditions of a if statement are true, then do nothing; otherwise, execute some commands. How do I write the "do nothing" statement in the following example? Example: if (( "$x"="1" && "$y"="a" && "$z"="happy" )) then do nothing else command command fi... (3 Replies)
Discussion started by: april
3 Replies
Login or Register to Ask a Question