2

I'm trying to get the filename from unrar command verbose and deleting them all through command line.

The unrar command output is similar to this:

Extracting  BucketSort.cpp                                            OK 
Extracting  BucketSort.vcxproj                                        OK 
Extracting  BucketSort.vcxproj.filters                                OK 
Extracting  BucketSort.lastbuildstate                                 OK 
Extracting  BucketSort.log                                            OK 
Extracting  BucketSort.obj                                            OK 
Extracting  BucketSort.pch                                            OK 

So I came up with this awk command so far:

unrar e BucketSort.rar | awk '{if (match($0,/Extracting\s*([\w|.]+)\s*OK/,m)) print m[0]}'

but nothing is printed. When I leave just the word "Extracting" to match I can see "Extracting" for all the extracting files. So it's not recognizing something written in regex but I don't know what it is. Do you know what's wrong with the expression?

PS: You can check the regex expression here.

  • It looks like you're used to Perl, actually. In awk you need to backslash-escape a lot more of the "special" bits of regexes, including parentheses, and (I believe) + as well. – Wildcard Feb 11 '16 at 20:01
  • 1
    I think GNU awk (which seems to be what's implied here, given the array argument to match) supports the perl-like \s and \w classes, however it's not clear to me how [\w|.] should be interpreted. Regardless, it seems to be a heavy-handed way to split what appear to be simple whitespace delimited fields - why not just do $1=="Extracting" && $3=="OK" – steeldriver Feb 11 '16 at 20:59
  • 1
    gawk doesn't seem to like \w inside brackets (although I can't find documentation to support this claim). Both of these provide the output you seek: (\S+) and ([[:alnum:]_.]+). references gnu.org/software/gawk/manual/html_node/… and gnu.org/software/gawk/manual/html_node/Bracket-Expressions.html – glenn jackman Feb 11 '16 at 22:20
2

This should do the trick:

unrar e BucketSort.rar | awk '/^Extracting/ {print $2}'
  • Thank you DopeGhoti. Your expression does work but do you know why the expression I wrote doesn't? I'm wondering just in case I need to check another kind of output that I need. – Rinaldi Segecin Feb 11 '16 at 20:23
  • I think it's because your regex checker settings on the site you linked were for perl-compatible regex, and awk uses a slightly different flavor in which, as a comment on your OP mentions, needs a lot more escaping of special characters. awk is more Cish than perlish. – DopeGhoti Feb 11 '16 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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