Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2009
    Posts
    14

    Unanswered: nawk - passing a variable to a search cmd

    The code below replaces the search_str with the rep_str in the example.dat file ... and it works if I put the actual text (no variable) in the nawk search statement. However I need to change it to accept such a variable instead as ultimatley I want to loop through every DB on the server.

    The -v option I have hashed out didn't work. Anybody know how to do this? Any help would be greatly appreciated.



    SID=TESTDB1

    search_str="Search_Example_$SID"

    rep_str="Replace_Ex_$SID \nReplace_Ex_line2_${SID}"

    #nawk -v awkvar="$search_str" ' {
    nawk ' {
    if(/^$search_str/) # This is the part doesn't work if I use a variable.
    $0=v1

    print
    } ' v1="$rep_str" example.dat

  2. #2
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Talking

    Try this:

    Code:
    SID=TESTDB1
    
    search_str="Search_Example_$SID"
    
    rep_str="Replace_Ex_$SID \nReplace_Ex_line2_${SID}"
    
    nawk -v awkvar="$search_str" -v v1="$rep_str" '
    {
     sub(awkvar,v1)
     print
    } ' example.dat
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  3. #3
    Join Date
    Sep 2009
    Posts
    14
    That is absolutely perfect!!! Thank you. Thank you. Thank you.
    :-)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •