awk "search and replace" 
Author Message
 awk "search and replace"

I'm using the following code to extract and reformat data from an
input file.

Input: Some Interesting Data abc xyz

awk '/Some Interesting Data/{print $1,$2,$3","$4","$5}' input.txt >
output.txt

Resulting output: Some Interesting Data,abc,xyz

What I'd like to do is to reformat the search string /Some Data/ when
it is found in the input file and print that reformatted data to the
output file. When the input string /Some Data/ is found, it is
reformatted to My New Data

The result would be:
My New Data,abc,xyz

Basically, I want to do a "search and replace".

How do I do that in AWK?

Thanks

--
Luis
(remove * to reply by mail)



Fri, 15 Aug 2003 17:56:09 GMT  
 awk "search and replace"

Quote:

> I'm using the following code to extract and reformat data from an
> input file.

> Input: Some Interesting Data abc xyz

> awk '/Some Interesting Data/{print $1,$2,$3","$4","$5}' input.txt >
> output.txt

> Resulting output: Some Interesting Data,abc,xyz

> What I'd like to do is to reformat the search string /Some Data/ when
> it is found in the input file and print that reformatted data to the
> output file. When the input string /Some Data/ is found, it is
> reformatted to My New Data

> The result would be:
> My New Data,abc,xyz

> Basically, I want to do a "search and replace".

> How do I do that in AWK?

> Thanks

> --
> Luis
> (remove * to reply by mail)

How about:

awk '/Some Interesting Data/{print "My New Data,"$4","$5}' input.txt >
output.txt

--
Cheers,
Ted J. Loeffelholz
Senior Design Engineer
Caterpillar Inc



Fri, 15 Aug 2003 22:12:28 GMT  
 awk "search and replace"

Quote:

>I'm using the following code to extract and reformat data from an
>input file.

>Input: Some Interesting Data abc xyz

>awk '/Some Interesting Data/{print $1,$2,$3","$4","$5}' input.txt >
>output.txt

>Resulting output: Some Interesting Data,abc,xyz

>What I'd like to do is to reformat the search string /Some Data/ when
>it is found in the input file and print that reformatted data to the
>output file. When the input string /Some Data/ is found, it is
>reformatted to My New Data

>The result would be:
>My New Data,abc,xyz

>Basically, I want to do a "search and replace".

>How do I do that in AWK?

Use the sub or gsub function.

man awk
man nawk
man gawk

So much homework, so little time!

Chuck Demas

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

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



Fri, 15 Aug 2003 22:29:16 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. replace string AFTER "size","initial", "next"

2. string.join(["Tk 4.2p2", "Python 1.4", "Win32", "free"], "for")

3. What replaces "EXIT"?

4. Replacing "Shutdown" Action Button in LaunchPad

5. Rookie question: "Replace"-command?

6. Replacing "\"s using re.sub

7. Replace with regular expressions "except if..."

8. 2-file awk search-and-replace

9. "&" and Awk

10. "Embedding" an awk script in bash

11. Searching information about "learningworks"

12. Looking for "pretty-printer"/reformatter for AWK

 

 
Powered by phpBB® Forum Software