• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 491
  • Last Modified:

How to insert an IF statement in NAWK

Hi,

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.
0
Dwebb33
Asked:
Dwebb33
  • 2
  • 2
1 Solution
 
ozoCommented:
nawk '$10~/^jz/{$12=$12 "|text1|text2|text3.";$13=$13 "|text1|text2|text3.";$14=$14 "|text1|text2|text3." };1'
0
 
Dwebb33Author Commented:
I've inserted your comment, it works fine, except that I loose the field separators "|" from the previous fields ($1 to $10).


#!/bin/bash
 
nawk '
BEGIN {
 
  FS1="|"
  FS2=","
  FS=FS2
}
 
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

0
 
ozoCommented:
do you want to set OFS="|" ?
0
 
Dwebb33Author Commented:
Thanks for the comment, but I already did ;)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now