# How to calculate by taking first field

I am trying to do a calculation like this, I have a text file with numbers like

``````459
455
463
``````

And I must calculate percentage of those numbers relative to 600. I mean exactly

`(459/600)*100` that will be my percentage, `459` being obtained from the text file.

Could somebody help me please ?

• are you sure you just want to repeat the number on a 2nd column and add "%" to it, or do you instead need to really calculate the percentage, ie : 1) read them all and sum them up, 2) then find out the % of each one (in regard to the total) and put that in the 2nd column ? That would make all the answers below irrelevant but it would probably make more sense... Oct 15, 2013 at 10:45
• Oh no i want percantage calculation and not simply adding % symbol Oct 15, 2013 at 13:23
• then read my answer, it provides calculation (but may not be what you wanted? let me know by commenting under it) Oct 15, 2013 at 13:40
• @slm I have edited and represented my best Oct 16, 2013 at 1:29
• And I updated my answer now too. Thanks for claryfying (we couldn't guess you needed to calculate a % over 600 until you mentionned it ^^) Oct 16, 2013 at 8:47

If your file is really just a list of numbers, one per line, do this:

``````awk '{print \$1,\$1"%"}' numbers.txt
``````

or, in `sed`:

``````sed -r 's/(.+)/\1 \1%/' numbers.txt
``````
• I want calculation Oct 15, 2013 at 13:24
• @rajagenupula you want calculation of what? Your question simply replaces each number with the same number followed by a `%`. Please show us what you actually need, we are not psychic :). What does the percentage represent?
– terdon
Oct 15, 2013 at 18:28

ok, now the questino changed almost completely ^^ You now need to calculate, given numbers, how many % they represent relative to the number 600.

Here is a revised version.

I let my old answer below for historical reason ^^

``````awk ' { printf "%s %.2f%\n",\$1,(\$1/600)*100; }'  numbers.txt
``````

ie, assuming the file "numbers.txt" only contain 1 column with a number between 0 and 600, it just print the number, and in the next column the % it represents with regard to 600. I could simplyfy the 2nd calculation as (\$1/6)"%" but it would, in my opinion, take out the important information out of the script.

on your new example data it now outputs:

``````459 76.50%
455 75.83%
463 77.17%
``````

If you really need to calculate the percentage, then it would be something like:

``````awk '
{ # each line is read and stored, and the sum is computed also.
original[NR]=\$0 ; #store the current line (line NR) in the "original[]" tab
sum+=\$1       ; #and compute the sum of the 1st column
}
END  { #at the END, ie after we processed the whole file
for(line=1;line<=NR;line++)
{   printf "%s %.2f%\n",original[line],original[line]/sum*100 ;
}
} '  numbers.txt
``````

something like this should compute the % and put it next to the number (with 2 fractionnal digits)

on your given example it outputs:

``````12 5.36%
23 10.27%
35 15.62%
67 29.91%
87 38.84%
``````
• I'm aware that it doesn't match his "expected output", but at the same time the "expected output" doesn't seem to match the "compute the percentage" sentence a bit earlier... hence I propose this as an alternative in case the sentence was more important than the given output. Oct 15, 2013 at 10:59
• But actually for 12 the % is 12% is right ?? could you plz elaborate it. Oct 15, 2013 at 14:03
• I will give you more clarification . Example ... i have scored 459 marks for total 600 marks. So whats the % . That 459 has stored in a txt file. So this is the theory i think enough to solve Oct 15, 2013 at 14:10
• @rajagenupula Please edit your question and provide a sample input of 2-3 lines and the desired output. Oct 15, 2013 at 15:36
• @rajagenupula: now that you precised you wanted a "% in regard to 600" I updated my answer. Thanks for claryfying! (but please next time you ask a question, be more specific and provide the context. Try to put yourself in our shoes: we couldn't at all guess you needed that ^^ a % could be anything, on anything, in regard to anything...) Oct 16, 2013 at 8:50
``````\$ paste -d' %' numbers.txt numbers.txt /dev/null
12 12%
23 23%
35 35%
67 67%
87 87%
``````

`-d` switch takes multiple delimiters: White-space and Percentage here

• Smart, albeit non-intuitive, use of paste! Oct 15, 2013 at 3:37
• +1 because it helps discover 'paste' and one of its nice features. Clever. (I resisted saying "That's a tough paste!" ...) Oct 15, 2013 at 10:44

Same as @slm's, just using character class instead:

``````\$  sed -r 's/([[:digit:]]+)/\1 \1%/g' numbers.txt
``````

This can be done quite easily with `sed`.

If the numbers are arranged:

``````12 23 35 67 87
``````

### Example

``````\$ sed 's/\([0-9]\+\)/\1 \1%/g' somefile.txt
12 12% 23 23% 35 35% 67 67% 87 87%
``````

### Details

1. We're making use of `sed`'s ability to search and replace (`s/../../g`).
2. `\([0-9]\+\)` matches any sequence of numbers (12, 23, ..). The parens around this saves these results in a temporary variable (`\1`).
3. Each time we find a matching sequence of numbers, we'll replace it with itself & another copy of itself and a percentage sign (`\1 \1%`)

NOTE: If you're version of `sed` supports the `-r` switch you can condense the above slightly:

``````\$ sed -r 's/([0-9]+)/\1 \1%/g' somefile.txt
12 12% 23 23% 35 35% 67 67% 87 87%
``````

If they're arranged:

``````12
23
35
67
87
``````

### Example

``````\$ sed -r 's/([0-9]+)/\1 \1%/g' somefile.txt
12 12%
23 23%
35 35%
67 67%
87 87%
``````

Why not use a simple `cat` followed by `echo` by building and executing command line from stdin?

``````cat somefile.txt|xargs -I {} echo "{} {}%" > somefile.out
``````
• I want calculation not just adding a symbol Oct 15, 2013 at 15:00