仅打印 Linux 中 CSV 文件上最新的非空单元格以“失败”一词开头的行

仅打印 Linux 中 CSV 文件上最新的非空单元格以“失败”一词开头的行

我想仅在 Linux 中的 CSV 文件上以“失败”一词开头的非空最新列时打印该表。

我的意见 -

Name,Mon,Tue,Wed,Thu,Fri,Mon
Linux,Successfull Fullbackup; Failed Fullbackup,,,,,Failed Fullbackup
Unix,,,,Failed Fullbackup,,
Windows,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Successfull Fullbackup
MAC,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup
Solaris,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Successfull Fullbackup; Failed Fullbackup,,

我的输出应该是:

Name,Mon,Tue,Wed,Thu,Fri,Mon
Linux,Successfull Fullbackup; Failed Fullbackup,,,,,Failed Fullbackup
Unix,,,,Failed Fullbackup,,
MAC,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup

答案1

sed -e 1b -e $'/[\t,]Failed[^\t,]*[\t,]*$/!d'

答案2

我真的不知道这一切是怎么回事,但那是一些很长的线——对我来说很难解释。所以,我这样做了:

sed 's/,S[^,]*/,A/g;s/,Fa[^,]*/,F/g
' <<\DATA
Name,Mon,Tue,Wed,Thu,Fri,Mon
Linux,Successfull Fullbackup; Failed Fullbackup,,,,,Failed Fullbackup
Unix,,,,Failed Fullbackup,,
Windows,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Successfull Fullbackup
MAC,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup
Solaris,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Successfull Fullbackup; Failed Fullbackup,,
DATA

输出

Name,Mon,Tue,Wed,Thu,Fri,Mon
Linux,A,,,,,F
Unix,,,,F,,
Windows,F,F,F,F,F,A
MAC,F,F,F,F,F,F
Solaris,F,F,F,A,,

我不知道它是否回答了你的问题,但它确实让我更容易阅读。报告卡我明白。顺便说一句,真应该有人看看那台 Mac。潜力如此之大...

答案3

如果你可以使用perl

$ perl -F',' -anle 'print if $. == 1 or (grep {$_} @F)[-1] =~ /^Failed/' file
Name,Mon,Tue,Wed,Thu,Fri,Mon
Linux,Successfull Fullbackup; Failed Fullbackup,,,,,Failed Fullbackup
Unix,,,,Failed Fullbackup,,
MAC,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup

答案4

$ cat file | awk -F"," \
    '{for ( x=NF ; x>1 ; x-- ) { if ( $x ~ /^Failed/ ) { x = 0 ; print } ; \
    if ( $x ~ /^Successful/ ) x = 0 } }'

这对我有用。

相关内容