Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Last active December 15, 2015 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joyrexus/5322129 to your computer and use it in GitHub Desktop.
Save joyrexus/5322129 to your computer and use it in GitHub Desktop.
Move files with specified pattern in first line to target dir.

Sift files by header

Suppose you have a set of files you want to partition on the basis of the first line in the file.

This script enables you to move a filtered subset (based on the header line) to a target directory.

You specify the pattern in the first line of the files you wish to move. Those files containing the pattern in their header get moved, those without don't.

Usage

head_move.sh PATTERN FILES... DIR

Example

/surveys
    a.tsv
    b.tsv
    c.tsv
    d.tsv
    .
    .
    .
    /standard
    /extended


head_move.sh "Q14" *.tsv extended
#!/bin/bash
# Move files with specified pattern in first line to target dir.
USAGE="Usage: $0 PATTERN FILES... DIR"
if [ "$#" == "0" ]; then
echo "$USAGE"
exit 1
fi
PATTERN=$1
TARGET_DIR=${!#}
shift
for i in "$@"
do
if [ -f $i ] && [ `perl -ne'print 1 if /'$PATTERN'/; close ARGV' $i` ]
then
echo "Moving $i to $TARGET_DIR"
mv $i $TARGET_DIR
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment