Can someone tell me how to print a dollar amount (i.e. 1140.95) as 114095?


Via awk I presume...

echo 1140.95 | awk -F. '{ print $1 $2 }'

-F tells it the field delimiter. But I have a feeling you are looking for something a little different. Can you post the actual line you are trying to parse and what the result should look like and I can do better.

A sample line of what I'm trying to process:

D0010123456789 36404 1140.95 20090108 1 consistent computer bargains

How the parsed line should look:

D00101234567890000036404000000011409520090108 CONSISTENT COMPUTER BARGAINS

I've got most of this worked out except for the dollar amount and I think one other thing.

The lazy answer is get every thing else done and pipe it to the awk command I gave in my last answer. The only issue I see is if a "." was to show up somewhere else in the line.

You could use Substitution Functions and substitute nothing for the period...

Awk provides two substitution functions: sub() and gsub(). The difference between them is that gsub() performs its substitution globally on the input string whereas sub() makes only the first possible substitution. This makes gsub() equivalent to the sed substitution command with the g (global) flag.

Both functions take at least two arguments. The first is a regular expression (surrounded by slashes) that matches a pattern and the second argument is a string that replaces what the pattern matches. The regular expression can be supplied by a variable, in which case the slashes are omitted. An optional third argument specifies the string that is the target of the substitution. If there is no third argument, the substitution is made for the current input record ($0).

The substitution functions change the specified string directly. You might expect, given the way functions work, that the function returns the new string created when the substitution is made. The substitution functions actually return the number of substitutions made. sub() will always return 1 if successful; both return 0 if not successful. Thus, you can test the result to see if a substitution was made.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.