for filename in *.cluster
class=$(cut -d$'\t' -f1) # Part 1
if [ $(wc -l "$filename") -eq 2 ] # Part 2, start
fi # Part 2, end
printf '%s\n' "$filename" >> class_"$class".txt # Part 3
This has three parts:
By default, it classifies the file based on the first field of the only line: the
class variable is set to whatever is in the file, up to the first tab character on each line. This will be either
3 for class 2 & 3, since those files have only one line.
cut chops files up by delimiters,
$'\t' is a Bash way of writing a tab character, and
cut to output only the first delimited field.
- If the file has two lines (
$(wc -l "$filename") -eq 2), it must be class 1, so the
class variable is forcibly set to 1, replacing its value from step 1. The
fi deals with this.
- Finally, the filename is appended to the appropriate class file:
printf '%s\n' "$filename" >> class_"$class".txt
At the end you will have three files
class_N.txt for each N in 1, 2, 3, with one filename per line. If any file has some other contents than what you outlined in the question, like a different first field or length, you will get extra class files created.
In the perverse case where a filename itself contains a newline character, this will fall apart (and give you an opportunity to reconsider your filename choices), but otherwise it should be fine.