formating number strings


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers formating number strings
# 1  
Old 03-24-2004
formating number strings

I have treid searching for what I thought would be a common question...but to no avail.


I have to long numbers (4-8) digits. how can I format them so they are printed as 1,234,567 rather than 1234567? I have no idea where to start, have played with varying types of awk, print, substr.

Also, pecentages.....if you have total amount and amount used how, using expr, can you work out the percentage used? I get 0 as the value.

pecent=`expr $used / $avail \* 100`

percent, used, and avail have all been set previously as an integer value.

PS. I'm no mathsmatitiojkljknjn.....how ever you spell it, so my logic may be wrong.

Cheers,
Neil
# 2  
Old 03-24-2004
Try:
Code:
Percent=$(bc << %%
   scale=4
   ( ${used} / ${avail} ) * 100
   quit
 %%)

# 3  
Old 03-24-2004
genius.....don't know what it all means but it works.

Cheers and beers,
Neil
# 4  
Old 03-24-2004
Neither used nor avail sounds like a total and you need a total. But, for example 2 is 50% of 4. My mathematical training enables me to construct:
expr 2 \* 100 / 4
which will result in 50.

Since you are using expr, you must be using a weak shell. One solution for formatting numbers might be sed:
echo 1234567 | sed 's/\(.\)\(...\)\(...\)/\1,\2,\3/'
which will only work for 7 digit numbers. You will need similiar sed expressions for other number lengths and you will need to select the right one with if statements.
# 5  
Old 03-24-2004
many thanks.......

This may not be allowed V's questions........

But I'm using ksh....is this a weak shell? if it is what makes it so......I'm always looking to improve my scripting and if changing the shell or the language is required, then so be it.
# 6  
Old 03-24-2004
From: HANDY ONE-LINERS FOR SED
Quote:
# add commas to numeric strings, changing "1234567" to "1,234,567"
gsed ':a;s/\B[0-9]\{3\}\>/,&/;ta' # GNU sed
sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' # other seds

# add commas to numbers with decimal points and minus signs (GNU sed)
gsed ':a;s/\(^\|[^0-9.]\)\([0-9]\+\)\([0-9]\{3\}\)/\1\2,\3/g;ta'
# 7  
Old 03-24-2004
The shell can do integer arithmetic, you don't need to use expr:-

(( percent = 100 * $used / $avail ))
echo $percent
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count the number of strings

I have 500 text files in a folder. The data of the text files are shown below. USA Germany 23-12 USA Germany 23-12 USA Germany 23-12 France Germany 15-12 France Germany 15-12 France Italy 25-50 China China 30-32 China China 30-32 I would... (1 Reply)
Discussion started by: sahith
1 Replies

2. Shell Programming and Scripting

How to count the number of strings?

Hi, I have a text file as shown below. I would like to count the unique number of connections of each person in the first and second column. Third column is the ID numbers of first column persons and fourth column is the ID numbers of second column persons. susan ali 156 294... (7 Replies)
Discussion started by: mohamad
7 Replies

3. Shell Programming and Scripting

Number of matches in 2 strings

Hello all, I have a file with column header which looks like this. C1 C2 C3 A A G T T A G C CI want to make columnwise (and bitwise) comparison of strings and calculate the number of matches. So the number of matches between C1 and C2 will be comparing ATG and ATC. Here there are... (5 Replies)
Discussion started by: newbie83
5 Replies

4. Shell Programming and Scripting

Finding number of strings in List

I have a list of strings stored in $Lst Example set Lst = "John Fred Kate Paul" I want to return 4 in this case. (1 Reply)
Discussion started by: kristinu
1 Replies

5. UNIX for Dummies Questions & Answers

Count the number of strings in a block

Hi, I have the following text in a file: ISA*00* *00* *ZZ*ENS_EDI *ZZ*GATE0215 *110106*2244*U*00401*006224402*1*P*>~ GS*HP*ENS_EDI*GATE0215*20110106*2244*6224402*X*004010X091A1~ ST*835*00006~... (2 Replies)
Discussion started by: donisback
2 Replies

6. UNIX for Dummies Questions & Answers

Print number of occurrences of many different strings

People, I need your help with making a script which will 1. take as an input the number of lines, smth like this: ((RUBROBACTER_1_PE1288 (((SALINISPORA_1_PE1863 SALINISPORA_1_PE1828)100 ((NOCARDIOIDES_2_PE2419 PROPIONIBACTERIUM_1_PE1395)96 ((((((((CORYNEBACTERIUM_1_PE1119... (3 Replies)
Discussion started by: roussine
3 Replies

7. Shell Programming and Scripting

AWK: formating number without printf

Hello, I wrote a script that does lot of things, and I would like to change the format of a number but without printing it now (so I don't want to use printf as it will print the value immediately). Schematically here is what I have: awk 'BEGIN{number=0.01234567} $1==$2{$3=number}... (5 Replies)
Discussion started by: jolecanard
5 Replies

8. Shell Programming and Scripting

calculate number of strings in a variable

Hi all I have a variable called "variable" and is of the form variable ="AAA BBB CCC DDD" {basically it has values separated by spaces} What is the simplest way to check if "variable" has more that one value in its list? Thanks. (9 Replies)
Discussion started by: felixmat1
9 Replies

9. Shell Programming and Scripting

Varying number of awk search strings

I've created an awk script that handles a varying number of search strings handed to it as command line parameters ($1 $2 etc). There may be 1, or 2 or 3 or more. A simplified version of the script is: awk -v TYP="$1 $2 $3 $4 $5 $6" ' BEGIN { CTYP = split (TYP,TYPP," ") } ... (2 Replies)
Discussion started by: CarlosNC
2 Replies

10. Shell Programming and Scripting

printing strings in one X number of times from another

I have one file of numbers 4 5 2 ... And another file of strings aaaaa bbbbb ccccc ddddd eeeee ffffff ... I'd like to print the stings from each line in reverse order with some decoration the number of times listed in the first file such as: Yeah bbbbb aaaaa Yeah bbbbb aaaaa (5 Replies)
Discussion started by: dcfargo
5 Replies
Login or Register to Ask a Question