Find first 5 characters in the file name

Hi,
I have some files in a folder.
I need to find first 5 characters in the file name of every file and write those into a file.

Example:
ABC126_ACCPT.txt
AYA127_ACCPT.txt

Output in the file:
ABC126
AYA127

ls *\_*.txt | while read file;do echo $file | cut -d_ -f1 >> output.txt;done

cat output.txt
ABC126
AYA127

Firstly there are 6 characters as shown, not 5 as described...
Secondly what have you tried?
Thirdly simple pseudo_code...

create a new file of say /tmp/filenames.lst
change the drawer to place where *.txt files are situated
ls *.txt > /tmp/listing
[optional]cat < /tmp/listing [as a visible check.]
read in the /tmp/listing to a var
create an array from var
use a loop derived from the number of array elements<and>
from each line of the array extract the first 6 characters
echo <each_set_of_6_characters> and append to /tmp/filenames.lst

If available:

ls|colrm 6
2 Likes