Looping through the content of a file in Bash

后端 未结 13 2362
傲寒
傲寒 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:46

    This is coming rather very late, but with the thought that it may help someone, i am adding the answer. Also this may not be the best way. head command can be used with -n argument to read n lines from start of file and likewise tail command can be used to read from bottom. Now, to fetch nth line from file, we head n lines, pipe the data to tail only 1 line from the piped data.

       TOTAL_LINES=`wc -l $USER_FILE | cut -d " " -f1 `
       echo $TOTAL_LINES       # To validate total lines in the file
    
       for (( i=1 ; i <= $TOTAL_LINES; i++ ))
       do
          LINE=`head -n$i $USER_FILE | tail -n1`
          echo $LINE
       done
    

提交回复
热议问题