Here's An Easy One for You(Obviously I'm too Stupid) 
Author Message
 Here's An Easy One for You(Obviously I'm too Stupid)


Quote:

>Okay ladies and gents,  here's an easy one for you.  I'm very inexperienced
>using awk and sed, and I guess I've been lucky so far, until now.  All I'm
>trying to do is to find a character position on each line, and input another
>character.

>For example:
> I want to take the following string - 12345600
> and make it 123456.00.  Keeping in mind, the number I'm working with is
> dollar amount, so the decimal will obviously fall immediately prior to the
> last two digits(regardless of what the last two digits are).

sed -e 's/\([0-9]*\)\([0-9][0-9]\)/\1.\2/g' infile > outfile

will change all consecutive strings of 2 or more numbers and insert a
decimal point before the last two numbers.

I assumed you wanted something more than:

sed -e 's/12345600/123456.00/g' infile > outfile

which changes 12345600 to 123456.00 everywhere that string appears
in the infile.

Quote:

>I also need to be able to go to a particular character position and replace
>a zero with two spaces.  This is difficult because there are zeros thoughout
>the file I'm working on.

sed -e 's/^\(......\)0/\1  /' infile > outfile

This replaces the zero after the first six characters with two spaces
on all lines where this occurs.

Vary the number of periods to vary the number of leading characters.
There are 6 periods in the above command.

See man sed.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Tue, 31 Oct 2000 03:00:00 GMT  
 Here's An Easy One for You(Obviously I'm too Stupid)

Quote:

> For example:
>  I want to take the following string - 12345600
>  and make it 123456.00.  Keeping in mind, the number I'm working with is
>  dollar amount, so the decimal will obviously fall immediately prior to the
>  last two digits(regardless of what the last two digits are).

> I also need to be able to go to a particular character position and replace
> a zero with two spaces.  This is difficult because there are zeros thoughout
> the file I'm working on.

> I'd really appreciate any and all help that anybody could offer.

> R.

I can only help you according to your example..
(problem A) change 12345600
        (a) if you know the field position of the string. i.e. $4
(long way for expanation purpose)
        this procedure will convert any number string from xxxyy to xxx.yy
                                                        xxxxyy to xxxx.yy
        thekeylen = length($4);
        svalue = substr($4, 1, thekeylen -2) "." substr($4, thekeylen -1, 2);
        printf "%s", svalue;

(problem B) if you know the extract position of the string...
    GIVEN: (1) the position of 0s start at 40
           (2) number of zeros to be replaced = 2
    TRYTHIS:
        print substr($0, 1, 20) "  " substr($0, 1, length($0) - 22);

hope this help

Simon



Thu, 02 Nov 2000 03:00:00 GMT  
 Here's An Easy One for You(Obviously I'm too Stupid)

Quote:


> >  I want to take the following string - 12345600
> >  and make it 123456.00.

> this procedure will convert any number string from xxxyy to xxx.yy
>      xxxxyy to xxxx.yy
> thekeylen = length($4);
> svalue = substr($4, 1, thekeylen -2) "." substr($4, thekeylen -1, 2);
> printf "%s", svalue;

How do you convert the value 123 to 1.23? You divide by 100.
Simple arithmetic.

The beauty of awk is that you can do arithmetic with "strings", thus:

$ awk 'BEGIN { printf("%.2f\n", "12345600" / 100); exit }'
123456.00
$ awk 'BEGIN { printf("%.2f\n", 12345600 / 100); exit }'
123456.00
$

Same-o, same-o!

--
Jim Monty

Tempe, Arizona USA



Thu, 02 Nov 2000 03:00:00 GMT  
 Here's An Easy One for You(Obviously I'm too Stupid)


:> >  I want to take the following string - 12345600
:> >  and make it 123456.00.
:>
:> this procedure will convert any number string from xxxyy to xxx.yy
:>      xxxxyy to xxxx.yy
:> thekeylen = length($4);
:> svalue = substr($4, 1, thekeylen -2) "." substr($4, thekeylen -1, 2);
:> printf "%s", svalue;

: How do you convert the value 123 to 1.23? You divide by 100.
: Simple arithmetic.

: The beauty of awk is that you can do arithmetic with "strings", thus:

This can be very misleading, while it sometimes looks like you can,
if you want your code to be reliable, you really should investigate
how awk really does this.
The short version is that it does "magic" typing, whereby if you treat
it like a number, it will be a number, if like a string, it's a string.
"a"+10 = 10
"a"+"b" = 0
"10"+10 = 20
"10"+10 = 20

etc, etc

: $ awk 'BEGIN { printf("%.2f\n", "12345600" / 100); exit }'
: 123456.00
: $ awk 'BEGIN { printf("%.2f\n", 12345600 / 100); exit }'
: 123456.00
: $

: Same-o, same-o!

: --
: Jim Monty

: Tempe, Arizona USA

--
Ian Stirling.   Designing a linux PDA, see  http://www.mauve.demon.co.uk/
----- ******* If replying by email, check notices in header ******* -----
Lord, grant me the serenity to accept that I cannot change, the
courage to change what I can, and the wisdom to hide the bodies
of those I had to kill because they pissed me off.       Random



Wed, 08 Nov 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Easy one: Highlight execution works, but subvi's are skipped when it is turned off

2. Here's an easy one...

3. stupid me - subject is 'Book Idea'

4. 'stupid' 32bit regie question

5. It's stupid but I don't know fortran :-(((

6. Proably easy: 'symbol undefined' errors

7. Probably it's easy, but I don't know how to do :(

8. One's clutter is another's lesson

9. Score one for the 'little guy'

10. More than one 'Range Limit Field'

11. One and One doesn't equal two It equals 1 - The Who

 

 
Powered by phpBB® Forum Software