Looping through lines in a file in bash, without using stdin

后端 未结 3 724
无人及你
无人及你 2021-01-05 06:01

I am foxed by the following situation.

I have a file list.txt that I want to run through line by line, in a loop, in bash. A typical line in list.txt has spaces in.

3条回答
  •  逝去的感伤
    2021-01-05 06:42

    I would probably count lines in a file and iterate each of those using eg. sed. It is also possible to read infinitely from stdin by changing while condition to: while true; and exit reading with ctrl+c.

    line=0 lines=$(sed -n '$=' in.file)
    while [ $line -lt $lines ]
    do
        let line++
        sed -n "${line}p" in.file
        echo "Hit enter for the next ${line} of ${lines}."
        read -s x
    done
    

    AWK is also great tool for this. Simple way to iterate through input would be like:

    awk '{ print  $0; printf "%s", "Hit enter for the next"; getline < "-" }' file
    

提交回复
热议问题