2

This might be a stupid question, but i have never had to anything like this in scripting. So any help would be appreciated.

I am dealing with data from a census and have to generate reports. Each line in the file has a last name, frequency, etc etc. I have to grab the frequency. But if i grab some ones last name who has a frequency of "0.000" then i have to change it to "0.00025". I attempted to do an if statement but am not sure how to do it.

#!/bin/bash
all=/homes/ddailey/public_html/data/dist_all_last

count=`grep -w $1 $all | awk '{ print $2 }'`

if [ $count == "0.000" ]; then
   echo .00025
else
   echo $count
fi
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Sampson
  • 29
  • 6
  • This isn't perfect, but it looks like it should do what you describe. What's the problem with it? – kojiro Oct 14 '13 at 02:41
  • Yeah i know its not perfect, just put it together in a few minutes. The problem is that if $count greps a string that is 0.000 it doesnt change it to .00025. It just keeps it at 0.000 – Sampson Oct 14 '13 at 02:43
  • As written it shouldn't change `count`'s value, but it should *echo* .00025. Does it do that? – kojiro Oct 14 '13 at 02:45
  • How many lines are in the file? If there's more than one, that's the trouble. See also [How to debug a shell script](http://stackoverflow.com/questions/951336/). – Jonathan Leffler Oct 14 '13 at 02:46
  • it doesnt echo .00025, it echo's .000... but their is more than one line in the file – Sampson Oct 14 '13 at 02:49

2 Answers2

1

The first statement

count=`grep -w $1 $all | awk '{ print $2 }'`

is returning a list of values separated by newlines. A simple echo won't show the newlines but this will:

echo "$count"

The if statement, however, expects a single value, so if you have two or more results in $count it will not match the single test value. What you need to do is loop through the values in $count. Try the following:

counts=`grep -w $1 $all | awk '{ print $2 }'`
for count in $counts
do
    if [ $count == "0.000" ]; then
       echo .00025
    else
       echo $count 
    fi
done

I prefer the following design which puts all your logic into the awk command. That keeps all your processing logic in one script, not split across awk and bash.

grep -w $1 $all |
awk '{ if ($2 == "0.000") $2 = "0.00025"; print $2 }'
Peter Raynham
  • 647
  • 3
  • 6
0
  1. Pick a shell. The Bourne Again Shell (bash) and the basic /bin/sh are both popular. There are a number of other options which you should not use.

  2. Specific advice. Google. Really. See Compare a string in Unix for your answer, or How do I compare strings in Bourne Shell? if using bash.

  3. Watch spaces. Shell programming is sensitive to white space, so having or forgetting a space breaks scripts. For example, make sure you remember the space before and after '[' or ']'. Getting it to run often involves adding and removing spaces.

  4. Watch for empty replacements. if [ $nosuchvar = 3 ] makes an error but if [ "$nosuchvar" = 3 ] does not. Also, glob replacements, so if [[ $foo == "a*" ]] is different than if [ $foo == "a*" ]

  5. Dump the shell. Running Python or another 'real' language is usually far cleaner and more maintainable than any shell script over five lines.

Community
  • 1
  • 1
Charles Merriam
  • 19,908
  • 6
  • 73
  • 83