Question

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.

Was it helpful?

Solution

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')

OTHER TIPS

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' )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top