Results 1 to 3 of 3

Thread: Math Calculations - Unix Shell

  1. #1
    Junior Member
    Join Date
    May 2008
    Answers
    1

    Math Calculations - Unix Shell

    Can someone tell me how to do the following inside a unix script?

    I have a file and can do a wc -l on it....say the count is 123 lines.

    #wcl /tmp/file
    >123

    I would then like to divide this number by 2 to get a decimal number.

    # wcl /tmp/file| `expr /tmp/file /2`

    Can't find the right expr combo.

    Any idea how to accomplish this?

    Thx.
    Kurt


  2. #2
    Junior Member
    Join Date
    Nov 2007
    Answers
    3

    Thumbs up Re: Math Calculations - Unix Shell

    Hi,

    you can first store the o/p of wc -l value in a variable.

    say a = wc -l /tmp/file.

    then b=`expr $a/2`

    Thanks.


  3. #3
    Junior Member
    Join Date
    Jul 2008
    Answers
    6

    Re: Math Calculations - Unix Shell

    ur approch is right but u need to change some thing
    as wc -l does not only returns file rows it also return file name so that name can not be divided
    so use cut to remove that file name
    also make sure in expr there should be space around arithmetic operator


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact