awk: Find and Replace text

On August 28, 2009, in General Linux, by linuxtips
Post to Google Buzz
Bookmark this on Yahoo Bookmark
Bookmark this on Digg
Share on reddit
Share on StumbleUpon



Syntax for awk

awk <options> 'Program' Input-File1 Input-File2 ...
awk -f PROGRAM-FILE <options> Input-File1 Input-File2 ...

If no Input-File is specified then `awk’ applies the Program to “standard input”, (piped output of some other command or the terminal. Typed input will continue until end-of-file (typing `Control-d’)

Example for search and replace

awk  ‘{ gsub( /search_string/ , replacestring , $0 ); print }’ file

If you want to write the output to a file, then simple redirect the output as shown below:

awk  ‘{ gsub( /search_string/ , replacestring , $0 ); print > output_file }’ file

Hope, it helps!

Tagged with:  

2 Responses to “awk: Find and Replace text”

  1. Matthew says:

    So where's the replace portion of the post? I don't see any sub()s or the like …

  2. Damir Colak says:

    Find and replace text and there is no REPLACE. Bravó!

Leave a Reply

*

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...