Read file line by line on a Bash Unix & Linux shell

https://www.cyberciti.biz/faq/unix-howto-read-line-by-line-from-file/

How do I read a text file line by line under a Linux or UNIX-like system using KSH or BASH shell? How do I read a file line by line in bash script?

Syntax

The syntax is as follows for bash, ksh, zsh, and all other shells to read a file line by line

while IFS= read -r line
do
      command1 on $line
      command2 on $line
      ..
      ....
      commandN
done < "/path/to/filename"

or

while IFS= read -r field1 filed2 field3 ... fieldN
do
      command1 on $field1
      command2 on $field1 and $field3
      ..
      ....
      commandN on $field1 ... $fieldN
done < "/path/to dir/file name with space"

IFS is used to set field separator (default is while space). The -r option to read command disables backslash escaping (e.g., \n, \t). This is failsafe while read loop for reading text files.

How to Read a File Line By Line in Bash

Here is more human readable syntax for you:

#!/bin/bash
input="/path/to/txt/file"
while IFS= read -r line
do
  echo "$line"
done < "$input"

The input file ($input) is the name of the file you need use by the read command. The read command reads the file line by line, assigning each line to the $line bash shell variable. Once all lines are read from the file the bash while loop will stop. The internal field separator (IFS) is set to the empty string to preserve whitespace issues. This is a fail-safe feature.

How to use command/process substitution to read a file line by line

Process or command substitution means nothing more but to run a shell command and store its output to a variable or pass it to another command. The syntax is:

while IFS= read -r line
do
   ## take some action on $line
  echo "$line"
done < <(ps aux)

Using a here strings

Here strings is just like here documents:

## shell script to purge urls from Cloudflare ##
t="10"
I="/home/vivek/.data/tags.deleted.410"
url=""
while IFS= read -r line
do
url="$url $line"
done <<<"$(tail -${t} ${I})"
[ "$url" != "" ] && ~/bin/cloudflare.purge.urls.sh "$url"

How to file descriptor with read command

while IFS= read -r -u13 line
do 
   echo "$line"
done 13<"${input}"

參考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章