Skip to main content
Added more correct solution
Source Link
gnubeard
  • 319
  • 1
  • 5

By combining egrep (called with the -l option so that positive matches return only filenames) with xargs you can perform arbitrary commands based on text matches:

[gnubeard@mothership: ~/mver]$ ls
destination  example.fna  non_example.fna
[gnubeard@mothership: ~/mver]$ cat example.fna
>>>>>>>>>
[gnubeard@mothership: ~/mver]$ cat non_example.fna
<<<<<<<<<
>>
[gnubeard@mothership: ~/mver]$ egrep -l '>{9}' *.fna | xargs -I{} mv {} destination/
[gnubeard@mothership: ~/mver]$ ls
destination  non_example.fna
[gnubeard@mothership: ~/mver]$ ls destination/
example.fna

Edit: This doesn't match files with a certain number of instances of a character, if they're separated by other characters or newlines. I've created this short script that should operate as desired.

#! /usr/bin/env bash

DESTINATION=./destination
MATCH_CHAR='>'
MATCH_COUNT=9

for FILE in $1; do
  MATCHES=$(grep -o $MATCH_CHAR $FILE | wc -l)
  if [ $MATCHES -eq $MATCH_COUNT ]; then
    mv $FILE $DESTINATION
  fi
done

Example:

[gnubeard@mothership: ~/mver]$ ls
destination  example.fna  mver  non_example.fna
[gnubeard@mothership: ~/mver]$ cat example.fna
>>
>>foo>>bar>>>baz
[gnubeard@mothership: ~/mver]$ cat non_example.fna
<<<<<<<<<
>>
[gnubeard@mothership: ~/mver]$ ./mver *.fna
[gnubeard@mothership: ~/mver]$ ls destination/
example.fna
[gnubeard@mothership: ~/mver]$ ls
destination  mver  non_example.fna

By combining egrep (called with the -l option so that positive matches return only filenames) with xargs you can perform arbitrary commands based on text matches:

[gnubeard@mothership: ~/mver]$ ls
destination  example.fna  non_example.fna
[gnubeard@mothership: ~/mver]$ cat example.fna
>>>>>>>>>
[gnubeard@mothership: ~/mver]$ cat non_example.fna
<<<<<<<<<
>>
[gnubeard@mothership: ~/mver]$ egrep -l '>{9}' *.fna | xargs -I{} mv {} destination/
[gnubeard@mothership: ~/mver]$ ls
destination  non_example.fna
[gnubeard@mothership: ~/mver]$ ls destination/
example.fna

By combining egrep (called with the -l option so that positive matches return only filenames) with xargs you can perform arbitrary commands based on text matches:

[gnubeard@mothership: ~/mver]$ ls
destination  example.fna  non_example.fna
[gnubeard@mothership: ~/mver]$ cat example.fna
>>>>>>>>>
[gnubeard@mothership: ~/mver]$ cat non_example.fna
<<<<<<<<<
>>
[gnubeard@mothership: ~/mver]$ egrep -l '>{9}' *.fna | xargs -I{} mv {} destination/
[gnubeard@mothership: ~/mver]$ ls
destination  non_example.fna
[gnubeard@mothership: ~/mver]$ ls destination/
example.fna

Edit: This doesn't match files with a certain number of instances of a character, if they're separated by other characters or newlines. I've created this short script that should operate as desired.

#! /usr/bin/env bash

DESTINATION=./destination
MATCH_CHAR='>'
MATCH_COUNT=9

for FILE in $1; do
  MATCHES=$(grep -o $MATCH_CHAR $FILE | wc -l)
  if [ $MATCHES -eq $MATCH_COUNT ]; then
    mv $FILE $DESTINATION
  fi
done

Example:

[gnubeard@mothership: ~/mver]$ ls
destination  example.fna  mver  non_example.fna
[gnubeard@mothership: ~/mver]$ cat example.fna
>>
>>foo>>bar>>>baz
[gnubeard@mothership: ~/mver]$ cat non_example.fna
<<<<<<<<<
>>
[gnubeard@mothership: ~/mver]$ ./mver *.fna
[gnubeard@mothership: ~/mver]$ ls destination/
example.fna
[gnubeard@mothership: ~/mver]$ ls
destination  mver  non_example.fna
Source Link
gnubeard
  • 319
  • 1
  • 5

By combining egrep (called with the -l option so that positive matches return only filenames) with xargs you can perform arbitrary commands based on text matches:

[gnubeard@mothership: ~/mver]$ ls
destination  example.fna  non_example.fna
[gnubeard@mothership: ~/mver]$ cat example.fna
>>>>>>>>>
[gnubeard@mothership: ~/mver]$ cat non_example.fna
<<<<<<<<<
>>
[gnubeard@mothership: ~/mver]$ egrep -l '>{9}' *.fna | xargs -I{} mv {} destination/
[gnubeard@mothership: ~/mver]$ ls
destination  non_example.fna
[gnubeard@mothership: ~/mver]$ ls destination/
example.fna