while loop:

One way to do it is:

while read p; do
echo "$p"
done <peptides.txt

As pointed out in the comments, this has the side effects of trimming leading whitespace, interpretting backslash sequences, and skipping the trailing line if it’s missing a terminating linefeed. If these are concerns, you can do:

while IFS="" read -r p || [ -n "$p" ]
do
printf '%s\n' "$p"
done < peptides.txt

Exceptionally, if the loop body may read from standard input, you can open the file using a different file descriptor:

while read -u 10 p; do
...
done 10<peptides.txt

Here, 10 is just an arbitrary number (different from 0, 1, 2).

src: stackoverflow.com

for loop:

iterate over all lines of a file with the for loop: with an ever incrementing counter

vim /scripts/loop_over_file.sh
#!/bin/bash
LINES_COUNTED=$(cat $1 | wc -l)

for ((i = 0 ; i < $LINES_COUNTED ; i++)); do
   # some verbose debug output:
   # echo " currently processing line number" $i of file $1
   # print line number i
   awk "NR==$i" $1
   # alternative:
   # sed '10!d' $1
done

# after
chmod +x /scripts/*.sh

# call script like this
/scripts/loop_over_file.sh /path/to/filename.txt

liked this article?

  • only together we can create a truly free world
  • plz support dwaves to keep it up & running!
  • (yes the info on the internet is (mostly) free but beer is still not free (still have to work on that))
  • really really hate advertisement
  • contribute: whenever a solution was found, blog about it for others to find!
  • talk about, recommend & link to this blog and articles
  • thanks to all who contribute!
admin