bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: test fields in bash


From: Bob Proulx
Subject: Re: test fields in bash
Date: Mon, 26 Jan 2009 14:42:44 -0700
User-agent: Mutt/1.5.13 (2006-08-11)

BooBz wrote:
> I have a file :
> fghfgh 45 45 100
> hjuhju 90 45 90
> and i want to test the field to rewrite or not another field :
> if field2 = 45  and field3 = 45 then field4 = field4 - 10
> if field2 = 45  and field3 = 90 then field4 = field4 - 5
> if field2 = 90  and field3 = 45 then field4 = field4 - 5
> 
> I don't know if it's possible in bash ? Maybe some language are useful for
> the things i want to do ...

Some languages are better suited to this type of manipulation than
others.  But you are asking in bug-bash and so you must be expecting a
bash scripting answer.  :-)

Try something like this:

  while read field1 field2 field3 field4; do
    if [[ $field2 = 45 && $field3 = 45 ]]; then field4=$(($field4 - 10)); fi
    if [[ $field2 = 45 && $field3 = 90 ]]; then field4=$(($field4 - 5)); fi
    if [[ $field2 = 90 && $field3 = 45 ]]; then field4=$(($field4 - 5)); fi
    echo $field1 $field2 $field3 $field4
  done

There are endless variations but this matched your description fairly
closely.

Bob




reply via email to

[Prev in Thread] Current Thread [Next in Thread]