Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add commas into number for readability 2

Status
Not open for further replies.

Ternion

MIS
Aug 13, 2003
16
US
I would like to add commas into numbers to make them more readable. Such as, when awk comes across a number, add commas to make "31231104990" into "31,231,104,990". Is this possible with awk or another program?

thanks, Scott
 
Try this :
[tt]
echo "$number" | sed ':loop
s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/
t loop'
[/tt]


Jean Pierre.
 
Thank you Jean Pierre, that works great for just a number, but actually, here is what I'm trying to do:
I'm using the following script to get only the pertinent information from a backup exec log file and sort it by size, big to small.

awk '/Backup of/{printf("%s ",$3)};/Processed/{printf("%s\n",$2)};/Job Operation - Verify/{exit}' c:/temp/bex.txt | sed s/\"//g | sed s/,//g | sort -r -n -k 2

I strip the commas from the number in order to allow the sort to work, but would like to put the commas back in to make it easier to read. Could there be a way within awk to sort the list without removing the commas in the first place? Below is a sample of the "bex.txt" I'm pulling from:

===========================================================
Set Information - \\PCABC\C:
Backup Set Information
Family Name: "Media created 7:00:07 PM 3/12/2004 - Fri Full Backup - Work / User Job"
Backup of "\\PCABC\C: "
Backup set #12 on storage media #6
Backup set description: "Fri Full Backup - User Job"
Backup Type: FULL - Back Up Files - Reset Archive Bit



Backup started on 3/27/2004 at 1:23:45 AM.
Backup completed on 3/27/2004 at 2:10:06 AM.
Backup Set Summary
Backed up 7613 files in 486 directories.
Processed 2,994,485,909 bytes in 46 minutes and 21 seconds.
Throughput rate: 61.6 MB/min
============================================================

The output of the script is:

\\PCABC\C: 2994485909


thanks again for everone's help!

Scott
 
Try this :
[tt]awk '/Backup of/{printf("%s ",$3)};/Processed/{printf("%s\n",$2)};/Job Operation - Verify/{exit}' c:/temp/bex.txt | sed s/\"//g | sed s/,//g | sort -r -n -k 2 | sed -e ':loop' -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;t loop'[/tt]



Jean Pierre.
 
You can try something like this:
awk '
/Backup of/{share=$3;gsub(/"/,"",share)}
/Processed/{key=$2;gsub(/,/,"",key)printf("%s %s %s\n",key,share,$2)}
/Job Operation - Verify/{exit}
' c:/temp/bex.txt | sort -r -n | sed 's!^[ 0-9]*!!'


Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
That works great! Thank you so much for your help. I should have realized that I could just continue the pipe on into the sed. As long as we're on a roll here, is there a resonably handy way to pad the spaces between the system name/share and the size so that the size portion is right justified? Its not really needed, but it would make it easier to read. Below is a larger sample output:

\\PCABC\C: 2,994,485,909
\\PCDEF\C: 2,286,570,299
\\PCGHI\C: 2,107,089,738
\\PCJKL\C: 796,474,951
\\PCMNO\C: 779,595,962
\\PCPQR\D: 681,290,833

But it would be nice for it to look like:

\\PCABC\C: 2,994,485,909
\\PCDEF\C: 2,286,570,299
\\PCGHI\C: 2,107,089,738
\\PCJKL\C: 796,474,951
\\PCMNO\C: 779,595,962
\\PCPQR\D: 681,290,833

Just curious. Thanks again for the help, it worked so well!

thanks,
Scott

 
You can try something like this:
awk '
/Backup of/{share=$3;gsub(/"/,"",share)}
/Processed/{key=$2;gsub(/,/,"",key)printf("%s %-20s %16s\n",key,share,$2)}
/Job Operation - Verify/{exit}
' c:/temp/bex.txt | sort -r -n | sed 's!^[ 0-9]*!!'


Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Hey PHV,
I tried it and got the following error:
/cygdrive/c/docume~1/saw.TERNION/desktop-> domaxbackups.sh
awk: cmd. line:3: /Processed/{key=$2;gsub(/,/,"",key)printf("%s %s %s\n",key,sha
re,$2)}
awk: cmd. line:3: ^ syntax error

The syntax error is pointing to the printf on the "Processed" line. I'm not sure how the "gsub" part works, but it look interesting.
thanks,
Scott

 
Sorry for the typo, just add a semi-colon before the printf, like this:
awk '
/Backup of/{share=$3;gsub(/"/,"",share)}
/Processed/{key=$2;gsub(/,/,"",key)[highlight];[/highlight]printf("%s %-20s %16s\n",key,share,$2)}
/Job Operation - Verify/{exit}
' c:/temp/bex.txt | sort -r -n | sed 's!^[ 0-9]*!!'


Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Hey PHV, Yeah, I just found that I forgot to put in the semi-colon before the printf. I added it and it worked great like Jean Pierre's example. Both ways work so slick. Thank you both for your help!
If anyone knows of a handy way to right-justify the number while keeping the system name left-justified, that would be nice, but it would just be an extra kind of thing; not a requirement.

thanks again, Scott
 
Ternion, your extra kind is already in my 2 previous post!

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Oop, my bad. I apologize, I missed it. I just thought that the bolded letters in the printf above were an HTML hiccup of the site here. Actually, it was a hiccup of my brain. That works perfect! Again, many thanks!

Scott
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top