It is unclear what you mean with textfile in the first version of your question. To run it as program it can be a source file or a script. A source file must be compiled with a compiler like
gcc to get an executable binary program. With a script you need an interpreter.
I assume you mainly mean to execute a script. Executing it is only possible with an interpreter like bash, python, perl, awk, sed or any other known interpreter. If you want to make it executable you have to tell the operating system what interpreter it has to use. This is done with the first line in the script, a so called shebang. For a bash script you have to insert as first line
For a python script it could be
Similar lines are needed for other interpreter.
If you have added the shebang then you must make the script executable as you already have done with:
rpi ~$ sudo chmod a+x filename
rpi ~$ ls -l filename
-rwxr-xr-x 1 pi pi 0 Oct 22 18:26 filename
Notice the three
x in the modes. If you want to execute the script from the same directory you are in you have to prefix
./ to avoid unwanted execution:
rpi ~$ ./filename