I'm trying to filter a file path in a script with awk so it omits anything within a regular expression:

FILTERED_FILE=$(echo $File |  awk -F / "{(for(i=NF; i>=0; i--) {if $(i!~^(NewConfig|Old|old|backup|Backup|Servers)$ print $i}}") ;

What I'm basically trying to do iterate field by field and extract the first field in the filepath that does not contain any of these keywords. So, if the file path is, for example:


I want the variable $FILTERED_FILE to be blah.

This exact line worked as expected in a different script, but now, the awk variable ($i) remains empty. I'm very much a beginner with awk, so I'm certain to be missing something - I'd be grateful for advice on where to look. I don't think this line is dependent on any other line in the script (again, this worked in a previous script from which I adapted this one), but I'll upload more if necessary. I'm not commited to awk either - if you can think of a better tool (grep, sed, etc) it's just as good.

Thank you!


I think there are several issues with your shell as well as your awk code.

First, always place the awk code in single quotes, as in awk '<commands>'. Otherwiese, strange effects may happen such as the shell interpreting $<...> statements.

Second, your syntax looks wrong at several places:

  • You say are looking for the first field that doesn't match, but seem to be looking for the last one? However, your code would print all other non-matching fields, too, since you don't break out of the loop after the first match was found.
  • The RegExp comparison should look like
    if ($i !~ /^( .... )$/) { ... }
  • In your example code, the closing bracket of the if statement is missing.

Third, you should also double-quote command substitutions in your shell scripts.

The revised awk code would be

awk -F '/' '{for (i=NF; i>0; i--) {if ($i !~ /^(NewConfig|[Oo]ld|[Bb]ackup|Servers)$/) {print $i; exit}}}'

And to call it from within a script, use

Filtered_File="$( echo "$File" | awk -F '/' ' .... ')"

Note that this last part will really only work inside a script because of the !~ construct. When done on the command-line, at least bash will try to perform command history expansion before starting the sub-shell used for the command-substitution, which leads to an error message.

  • Hi, previous comment was mistaken. Everything is working beautifully. Thank you!
    – dkd6
    Jul 15 '20 at 9:56

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.