Looping through the content of a file in Bash

后端 未结 13 2371
傲寒
傲寒 2020-11-21 10:08

How do I iterate through each line of a text file with Bash?

With this script:

echo \"Start!\"
for p in (peptides.txt)
do
    echo \"${p}\"
done
         


        
13条回答
  •  礼貌的吻别
    2020-11-21 10:30

    cat peptides.txt | while read line 
    do
       # do something with $line here
    done
    

    and the one-liner variant:

    cat peptides.txt | while read line; do something_with_$line_here; done
    

    These options will skip the last line of the file if there is no trailing line feed.

    You can avoid this by the following:

    cat peptides.txt | while read line || [[ -n $line ]];
    do
       # do something with $line here
    done
    

提交回复
热议问题