cat files with at least 2 specified components in file name

I have file names that looks like this:

act-art-atr-phm-pro-psy act-art-atr-phm-pro-sta act-art-atr-pro-psy-rel-sta act-art-atr-pro-tme act-art-atr-psy act-art-atr-psy-qud-sta act-art-atr-psy-sta act-art-atr-rel act-art-atr-sta act-art-com 

I want to cat files that contain at least two specific words (separated by "-") that I manually indicate.

For instance: I want to cat all files that contain at least "act" and "psy", thus the resulting file would contain all of the contents from the files

act-art-atr-phm-pro-psy act-art-atr-pro-psy-rel-sta act-art-atr-psy act-art-atr-psy-qud-sta act-art-atr-psy-sta 

I am not sure of a straight-forward regular expression that would be able to do this, as the order of the file names is unique for each files.

Is there a particular regex that can search for files containing at least 2 of the three-lettered names separated by "-" that I can use to cat all of the corresponding files?

Or even a more efficient way to handle this task, if my proposed regex +cat strategy is not the best.

2 Answers

It seems that the filenames contain the terms in alphabetical order. So you could say

cat `ls -1 | grep 'act.*psy'` 

or

cat $(ls -1 | grep 'act.*psy') 
1

If you want to get the file names which are separated by - with your words:

ls -1 | grep 'word1-word2' 

If you want to print all the contents of those files:

cat $(ls -1 | grep 'word1-word2' ) 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like