Bash 下如何逐行读取一个文件

在 Linux 或类 UNIX 系统下如何使用 KSH 或 BASH shell 逐行读取一个文件?

在 Linux、OSX、 *BSD 或者类 Unix 系统下你可以使用 ​​while..do..done 的 bash 循环来逐行读取一个文件。

 

在 Bash Unix 或者 Linux shell 中逐行读取一个文件的语法

对于 bash、ksh、 zsh 和其他的 shells 语法如下

  1. while read -r line;do COMMAND;done< input.file

通过 -r 选项传递给 read 命令以防止阻止解释其中的反斜杠转义符。

在 read 命令之前添加 IFS= 选项,来防止首尾的空白字符被去掉。

  1. while IFS= read -r line;do COMMAND_on $line;done< input.file

这是更适合人类阅读的语法:

  1. #!/bin/bash
  2. input="/path/to/txt/file"
  3. while IFS= read -r var
  4. do
  5. echo "$var"
  6. done<"$input"

示例

下面是一些例子:

  1. #!/bin/ksh
  2. file="/home/vivek/data.txt"
  3. while IFS= read line
  4. do
  5. # display $line or do somthing with $line
  6. echo "$line"
  7. done<"$file"

在 bash shell 中相同的例子:

  1. #!/bin/bash
  2. file="/home/vivek/data.txt"
  3. while IFS= read -r line
  4. do
  5. # display $line or do somthing with $line
  6. printf '%sn'"$line"
  7. done<"$file"

你还可以看看这个更好的:

  1. #!/bin/bash
  2. file="/etc/passwd"
  3. while IFS=: read -r f1 f2 f3 f4 f5 f6 f7
  4. do
  5. # display fields using f1, f2,..,f7
  6. printf 'Username: %s, Shell: %s, Home Dir: %sn'"$f1""$f7""$f6"
  7. done<"$file"

示例输出:

Fig.01: Bash shell scripting- read file line by line demo outputs

图01:Bash 脚本:读取文件并逐行输出文件

 

Bash 脚本:逐行读取文本文件并创建为 pdf 文件

我的输入文件如下(faq.txt):

  1. 4|http://www.cyberciti.biz/faq/mysql-user-creation/|Mysql User Creation: Setting Up a New MySQL User Account
  2. 4096|http://www.cyberciti.biz/faq/ksh-korn-shell/|What is UNIX / Linux Korn Shell?
  3. 4101|http://www.cyberciti.biz/faq/what-is-posix-shell/|What Is POSIX Shell?
  4. 17267|http://www.cyberciti.biz/faq/linux-check-battery-status/|Linux: Check Battery Status Command
  5. 17245|http://www.cyberciti.biz/faq/restarting-ntp-service-on-linux/|Linux Restart NTPD Service Command
  6. 17183|http://www.cyberciti.biz/faq/Ubuntu-linux-determine-your-ip-address/|Ubuntu Linux: Determine Your IP Address
  7. 17172|http://www.cyberciti.biz/faq/determine-ip-address-of-linux-server/|HowTo: Determine an IP Address My Linux Server
  8. 16510|http://www.cyberciti.biz/faq/unix-linux-restart-php-service-command/|Linux / Unix: Restart PHP Service Command
  9. 8292|http://www.cyberciti.biz/faq/mounting-harddisks-in-freebsd-with-mount-command/|FreeBSD: Mount Hard Drive / Disk Command
  10. 8190|http://www.cyberciti.biz/faq/rebooting-solaris-unix-server/|Reboot a Solaris UNIX System

我的 bash 脚本:

  1. #!/bin/bash
  2. # Usage: Create pdf files from input (wrapper script)
  3. # Author: Vivek Gite under GPL v2.x+
  4. #---------------------------------------------------------
  5. #Input file
  6. _db="/tmp/wordpress/faq.txt"
  7. #Output location
  8. o="/var/www/prviate/pdf/faq"
  9. _writer="~/bin/py/pdfwriter.py"
  10. # If file exists
  11. if[[-f "$_db"]]
  12. then
  13. # read it
  14. while IFS='|' read -r pdfid pdfurl pdftitle
  15. do
  16. local pdf="$o/$pdfid.pdf"
  17. echo "Creating $pdf file ..."
  18. #Genrate pdf file
  19. $_writer --quiet --footer-spacing 2
  20. --footer-left "nixCraft is GIT UL++++ W+++ C++++ M+ e+++ d-"
  21. --footer-right "Page [page] of [toPage]"--footer-line
  22. --footer-font-size 7--print-media-type "$pdfurl""$pdf"
  23. done<"$_db"
  24. fi

 

技巧:从 bash 变量中读取

让我们看看如何在 Debian 或者 Ubuntu Linux 下列出所有安装过的 php 包,请输入:

  1. # 我将输出内容赋值到一个变量名为 $list中 #
  2. list=$(dpkg --list php* | awk '/ii/{print $2}')
  3. printf '%sn'"$list"

示例输出:

  1. php-pear
  2. php5-cli
  3. php5-common
  4. php5-fpm
  5. php5-gd
  6. php5-json
  7. php5-memcache
  8. php5-mysql
  9. php5-readline
  10. php5-suhosin-extension

你现在可以从 $list 中看到它们,并安装这些包:

  1. #!/bin/bash
  2. # BASH can iterate over $list variable using a "here string" #
  3. while IFS= read -r pkg
  4. do
  5. printf 'Installing php package %s...n'"$pkg"
  6. /usr/bin/apt-get-qq install $pkg
  7. done<<<"$list"
  8. printf '*** Do not forget to run php5enmod and restart the server (httpd or php5-fpm) ***n'

示例输出:

  1. Installing php package php-pear...
  2. Installing php package php5-cli...
  3. Installing php package php5-common...
  4. Installing php package php5-fpm...
  5. Installing php package php5-gd...
  6. Installing php package php5-json...
  7. Installing php package php5-memcache...
  8. Installing php package php5-mysql...
  9. Installing php package php5-readline...
  10. Installing php package php5-suhosin-extension...
  11. ***Donot forget to run php5enmod and restart the server (httpd or php5-fpm)***

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

作者: VIVEK GIT 译者:strugglingyouth 校对:wxy

本文由 LCTT 原创翻译,Linux中国 荣誉推出