Search This Blog

shell script: read lines from file in a while loop

  • by piping cat:
    cat /path/to/file | while read line
    do
        echo $line
    done
    
  • by redirecting the file:
    while read line
    do
        echo $line
    done < /path/to/file
    
  • by using awk:
    awk '{print $0}' /path/to/file
    
  • by using head an tail:
    f=/path/to/file
    total=$(wc -l $f)
    n=0
    while [ $n -lt $total ]
    do
        let n++
        line=$(head -n $n $f | tail -1)
        echo ${line}
    done
    

No comments:

Post a Comment