BASH: Curly Brace Wizardry (Multiple Word Matching)

Single character/range pattern matching is great but we can do the same with strings. This way, there’s no need to repeat the entire length of the filename if a section has the same name. Check out the examples below using the {curly brace}.

Create files prefixed with names

$ touch {james,ken,ryu,vega,bison}_files.txt; ls -l
total 0
-rw-r--r-- 1 jvalero wheel 0 Jun  5 06:42 bison_files.txt
-rw-r--r-- 1 jvalero wheel 0 Jun  5 06:42 james_files.txt
-rw-r--r-- 1 jvalero wheel 0 Jun  5 06:42 ken_files.txt
-rw-r--r-- 1 jvalero wheel 0 Jun  5 06:42 ryu_files.txt
-rw-r--r-- 1 jvalero wheel 0 Jun  5 06:42 vega_files.txt

 Grep through files

$ echo bad > bison_files.txt ; echo good > ken_files.txt ; echo good > ryu_files.txt

$ grep good {bison,ken,ryu}_files.txt
ken_files.txt:good
ryu_files.txt:good

 

Leave a Reply

Your email address will not be published. Required fields are marked *