How to insert an IF statement in NAWK

Posted on 2009-04-22
Last Modified: 2012-05-06

In the code below I'm trying to add an "if" statement saying if in field $10 the first two letters are "jz" then add to fields $12, $13 and $14 the following |text1|text2|text3.
Question by:Dwebb33
    LVL 84

    Accepted Solution

    nawk '$10~/^jz/{$12=$12 "|text1|text2|text3.";$13=$13 "|text1|text2|text3.";$14=$14 "|text1|text2|text3." };1'

    Author Comment

    I've inserted your comment, it works fine, except that I loose the field separators "|" from the previous fields ($1 to $10).

    nawk '
    BEGIN {
    FNR==NR { a[toupper ($2)]=$1;next}
    FNR==1 {FS=FS1;$0=$0}
    $10~/^jz/{$12=$12 FS "Text1";$13=$13 FS "Text2";$14=$14 FS "Text3" ; print }
    $10 in a {$0=$0 FS a[$10]; print} ' File2.txt File1.txt > result.txt

    Open in new window

    LVL 84

    Expert Comment

    do you want to set OFS="|" ?

    Author Comment

    Thanks for the comment, but I already did ;)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now