28 ms·
$ join -v 2 <(ls | grep _A | sort | cut -c-4) <(ls | grep -v _A | sort | cut -c-4) The shortest one I could come up with, no need to use python. `join -v 2` s
by maratc 8y ago
$ join -v 2 <(ls | grep _A | sort | cut -c-4) <(ls | grep -v _A | sort | cut -c-4)
The shortest one I could come up with, no need to use python.
`join -v 2` shows the entries in the second sorted stream that don't have match in the first sorted stream, the rest is self-explanatory I hope.
Edit:
$ join -v2 -t_ -j1 <(ls | grep _A | sort ) <(ls | grep -v _A | sort)
Is even shorter, it takes first field (-j1) where fields are separated by '_' (-t_)
- v3gas 8y agoNice golf!
- zimpenfish 8y agoSlightly shorter: ls -v|cut -d_ -f1|uniq -c|awk '$1<2{print $2}' Tested by creating 500 sets of dual files and removing 10 `_A` randomly. for i in $(seq 1 500); do j=$(printf %04d $i); touch ${j}_data.csv; touch ${j}_A.csv; done for i in $(seq 1 10); do q=$((RANDOM % 500)); r=$(printf %04d $q); rm -v ${r}_A.csv; done removed '0438_A.csv' removed '0327_A.csv' removed '0150_A.csv' removed '0173_A.csv' removed '0460_A.csv' removed '0194_A.csv' removed '0073_A.csv' removed '0293_A.csv' removed '0404_A.csv' removed '0153_A.csv' And then using the code above to verify the missing files 0073 0150 0153 0173 0194 0293 0327 0404 0438 0460
- maratc 8y agoElegant and short! But unless I'm missing something, your script will print even the datasets that have _A but not the corresponding _data?
- zimpenfish 8y agoA fair point but I was working within the context of the original problem which seemed to be "there's always a _data but not always an _A". If I was trying to provide a robust generic solution, I wouldn't be code golfing it...
- taviso 8y agoYou could use uniq -u to avoid the awk.
- zimpenfish 8y agoPerfect, ta.
- maratc 8y agols|cut -d_ -f1|uniq -u You win.
- e12e 8y agoI like this solution - I'm not very used to using "cut" - or more generally to map from "files" to "fields/lines in a text stream". I'm more inclined to ask: given a list of files with this name, does a file of a different name exist on the file system? But the more Unix approach is really: how can I model my data as a text stream, and how can I then pose/answer my question? (here: list all filenames in folder in sorted order - cut away the text indicating type - then count/display the non-repeat/single entries) My solution would probably be more like (with bash in mind, most of this could be "expanded" to fork out to more utils, like "basename -s" etc) : for data_file in *_data.csv do; alg_file="${data_file%_data.csv}_A.csv"; if [[ ! -f "${alg_file}" ]]; then echo "Missing alg file:\ ${alg_file} for data \ file: ${data_file}"; fi; done Ed: this is essentially the same solution as: https://news.ycombinator.com/item?id=19161358 https://news.ycombinator.com/item?id=19161358 Although more verbose. I think I prefer omitting the explicit if, though - and just using "test" and "or" ("[[", "||" ).
- mnaydin 8y agoThis can be more shortened to ls -1 | uniq -u -w4 using GNU uniq, for these special filenames. Unfortunately, Posix does not define -w option for uniq.