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 a 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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7163995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:59:44+00:00 2026-05-28T13:59:44+00:00

I have two command line with AWK which works perfectly: awk ‘NR >=2 {for

  • 0

I have two command line with AWK which works perfectly:

awk 'NR >=2 {for (i=7;i<=NF;i++) if ($i ~ /^-/) sum1 += $i; print $1,$2,$3,$4,$5,$6,sum1, ; sum1 = 0}' test.txt

awk 'NR >=2 {for (i=7;i<=NF;i++) if ($i ~! /^-/) sum += $i; print $1,$2,$3,$4,$5,$6,sum, sum/192 ; sum = 0}' test.txt

I want to combine these two commands to one command so I would be able to get sum and sum1 and I want to print them both! and if posible use an equation!

something like this:

awk 'NR >=2 {for (i=7;i<=NF;i++) if ($i ~! /^-/) sum += $i; {for (i=7;i<=NF;i++) if ($i ~ /^-/) sum1 += $i ; print $1,$2,$3,$4,$5,$6,sum,sum1, sum/(192 +(sum1*2)) ; sum = 0 ; sum1 = 0}' test.txt

or

awk 'NR >=2 {for (i=7;i<=NF;i++) if ($i ~! /^-/) sum += $i && {for (i=7;i<=NF;i++) if ($i ~ /^-/) sum1 += $i ; print $1,$2,$3,$4,$5,$6,sum,sum1, sum/(192 +(sum1*2)) ; sum = 0 ; sum1 = 0}' test.txt

but I get this error:

awk: cmd. line:1:
^ unexpected newline or end of string

in case it helps my file is something like this:

probeset_id submitted_id chr snp_pos alleleA alleleB 562_201 562_202 562_203 562_204 562_205 562_206 562_207 562_208 562_209 562_210 562_211 562_212 562_213 562_214 562_215 562_216 562_217 562_218 562_219 562_220 562_221 562_222 562_223 562_224 562_225 562_226 562_227 562_228 562_229 562_230 562_231 562_232 562_233 562_234 562_235 562_236 562_237 562_238 562_239 562_240 562_241 562_242 562_243 562_244 562_245 562_246 562_247 562_248 562_249 562_250 562_251 562_252 562_253 562_254 562_255 562_256 562_257 562_258 562_259 562_260 562_261 562_262 562_263 562_264 562_265 562_266 562_267 562_268 562_269 562_270 562_271 562_272 562_273 562_274 562_275 562_276 562_277 562_278 562_279 562_280 562_281 562_283 562_284 562_285 562_289 562_291 562_292 562_294 562_295 562_296 562_400 562_401 562_402 562_403 562_404 562_405 
AX-75448119 Chr1_41908741 1 41908741 T C 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 1 1 1 1 0 1 0 0 0 0 2 2 0 0 0 0 0 1 0 0 0 0 0 
AX-75448118 Chr1_41908545 1 41908545 T C 2 2 2 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 1 2 2 2 2 2 2 2 2 2 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 1 2 2 2 0 1 1 1 2 -1 1 2 0 0 2 1 1 0 1 0 1 2 1 0 0 1 2 2 1 2 2 0 1 2 2 2 2 2 2 0 1 0 0 0 1 2 2 2 2 0

and I want the result to be like this

AX-75448119 Chr1_41908741 1 41908741 T C 13 0 0.067
AX-75448118 Chr1_41908545 1 41908545 T C 98 -1 0.515
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:59:45+00:00Added an answer on May 28, 2026 at 1:59 pm

    Here is the nicely formatted version with explanation:

    awk '
    NR>1{
        #Initialize the variables to 0 for every iteration
    
        sum=0;
        sum1=0;
    
        #Loop from 7th column till the end
    
        for(i=7;i<=NF;i++) 
        {
    
        #Test if the value in that column is greater than zero
    
            if($i>0)   
            {    
    
        #If test returns true, add value to variable sum
    
                sum+=$i; 
            }
            else 
            {
    
        #If test returns false, add value to variable sum1
    
                sum1+=$i; 
            }
         }
    
        #Loop again through the column 1-6
    
            for(i=1;i<7;i++) 
    
        #Print the values of those columns
    
                printf("%s ",$i); 
    
        #Print variables and function
    
                printf("%d %d %f\n",sum,sum1,sum/(192 + (sum1*2))) 
    }' test.txt
    

    Test:

    [jaypal:~/Temp] awk '
    NR>1{
        sum=0;                                                
        sum1=0; 
        for(i=7;i<=NF;i++) 
        {          
            if($i>0) 
            {                                 
                sum+=$i; 
            }                  
            else 
            {
                sum1+=$i;                                         
            }
         }                 
            for(i=1;i<7;i++) 
                printf("%s ",$i);
                printf("%d %d %f\n",sum,sum1,sum/(192 + (sum1*2)))
    }' test.txt
    AX-75448119 Chr1_41908741 1 41908741 T C 13 0 0.067708
    AX-75448118 Chr1_41908545 1 41908545 T C 98 -1 0.515789
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a command-line program which takes input from stdin. What's the best way
I have a Perl script that requires two command line arguments that takes a
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
Summary I have written a process monitor command-line application that takes as parameters: The
I have an executable that depending on the command line switch supplied looks something
I have two console applications to which I would like to add GUIs. The
Ok so I have two classes in which they are not really associated with
I have IIS express loaded and running from a command line entry. I am
I have two applications: 1) A multithreaded cocoa gui app. 2) A unix command
I have two commands I execute frequently. Let's say the first is 'abcd' and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.