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}"

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值