Awk 如果其中一列有字母-bash,则跳过该行上的操作

Awk 如果其中一列有字母-bash,则跳过该行上的操作,awk,sed,Awk,Sed,我正在尝试跳过结束时间值为“Failed”的列行上的操作 这是我的真实档案 检查\u time.log Done City Start_time End_time Yes Chicago 18 10 Yes Atlanta 208 11 No Minnetonka

我正在尝试跳过结束时间值为“Failed”的列行上的操作

这是我的真实档案

检查\u time.log

Done  City                               Start_time  End_time
  Yes   Chicago                            18          10
  Yes   Atlanta                            208         11
   No   Minnetonka                          57        Failed
  Yes   Hopkins                           112         80
   No   Marietta                          2018        Failed
这是我到目前为止所拥有的

awk 'BEGIN { OFS = "\t" } NR == 1 { $5 = "Time_diff" } NR >= 2 { $5 = $3 - $4 } 1' < files |column -t
所需的输出应如下所示:

  Done  City                               Start_time  End_time  Time_diff
  Yes   Chicago                            18          10        8
  Yes   Atlanta                            208         11        197
   No   Minnetonka                          57        Failed 
  Yes   Hopkins                            112         80        32
   No   Marietta                          2018        Failed    

那么我如何跳过它呢?

您应该能够更改:

$5 = $4 - $5
进入:

这将:

  • 拒绝将
    $5
    从空更改为结束时间为
    失败的行中的计算值
    ;及
  • 正确计算所有其他行
我说的没错,因为在这些情况下,你似乎想要开始时间减去结束时间,尽管事实上持续时间往往是结束时间减去开始时间。我已将其更改为符合您期望的输出,而不是“正常”的期望

下面是一份成绩单,您可以看到它的作用:

pax$ awk 'BEGIN{OFS="\t"}NR==1{$5="Time_diff"}NR>=2{if($4!="Failed"){$5=$3-$4}}1' <inputFile.txt |column -t
Done  City        Start_time  End_time  Time_diff
Yes   Chicago     18          10        8
Yes   Atlanta     208         11        197
No    Minnetonka  57          Failed
Yes   Hopkins     112         80        32
No    Marietta    2018        Failed

pax$awk'BEGIN{OFS=“\t”}NR==1{$5=“Time_diff”}NR>=2{if($4!=“Failed”){$5=$3-$4}}}}1'请尝试以下内容。(此处考虑到您的输入_文件的最后一个字段仅按此顺序排列,不会有任何其他字段,如果有,则您可能需要调整字段编号,因为如果您所在城市的值中有空间,则字段编号从开始将产生一个问题,即仅区分所有行的值,因为字段值将不同,然后根据行)

输出如下

Done  City        Start_time  End_time  Time_Diff
Yes   Chicago     18          10        8
Yes   Atlanta     208         11        197
No    Minnetonka  57          Failed
Yes   Hopkins     112         80        32
No    Marietta    2018        Failed
解释:现在添加上述代码的完整解释

awk '                      ##Starting awk program from here.
FNR==1{                    ##Checking conditoin if line is very first line then do following.
  print $0,"Time_Diff"     ##Printing current line with string Time_Diff here on very first line to print headings.
  next                     ##next is awk keyword which will skip all further statements from here.
}
$NF!="Failed"{             ##Checking if last field $NF where NF is number of fields and $ means in awk field value is NOT failed then do following.
  $(NF+1)=$(NF-1)-$NF      ##Add a new column by doing $(NF+1) whose value will be difference of 2nd last column and last column as per samples.
}                          ##Closing this condition block here.
1                          ##Mentioning 1 will print edited/non-edited line for Input_file.
' Input_file   |           ##Mentioning Input_file name and passing awk program output to next command by using pipe(|).
column -t                  ##Using column -t will print the output in TAB separated format.

如果您正在考虑Perl

> cat kwa.in 
Done  City                               Start_time  End_time
  Yes   Chicago                            18          10
  Yes   Atlanta                            208         11
   No   Minnetonka                          57        Failed
  Yes   Hopkins                           112         80
   No   Marietta                          2018        Failed
> perl -lane ' print join(" ",@F,"Time_Diff") if $.==1; if($.>1 ) { $F[4]=$F[2]-$F[3] if not $F[3]=~/Failed/; print join(" ",@F) } ' kwa.in | column -t
Done  City        Start_time  End_time  Time_Diff
Yes   Chicago     18          10        8
Yes   Atlanta     208         11        197
No    Minnetonka  57          Failed
Yes   Hopkins     112         80        32
No    Marietta    2018        Failed
> 

欢迎来到SO,很好,你已经展示了你在样本输入和样本输出方面的努力,继续保持下去。这就是为什么我考虑使用
$(NF-1)
$(NF-2)
从最后一个字段开始:)是的,OP必须确认至少OP的文件从最后一个字段也是一样的:)Paxdioblo,看来我在你的回答中遗漏了什么。我添加了if($4!=“失败”)。。但是它不起作用了吗?@Kwaborncana,我添加了一份显示它起作用的成绩单,以防你想对照你所做的更改来检查它。@Kwaborncana,很高兴它帮助了你,也很高兴依靠这个伟大的网站。另外,请参阅我的解释以及警告,它认为您的最后一个字段将只有类似显示的字段,否则我们可能需要查看。
Done  City        Start_time  End_time  Time_Diff
Yes   Chicago     18          10        8
Yes   Atlanta     208         11        197
No    Minnetonka  57          Failed
Yes   Hopkins     112         80        32
No    Marietta    2018        Failed
awk '                      ##Starting awk program from here.
FNR==1{                    ##Checking conditoin if line is very first line then do following.
  print $0,"Time_Diff"     ##Printing current line with string Time_Diff here on very first line to print headings.
  next                     ##next is awk keyword which will skip all further statements from here.
}
$NF!="Failed"{             ##Checking if last field $NF where NF is number of fields and $ means in awk field value is NOT failed then do following.
  $(NF+1)=$(NF-1)-$NF      ##Add a new column by doing $(NF+1) whose value will be difference of 2nd last column and last column as per samples.
}                          ##Closing this condition block here.
1                          ##Mentioning 1 will print edited/non-edited line for Input_file.
' Input_file   |           ##Mentioning Input_file name and passing awk program output to next command by using pipe(|).
column -t                  ##Using column -t will print the output in TAB separated format.
> cat kwa.in 
Done  City                               Start_time  End_time
  Yes   Chicago                            18          10
  Yes   Atlanta                            208         11
   No   Minnetonka                          57        Failed
  Yes   Hopkins                           112         80
   No   Marietta                          2018        Failed
> perl -lane ' print join(" ",@F,"Time_Diff") if $.==1; if($.>1 ) { $F[4]=$F[2]-$F[3] if not $F[3]=~/Failed/; print join(" ",@F) } ' kwa.in | column -t
Done  City        Start_time  End_time  Time_Diff
Yes   Chicago     18          10        8
Yes   Atlanta     208         11        197
No    Minnetonka  57          Failed
Yes   Hopkins     112         80        32
No    Marietta    2018        Failed
>