-1

I want to print the table only if non-empty latest column that starts with the word "Failed"on a CSV file in Linux..

My input -

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,,

My output should be:

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

closed as unclear what you're asking by jasonwryan, slm Aug 28 '14 at 2:38

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Would be better if you just could format your question neat. – Keyshov Borate Aug 26 '14 at 6:04
  • Is this a homework problem? – mdpc Aug 26 '14 at 6:05
  • Both the second row and the final two in your example would qualify. – jasonwryan Aug 26 '14 at 6:17
  • 1
    You don't indicate 'Successfull Fullbackup" and "Failed Fullbackup" are the only possible entries after the first column. CSV files in general can have multi-line cell values, making parsing with grep/awk cumbersome if not impossible. You seem to be presenting a TSV file, but you should use a CSV, as we cannot see the tabs which are gobbled by the system, thus not see for sure how many cells any of the lines might have. – Anthon Aug 26 '14 at 7:10
  • 2
    So now the field delimiters are commas and spaces (and a semicolon)? This is not helpful: voting to close... – jasonwryan Aug 26 '14 at 7:43
3
sed -e 1b -e $'/[\t,]Failed[^\t,]*[\t,]*$/!d'
  • Hi Stephen, "Successfull Fullbackup; Failed Fullbackup" this entire is a cell at 2nd row 2nd column, i want to skip this also. on a whole i need to print only rows that has latest non empty cell that starts with the word "Failed" I will change question. Thanks in advance. – user81341 Aug 26 '14 at 6:42
  • Also for your information "," or tab would be the delimiter for each cell in a row. – user81341 Aug 26 '14 at 6:47
  • Am not getting the proper output when ran on main file. Please find the new i/p and o/p tagged in the question. – user81341 Aug 26 '14 at 7:22
  • @user81341, works for me on your sample. – Stéphane Chazelas Aug 26 '14 at 9:10
1

I don't really know what all this is about, but those were some long lines - it was hard to interpret for me. So, I did this:

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

OUTPUT

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,,

I don't know if it answers your question or not, but it sure makes it a lot easier for me to read. Report cards I understand. And by the way, someone should really have a look at that Mac. So much potential...

  • Sorry this A was flagged as not an A. I now see you did A it with the sed at the top. My bad 8-) – slm Aug 27 '14 at 16:54
0

If you can use 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
0

A couple of variations on Stéphane’s answer:

(line; grep $'[\t,]Failed[^\t,]*[\t,]*$') < input

The (line; grep …) construct comes in very handy; for example, for filtering the output from ps.  If your system doesn’t have the line command,

(read x; echo "$x"; grep $'[\t,]Failed[^\t,]*[\t,]*$') < input

should work, too.  Alternatively,

grep -E $'^Name,Mon,Tue,Wed,Thu,Fri,Mon$|[\t,]Failed[^\t,]*[\t,]*$' input
  • Cat file | awk -F"," '{for ( x=NF ; x>1 ; x-- ) { if ( $x ~ /^Failed/ ) { x = 0 ; print } ; if ( $x ~ /^Successful/ ) x = 0 } }' ........ This worked for me...... thanks everyone. – user81341 Aug 27 '14 at 3:02
  • This does not, strictly speaking, answer the question that you asked, because you never said that every field (other than the first row and the first column) begins with either “Failed” or “Successful” – you said (in effect) that every field is blank, begins with “Failed”, or is something else. For instance, according to what you said about your data, Cray,Failed,Failed,,,,OK would be a valid line that should not be output, but your solution (above) would print it. We don’t expect you to post all your real data; we ask you to describe your problem and post a representative example. – G-Man Aug 27 '14 at 16:51
  • But also, you almost never need to say cat file | (command). This is known as a “useless use of cat”, which is even discussed on Wikipedia. With awk, as with most other Unix & Linux commands, you can say awk (awk_options) '(awk_script)' file or awk < file (awk_options) '(awk_script)'. – G-Man Aug 27 '14 at 16:52
  • That said, if you believe that you have an answer to your question, you should post it as an answer, not as a comment, and especially not on a comment on somebody else’s answer that is totally different from your answer. And, if you provide a few words of explanation, people might even vote for it and give you some reputation. – G-Man Aug 27 '14 at 16:54
-1
$ cat file | awk -F"," \
    '{for ( x=NF ; x>1 ; x-- ) { if ( $x ~ /^Failed/ ) { x = 0 ; print } ; \
    if ( $x ~ /^Successful/ ) x = 0 } }'

This worked for me.

  • Can you please explain what this is doing? – slm Aug 28 '14 at 2:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.