天天看點

Bash 下如何逐行讀取一個檔案

在 linux 或類 unix 系統下如何使用 ksh 或 bash shell 逐行讀取一個檔案?

在 linux、osx、 *bsd 或者類 unix 系統下你可以使用 ​​while..do..done 的 bash 循環來逐行讀取一個檔案。

<a target="_blank"></a>

對于 bash、ksh、 zsh 和其他的 shells 文法如下

<code>while read -r line; do command; done &lt; input.file</code>

通過 -r 選項傳遞給 read 指令以防止阻止解釋其中的反斜杠轉義符。

在 read 指令之前添加 <code>ifs=</code> 選項,來防止首尾的空白字元被去掉。

<code>while ifs= read -r line; do command_on $line; done &lt; input.file</code>

這是更适合人類閱讀的文法:

<code>#!/bin/bash</code>

<code>input="/path/to/txt/file"</code>

<code>while ifs= read -r var</code>

<code>do</code>

<code>echo "$var"</code>

<code>done &lt; "$input"</code>

示例

下面是一些例子:

<code>#!/bin/ksh</code>

<code>file="/home/vivek/data.txt"</code>

<code>while ifs= read line</code>

<code># display $line or do somthing with $line</code>

<code>echo "$line"</code>

<code>done &lt;"$file"</code>

在 bash shell 中相同的例子:

<code>while ifs= read -r line</code>

<code>printf '%s\n' "$line"</code>

你還可以看看這個更好的:

<code>file="/etc/passwd"</code>

<code>while ifs=: read -r f1 f2 f3 f4 f5 f6 f7</code>

<code># display fields using f1, f2,..,f7</code>

<code>printf 'username: %s, shell: %s, home dir: %s\n' "$f1" "$f7" "$f6"</code>

示例輸出:

Bash 下如何逐行讀取一個檔案

圖01:bash 腳本:讀取檔案并逐行輸出檔案

我的輸入檔案如下(faq.txt):

<code>4|http://www.cyberciti.biz/faq/mysql-user-creation/|mysql user creation: setting up a new mysql user account</code>

<code>4096|http://www.cyberciti.biz/faq/ksh-korn-shell/|what is unix / linux korn shell?</code>

<code>4101|http://www.cyberciti.biz/faq/what-is-posix-shell/|what is posix shell?</code>

<code>17267|http://www.cyberciti.biz/faq/linux-check-battery-status/|linux: check battery status command</code>

<code>17245|http://www.cyberciti.biz/faq/restarting-ntp-service-on-linux/|linux restart ntpd service command</code>

<code>17183|http://www.cyberciti.biz/faq/ubuntu-linux-determine-your-ip-address/|ubuntu linux: determine your ip address</code>

<code>17172|http://www.cyberciti.biz/faq/determine-ip-address-of-linux-server/|howto: determine an ip address my linux server</code>

<code>16510|http://www.cyberciti.biz/faq/unix-linux-restart-php-service-command/|linux / unix: restart php service command</code>

<code>8292|http://www.cyberciti.biz/faq/mounting-harddisks-in-freebsd-with-mount-command/|freebsd: mount hard drive / disk command</code>

<code>8190|http://www.cyberciti.biz/faq/rebooting-solaris-unix-server/|reboot a solaris unix system</code>

我的 bash 腳本:

<code># usage: create pdf files from input (wrapper script)</code>

<code># author: vivek gite &lt;www.cyberciti.biz&gt; under gpl v2.x+</code>

<code>#---------------------------------------------------------</code>

<code></code>

<code>#input file</code>

<code>_db="/tmp/wordpress/faq.txt"</code>

<code>#output location</code>

<code>o="/var/www/prviate/pdf/faq"</code>

<code>_writer="~/bin/py/pdfwriter.py"</code>

<code># if file exists</code>

<code>if [[ -f "$_db" ]]</code>

<code>then</code>

<code># read it</code>

<code>while ifs='|' read -r pdfid pdfurl pdftitle</code>

<code>local pdf="$o/$pdfid.pdf"</code>

<code>echo "creating $pdf file ..."</code>

<code>#genrate pdf file</code>

<code>$_writer --quiet --footer-spacing 2 \</code>

<code>--footer-left "nixcraft is git ul++++ w+++ c++++ m+ e+++ d-" \</code>

<code>--footer-right "page [page] of [topage]" --footer-line \</code>

<code>--footer-font-size 7 --print-media-type "$pdfurl" "$pdf"</code>

<code>done &lt;"$_db"</code>

<code>fi</code>

讓我們看看如何在 debian 或者 ubuntu linux 下列出所有安裝過的 php 包,請輸入:

<code># 我将輸出内容指派到一個變量名為 $list中 #</code>

<code>list=$(dpkg --list php\* | awk '/ii/{print $2}')</code>

<code>printf '%s\n' "$list"</code>

<code>php-pear</code>

<code>php5-cli</code>

<code>php5-common</code>

<code>php5-fpm</code>

<code>php5-gd</code>

<code>php5-json</code>

<code>php5-memcache</code>

<code>php5-mysql</code>

<code>php5-readline</code>

<code>php5-suhosin-extension</code>

你現在可以從 $list 中看到它們,并安裝這些包:

<code># bash can iterate over $list variable using a "here string" #</code>

<code>while ifs= read -r pkg</code>

<code>printf 'installing php package %s...\n' "$pkg"</code>

<code>/usr/bin/apt-get -qq install $pkg</code>

<code>done &lt;&lt;&lt; "$list"</code>

<code>printf '*** do not forget to run php5enmod and restart the server (httpd or php5-fpm) ***\n'</code>

<code>installing php package php-pear...</code>

<code>installing php package php5-cli...</code>

<code>installing php package php5-common...</code>

<code>installing php package php5-fpm...</code>

<code>installing php package php5-gd...</code>

<code>installing php package php5-json...</code>

<code>installing php package php5-memcache...</code>

<code>installing php package php5-mysql...</code>

<code>installing php package php5-readline...</code>

<code>installing php package php5-suhosin-extension...</code>

<code>*** do not forget to run php5enmod and restart the server (httpd or php5-fpm) ***</code>

<code>本文來自雲栖社群合作夥伴“linux中國”,原文釋出日期:2015-08-31</code>