Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果在文件行中找到某个值,则使用AWK打印相邻字段_Awk - Fatal编程技术网

如果在文件行中找到某个值,则使用AWK打印相邻字段

如果在文件行中找到某个值,则使用AWK打印相邻字段,awk,Awk,我有以下数据填充 Cross,Tim,43560,0000000000,0000000000 Smith,Sam,00000,4192125555,4192088888 Williams,Jill,00000,0000000000,4199139999 Wright,Mark,27645,0000000000,7042964444 Alston,Jacob,00000,2025674962,0000000000 如果填充了相应的字段,我需要输出来标记这些字段。目标字段是ZIP、HOM

我有以下数据填充

 Cross,Tim,43560,0000000000,0000000000
 Smith,Sam,00000,4192125555,4192088888
 Williams,Jill,00000,0000000000,4199139999
 Wright,Mark,27645,0000000000,7042964444
 Alston,Jacob,00000,2025674962,0000000000
如果填充了相应的字段,我需要输出来标记这些字段。目标字段是ZIPHOMEWORK,分别是来自内嵌的
$3
$4
$5
列。如果任何字段的值为null或0,则相应的输出字段标签将为空。以下是预期输出的外观:

 Cross,Tim,43560,ZIP,0000000000,,0000000000,
 Smith,Sam,00000,,4192125555,HOME,4192088888,WORK
 Williams,Jill,00000,,0000000000,,4199139999,WORK
 Wright,Mark,27645,ZIP,0000000000,,7042964444,WORK
 Alston,Jacob,00000,,2025674962,HOME,0000000000,
我已尝试使用AWK,但似乎无法针对
打印

不起作用的代码

zip1=ZIP
home1=HOME
work1=WORK
awk -F ,  '{
        
        if ($3 ~ "00000")
                "'${zip1}'"=="";
        if ($4 ~ "0000000000")
                "'${home1}'"=="";
        if ($5 ~ "0000000000")
                "'${work1}'"=="";

        print $1 "," $2 "," $3 "," "'${zip1}'" "," $4 "," "'${home1}'" "," $5 "," "'${work1}'"}' $inputfile

此代码只打印变量,而不考虑
if
语句。如果我直接在1
If
语句下打印,它当然只会针对该
If
语句进行打印。不确定如何让单个
打印
识别每个
if
条件。

请尝试以下操作。在
awk
中使用三元运算符

awk '
BEGIN{
  FS=OFS=","
}
{
  $3=($3==0?$3 OFS:$3 OFS "ZIP")
  $4=($4==0?$4 OFS:$4 OFS "HOME")
  $5=($5==0?$5 OFS:$5 OFS "WORK")
}
1' Input_file

说明:添加上述内容的详细说明

awk '                                ##Starting awk program from here.
BEGIN{                               ##Starting BEGIN section from here.
  FS=OFS=","                         ##Setting field separator and output field separator as comma here.
}
{
  $3=($3==0?$3 OFS:$3 OFS "ZIP")     ##Checking condition if 3rd field is 0 then add OFS to it else add OFS and string ZIP to it.
  $4=($4==0?$4 OFS:$4 OFS "HOME")    ##Checking condition if 4th field is 0 then add OFS to it else add OFS and string HOME to it.
  $5=($5==0?$5 OFS:$5 OFS "WORK")    ##Checking condition if 5th field is 0 then add OFS to it else add OFS and string WORK to it.
}
1                                    ##Will print current line.
' Input_file                         ##Mentioning Input_file name here.                              

请在您的问题中添加您的努力,这是非常鼓励的。请在提交问题之前阅读您申请的“linux”标签的说明。作为这里的一个新用户,也请阅读。非常优雅的答案,比我所尝试的要简单得多。谢谢
awk '                                ##Starting awk program from here.
BEGIN{                               ##Starting BEGIN section from here.
  FS=OFS=","                         ##Setting field separator and output field separator as comma here.
}
{
  $3=($3==0?$3 OFS:$3 OFS "ZIP")     ##Checking condition if 3rd field is 0 then add OFS to it else add OFS and string ZIP to it.
  $4=($4==0?$4 OFS:$4 OFS "HOME")    ##Checking condition if 4th field is 0 then add OFS to it else add OFS and string HOME to it.
  $5=($5==0?$5 OFS:$5 OFS "WORK")    ##Checking condition if 5th field is 0 then add OFS to it else add OFS and string WORK to it.
}
1                                    ##Will print current line.
' Input_file                         ##Mentioning Input_file name here.