Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 185703
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 10, 20222022-06-10T04:52:42+00:00 2022-06-10T04:52:42+00:00

How to extract some values in a column and calculate them, then generate a new column in linux

  • 0

[ad_1]

You can do it easily in awk, but your numbers seem to be off by 0.02 (see below for reason). For instance, you can do:

awk '
  FNR==1 {                    # if first record
    printf "DP\tRatio\n"      # output heading
    next                      # skip to next record
  } 
  {
    nf = $NF                  # save copy of last field
    gsub (/[,:]/, " ", nf)    # replace "," and ":" with " "
    split (nf, arr, " ")      # split into arr on space
    printf "%s\t%.2f\n", arr[4], arr[3]/(arr[2]+arr[3])  # output result
  }
' file

Where for example with record 2, arr[3] == 1125, and arr[2] == 2029. The result of the computation is 0.36 not 0.34? (you used 2092 in your calculation instead of the actual 2029 and 553 instead of the actual 533 — mystery solved)

Example Use/Output

You can just paste the script into an xterm with your data file (named file or whatever you change the filename to) as:

$ awk '
>   FNR==1 {                    # if first record
>     printf "DP\tRatio\n"      # output heading
>     next                      # skip to next record
>   }
>   {
>     nf = $NF                  # save copy of last field
>     gsub (/[,:]/, " ", nf)    # replace "," and ":" with " "
>     split (nf, arr, " ")      # split into arr on space
>     printf "%s\t%.2f\n", arr[4], arr[3]/(arr[2]+arr[3])  # output result
>   }
> ' file
DP      Ratio
3154    0.36
883     0.40

So the output based on the file with the fields split as shown above is:

DP      Ratio
3154    0.36
883     0.40

Creating an Awk Script

Creating a script with awk to run from the command line is a convenient way to make the awk commands reusable. For example you can create a file say, splitrec.awk containing:

#!/bin/awk

FNR == 1 {                  # if first record
  printf "DP\tRatio\n"      # output heading
  next                      # skip to next record
}
{
  nf = $NF                  # save copy of last field
  gsub (/[,:]/, " ", nf)    # replace "," and ":" with " "
  split (nf, arr, " ")      # split into arr on space
  printf "%s\t%.2f\n", arr[4], arr[3]/(arr[2]+arr[3])  # output result
}

Then your use of the script becomes:

$  awk -f splitrec.awk file
DP      Ratio
3154    0.36
883     0.40

[ad_2]

  • 0 0 Answers
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 325 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 295 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 292 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.