Add edited numeric strings in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add edited numeric strings in awk
# 8  
Old 11-08-2013
Code:
printf "%s\n%s\n" 10,250.00 20,103.15 | awk '{var=$1;gsub(/,/,x,var);print var}'
printf "%s\n%s\n" 10,250.00 20,103.15 | awk '{gsub(/,/,x,$1);print $1}'

both gave same result, sir.
Thank you

Last edited by Franklin52; 11-08-2013 at 01:12 PM.. Reason: Please use code tags
# 9  
Old 11-08-2013
Quote:
Originally Posted by paresh n doshi
printf "%s\n%s\n" 10,250.00 20,103.15 | awk '{var=$1;gsub(/,/,x,var);print var}'
printf "%s\n%s\n" 10,250.00 20,103.15 | awk '{gsub(/,/,x,$1);print $1}'
both gave same result, sir.
Thank you

In first command var=$1 and then gsub is used it means $1 will remain unchanged, in second command if $1 contains , then its removed, and $1 itself will be modified

look at the difference copy of $1 is assigned to variable var and then it's modified

Code:
$ printf "%s\n%s\n" 10,250.00 20,103.15 | awk '{var=$1;gsub(/,/,x,var);print var,$1}'
10250.00 10,250.00
20103.15 20,103.15

Here $1 itself is modified.
Code:
$ printf "%s\n%s\n" 10,250.00 20,103.15 | awk '{gsub(/,/,x,$1);print $1}'
10250.00
20103.15

This User Gave Thanks to Akshay Hegde For This Post:
# 10  
Old 11-08-2013
Note that although you need to remove the commas from your input before adding the values to your total, awk is happy to print your results with or without thousands separators. As an example, if you use the following code:
Code:
awk -v fmt="%'16.2f\t%16.2f\n" '
{       gsub(/,/, "")
        sum += $1
        printf(fmt, $1, $1)
}
END {   printf("----------------\t----------------\n")
        printf(fmt, sum, sum)
}' input

and input contains:
Code:
10,250.00
20,103.15
1,234,567,890.12499
2,333,444,555.1359
1234.3
5555555.55
9876

the output produced is:
Code:
       10,250.00	        10250.00
       20,103.15	        20103.15
1,234,567,890.12	   1234567890.12
2,333,444,555.14	   2333444555.14
        1,234.30	         1234.30
    5,555,555.55	      5555555.55
        9,876.00	         9876.00
----------------	----------------
3,573,609,464.26	   3573609464.26

If you want to try this on a Solaris/SunOS system, change awk at the start of this script to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename first N numeric strings in filename

Hello, I have some video files containing numbers and characters . To tell the problem shortly, due to a limitation, I am unable create a playlist regularly changing on a daily basis with the command shuffle....So I decided to rename filenames, just a replacement of first five numbers randomly.... (10 Replies)
Discussion started by: baris35
10 Replies

2. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

3. Shell Programming and Scripting

awk to remove field and match strings to add text

In file1 field $18 is removed.... column header is "Otherinfo", then each line in file1 is used to search file2 for a match. When a match is found the last four strings in file2 are copied to file1. Maybe: cut -f1-17 file1 and then match each line to file2 file1 Chr Start End ... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. Shell Programming and Scripting

filtering a numeric value which has '%' using awk

Hello Gurus, I have a requirement where I have to filter a value from some field which has 99% or greater than '99%'.. For ex: The Date (file -- sample.csv) will be like below Field1,Field2,Field3,Field4 860440512,844284992,16155520,99% 860440512,844284992,16155520,94%... (4 Replies)
Discussion started by: raghu.iv85
4 Replies

5. Shell Programming and Scripting

awk to search similar strings and add their values

Hi, I have a text file with the following content: monday,20 tuesday,10 wednesday,29 monday,10 friday,12 wednesday,14 monday,15 thursday,34 i want the following output: monday,45 tuesday,10 wednesday,43 friday,12 (3 Replies)
Discussion started by: prashu_g
3 Replies

6. UNIX for Dummies Questions & Answers

Listing numeric strings alone

i have a file called 'test.txt' which contains the below data. abc123 123445 123abc 23224q From the above data, i want to display the line which contains only numeric . So i have tried the below commands with sed sed -n '/^$/p' test.txt sed '/^$/!d' test.txt But it... (4 Replies)
Discussion started by: pandeesh
4 Replies

7. Shell Programming and Scripting

sed/awk-adding numeric to a column

I have a txt file as follows Code: Oct 1 file1 4144 Oct 1 file23 5170 Oct 2 file5 3434 Oct 21 file56 2343 I need to add a new column by marking the right log file from current directory. For example populate like this. Please not in the second columt for "1" it has... (2 Replies)
Discussion started by: gubbu
2 Replies

8. Shell Programming and Scripting

AWK: Discrepancy in numeric output

During a file-system cleanup I noticed a strange behavior of awk (HP-UX 11iv3 / IA64). When summing up the size of files in one directory it gives different numbers when using print as opposed to printf: find . -type f -name '*.dmp.Z' -mtime +35 -exec ls -l {} \+ | \ awk 'BEGIN{ OFMT="%f" } {... (1 Reply)
Discussion started by: pludi
1 Replies

9. Shell Programming and Scripting

Perl code to differentiate numeric and non-numeric input

Hi All, Is there any code in Perl which can differentiate between numeric and non-numeric input? (11 Replies)
Discussion started by: Raynon
11 Replies

10. Shell Programming and Scripting

How To Add Numeric Data of A file

Dear All, I want to add Numeric data of my file called temp_f41. But I am not getting how to add them, My File data are in following format of: 10 39 53 05 37 54 Plz send me code for this Thanks Nishant :mad: (3 Replies)
Discussion started by: krishna_sicsr
3 Replies
Login or Register to Ask a Question