Replace character by awk 
Author Message
 Replace character by awk

Does anyone know how I would replace a character in a string by
another charactor.
for example, "abc0def" would be abc3def".

Thanks



Tue, 30 Jul 2002 03:00:00 GMT  
 Replace character by awk


Quote:
>Does anyone know how I would replace a character in a string by
>another charactor.
>for example, "abc0def" would be abc3def".

>Thanks

use the gsub or sub functions, here's an example:

gawk '{gsub(/abc0def/,"abc3def");print}' infile > outfile

read the man page,

man awk
man gawk

Chuck Demas
Needham, Mass.

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

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



Tue, 30 Jul 2002 03:00:00 GMT  
 Replace character by awk
Quote:

>Does anyone know how I would replace a character in a string by
>another charactor.

There are two functions:
sub(REGEXP, REPLACEMENT [, TARGET])
gsub(REGEXP, REPLACEMENT [, TARGET])

sub replaces only first occurrence of REGEXP, gsub replaces all of them.

Quote:
>for example, "abc0def" would be abc3def".

str = "abc0def"
sub("0", "3", str)

--

GC{S,M} d- s+:+ a18 C+++ UL++ L+++ E--- W
P- N++ w-- O- t--- X+ b- DI+ e- h* r++ y?



Tue, 30 Jul 2002 03:00:00 GMT  
 Replace character by awk

: Does anyone know how I would replace a character in a string by
: another charactor.
: for example, "abc0def" would be abc3def".

Awk doesn't really support that kind of operation.  You could do it
with the "substr" function but it would be ugly.  Most systems these
days have nawk in addition to awk (or maybe instead of awk).  Try this:
    echo "abc0def" | nawk '{sub(0, 3, $0); print $0; }'

--
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

      Consulting                   wrong with a world in which Ken
      Modeling                     Thompson lives in obscurity and
      Training                     Bill Gates is a famous billionaire.
//////////////////////////////////////////////////////////////////////



Wed, 14 Aug 2002 03:00:00 GMT  
 Replace character by awk

Quote:


> : Does anyone know how I would replace a character in a string by
> : another charactor.
> : for example, "abc0def" would be abc3def".

This sounds like a "how do I drive this nail into the wall using
a screwdriver"-question to me.

If you really do not need any context (i.e. replace characters
only within special words), you should use "tr" for this purpose:

        tr '0' '3'

would replace (or "TRanslate") each "0" in the input text
to a "3".

Heiner
--
 ___ _                          

\__ \  _/ -_) V / -_) ' \    UNIX Shell Script Programmers: visit
|___/\__\___|\_/\___|_||_|   http://www.oase-shareware.org/shell/



Sun, 18 Aug 2002 03:00:00 GMT  
 Replace character by awk

Quote:



> > : Does anyone know how I would replace a character in a string by
> > : another charactor.
> > : for example, "abc0def" would be abc3def".

> This sounds like a "how do I drive this nail into the wall using
> a screwdriver"-question to me.

> If you really do not need any context (i.e. replace characters
> only within special words), you should use "tr" for this purpose:

>         tr '0' '3'

> would replace (or "TRanslate") each "0" in the input text
> to a "3".

> Heiner

Hello,

 I was deceived by your answer, because it doesn't correspond to the
question. I suppose that
 Cal knows tr and even sed, so to tell him to use tr is unuseful.



Thu, 22 Aug 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Issues with Non-COBOL characters and REPLACE / REPLACING

2. string.replace() can't replace newline characters???

3. Replacing a string from an input file within awk script

4. How to replace one or two words with one word with one line of awk code

5. 2-file awk search-and-replace

6. replace variable with awk

7. Window's 2000 Global Find and Replace Solution using awk

8. Sed/AWK - replace positions in multiple lines.

9. awk "search and replace"

10. Problem replacing [ and ] in AWK (HP-UH10.20)

11. Replacing text using awk (but in a c shell)

12. fast-loading lisp sought to replace awk and zsh

 

 
Powered by phpBB® Forum Software