This discussion is archived
1 Reply Latest reply: Jul 15, 2012 2:54 PM by Nik RSS

How to replace a character in a file using awk

slsam01 Newbie
Currently Being Moderated
Hello,

Does anyone has a sample script to replace a character in a file using awk?

Regards,

Edited by: slsam01 on Jul 15, 2012 12:56 PM
  • 1. Re: How to replace a character in a file using awk
    Nik Expert
    Currently Being Moderated
    Hi

    awk it's not best for this job.

    For replace charecter - generaly use tr or sed

    Example on awk:

    awk -F<old_char> ' { for (i=1; i<NF ;i++ ) printf ( "%s<new_char>", $i ); print $NF }' <file>
    #Replace "e" on "a" in /etc/hosts
    #
    awk -Fe ' { for (i=1; i<NF ;i++ ) printf ( "%sa", $i ); print $NF }' /etc/hosts


    Regards.


    PS. Same on *tr"
    cat file | tr "e" "a"

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points