if condition doesn't work


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if condition doesn't work
# 1  
Old 06-12-2009
if condition doesn't work

i want to get the value for column 4rth when i =4. please guide what i am doing wrong. thanks

var=`cat file.csv`
for i in $var; do {
if [ "$i" -eq 4 ]; then
var4=$var4+$i

fi
echo $i
}
done
I am geting this error message "0403-009 The specified number is not valid for this command."
# 2  
Old 06-12-2009
Not clear what you are trying to do. Please post some sample input from file.csv and the matching sample output from the script.
# 3  
Old 06-12-2009
here is csv file
xerox @DDCM READY 2
xerox xerox READY 3
i need to get column 1,3,4 values. but column 4 is differnt so need to get sum.
i would like to get value of column 4 from first line and then sum up with second line (2+3=5)
# 4  
Old 06-12-2009
Hammer & Screwdriver Is this what you are trying to do?

Code:
> cat file23
xerox @DDCM READY 2 
xerox xerox READY 3 

> awk '{sum=sum+$4} END {print sum}' file23
5

# 5  
Old 06-12-2009
One way would be to use the awk command.
Code:
var=`awk -F" " 'BEGIN{total=0;}{total+=$4;}END{print total}' testfile`

Then
Code:
echo $var
5

# 6  
Old 06-12-2009
Quote:
Originally Posted by BubbaJoe
One way would be to use the awk command.
Code:
var=`awk -F" " 'BEGIN{total=0;}{total+=$4;}END{print total}' testfile`

Then
Code:
echo $var
5

You don't need to initialize 'total'. All variable are strings by default and initialized to an empty string (""). Once you start performing arithmetic, an empty string is casted to 0.
# 7  
Old 06-15-2009
Thanks guys both reply works fine.once again thanks a lot for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Timeout doesn't work, please help me

#!/bin/sh trap "cleanup" TERM timeout=5 mainpid=$$ cleanup() { echo "at $i interupt" kill -9 0 } watchdog() { sleep $1 } (watchdog $timeout && kill -TERM $mainpid) & run_test() (10 Replies)
Discussion started by: yanglei_fage
10 Replies

2. Shell Programming and Scripting

-ne 0 doesn't work -le does

Hi, I am using korn shell. until ] do echo "\$# = " $# echo "$1" shift done To the above script, I passed 2 parameters and the program control doesn't enter inside "until" loop. If I change it to until ] then it does work. Why numeric comparison is not working with -ne and works... (3 Replies)
Discussion started by: ab_2010
3 Replies

3. UNIX for Dummies Questions & Answers

Why doesn't this work?

find . -name "05_scripts" -type d -exec mv -f {}/'*.aep\ Logs' {}/.LogFiles \; Returns this failure: mv: rename ./019_0120_WS_WH_gate_insideTEST/05_scripts/*.aep\ Logs to ./019_0120_WS_WH_gate_insideTEST/05_scripts/.LogFiles/*.aep\ Logs: No such file or directory I don't know why it's trying... (4 Replies)
Discussion started by: scribling
4 Replies

4. Shell Programming and Scripting

My if statement doesn't work why?

I have the following and for some reason I can't have two options together. I mean if I choose -u and -p it won't work... why? #!/bin/bash resetTime=1 mytotalTime=0 totalHour=0 totalMin=0 averagemem=0 finalaverage=0 times=0 function usage() { cat << EOF USAGE: $0 file EOF } (10 Replies)
Discussion started by: bashily
10 Replies

5. Shell Programming and Scripting

echo doesn't work right

Hi,when I run my first shell script,I got something that doesn't work right. I wrote this code in the script. echo -e "Hello,World\a\n"But the screen print like this: -e Hello,World The "-e" wasn't supposed to be printed out. Can anyone help me out?:wall: Many thanks!:) (25 Replies)
Discussion started by: Demon
25 Replies

6. Shell Programming and Scripting

What to do when mtime doesn't work?

I have a folder that I need to search for new files and copy on the latest. I've been using "-mtime -1" in my command line but it doesn't seem to work. I've been meaning to fine a different script because files are dropped into the folder all day long and because of the -mtime, I've only be... (19 Replies)
Discussion started by: bbbngowc
19 Replies

7. Shell Programming and Scripting

Awk: Can anyone tell me why this doesn't work?

If there exists a field in stdin, print it, otherwise, print hello..... These print nothing: cat /dev/null | awk '{if ( length > 0 ) print $1; else print "hello"}' cat /dev/null | awk '{if ( $1 ) print $1; else print "hello"}'But the scripts work if I run them directly in a terminal: ... (8 Replies)
Discussion started by: ksheller
8 Replies

8. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

9. Shell Programming and Scripting

sed doesn't work

Hello I' m confused a bit. I want to replace string "&amp" with "&" using this command. sed 's/&amp/&/g' and it doesn't work. Nothing happens. On the other side this works: sed 's/&amp/@/g' or sed 's/&amp/^/g' !!! Can somebody help please? Thanks (3 Replies)
Discussion started by: billy5
3 Replies

10. Shell Programming and Scripting

Why doesn't this work?

cat .servers | while read LINE; do ssh jason@$LINE $1 done exit 1 ./command.ksh "ls -l ~jason" Why does this ONLY iterate on the first server in the list? It's not doing the command on all the servers in the list, what am I missing? Thanks! JP (2 Replies)
Discussion started by: jpeery
2 Replies
Login or Register to Ask a Question