bash下逐行讀取一個文件

在 Linux 或類 UNIX 系統(tǒng)下如何使用 KSH 或 BASH shell 逐行讀取一個文件?

在 Linux勺像、OSX、 *BSD 或者類 Unix 系統(tǒng)下你可以使用 ??while..do..done 的 bash 循環(huán)來逐行讀取一個文件涡贱。

在 Bash Unix 或者 Linux shell 中逐行讀取一個文件的語法

對于 bash咏删、ksh惹想、 zsh 和其他的 shells 語法如下

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

通過 -r 選項傳遞給 read 命令以防止阻止解釋其中的反斜杠轉(zhuǎn)義符问词。

在 read 命令之前添加 IFS= 選項,來防止首尾的空白字符被去掉嘀粱。

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

這是更適合人類閱讀的語法:

#!/bin/bash

input="/path/to/txt/file"

while IFS= read -r var

do

echo "$var"

done < "$input"

示例

下面是一些例子:

#!/bin/ksh

file="/home/vivek/data.txt"

while IFS= read line

do

# display $line or do somthing with $line

echo "$line"

done <"$file"

在 bash shell 中相同的例子:

#!/bin/bash

file="/home/vivek/data.txt"

while IFS= read -r line

do

# display $line or do somthing with $line

printf '%s/n' "$line"

done <"$file"

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

#!/bin/bash

file="/etc/passwd"

while IFS=: read -r f1 f2 f3 f4 f5 f6 f7

do

# display fields using f1, f2,..,f7

printf 'Username: %s, Shell: %s, Home Dir: %s/n' "$f1" "$f7" "$f6"

done <"$file"

示例輸出:

Bash 下如何逐行讀取一個文件

圖01:Bash 腳本:讀取文件并逐行輸出文件

Bash 腳本:逐行讀取文本文件并創(chuàng)建為 pdf 文件

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

4|http://www.cyberciti.biz/faq/mysql-user-creation/|Mysql User Creation: Setting Up a New MySQL User Account

4096|http://www.cyberciti.biz/faq/ksh-korn-shell/|What is UNIX / Linux Korn Shell?

4101|http://www.cyberciti.biz/faq/what-is-posix-shell/|What Is POSIX Shell?

17267|http://www.cyberciti.biz/faq/linux-check-battery-status/|Linux: Check Battery Status Command

17245|http://www.cyberciti.biz/faq/restarting-ntp-service-on-linux/|Linux Restart NTPD Service Command

17183|http://www.cyberciti.biz/faq/ubuntu-linux-determine-your-ip-address/|Ubuntu Linux: Determine Your IP Address

17172|http://www.cyberciti.biz/faq/determine-ip-address-of-linux-server/|HowTo: Determine an IP Address My Linux Server

16510|http://www.cyberciti.biz/faq/unix-linux-restart-php-service-command/|Linux / Unix: Restart PHP Service Command

8292|http://www.cyberciti.biz/faq/mounting-harddisks-in-freebsd-with-mount-command/|FreeBSD: Mount Hard Drive / Disk Command

8190|http://www.cyberciti.biz/faq/rebooting-solaris-unix-server/|Reboot a Solaris UNIX System

我的 bash 腳本:

#!/bin/bash

# Usage: Create pdf files from input (wrapper script)

# Author: Vivek Gite ?under GPL v2.x+

#———————————————————

#Input file

_db="/tmp/wordpress/faq.txt"

#Output location

o="/var/www/prviate/pdf/faq"

_writer="~/bin/py/pdfwriter.py"

# If file exists

if [[ -f "$_db" ]]

then

# read it

while IFS='|' read -r pdfid pdfurl pdftitle

do

local pdf="$o/$pdfid.pdf"

echo "Creating $pdf file …"

#Genrate pdf file

$_writer –quiet –footer-spacing 2 /

–footer-left "nixCraft is GIT UL++++ W+++ C++++ M+ e+++ d-" /

–footer-right "Page [page] of [toPage]" –footer-line /

–footer-font-size 7 –print-media-type "$pdfurl" "$pdf"

done <"$_db"

fi

技巧:從 bash 變量中讀取

讓我們看看如何在 Debian 或者 Ubuntu Linux 下列出所有安裝過的 php 包激挪,請輸入:

# 我將輸出內(nèi)容賦值到一個變量名為 $list中 #

list=$(dpkg –list php/* | awk '/ii/{print $2}')

printf '%s/n' "$list"

示例輸出:

php-pear

php5-cli

php5-common

php5-fpm

php5-gd

php5-json

php5-memcache

php5-mysql

php5-readline

php5-suhosin-extension

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

#!/bin/bash

# BASH can iterate over $list variable using a "here string" #

while IFS= read -r pkg

do

printf 'Installing php package %s…/n' "$pkg"

/usr/bin/apt-get -qq install $pkg

done <<< "$list"

printf '*** Do not forget to run php5enmod and restart the server (httpd or php5-fpm) ***/n'

示例輸出:

Installing php package php-pear…

Installing php package php5-cli…

Installing php package php5-common…

Installing php package php5-fpm…

Installing php package php5-gd…

Installing php package php5-json…

Installing php package php5-memcache…

Installing php package php5-mysql…

Installing php package php5-readline…

Installing php package php5-suhosin-extension…

*** Do not forget to run php5enmod and restart the server (httpd or php5-fpm)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末锋叨,一起剝皮案震驚了整個濱河市垄分,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌娃磺,老刑警劉巖薄湿,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡豺瘤,警方通過查閱死者的電腦和手機(jī)吆倦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來坐求,“玉大人蚕泽,你說我怎么就攤上這事∏培停” “怎么了须妻?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長泛领。 經(jīng)常有香客問我荒吏,道長,這世上最難降的妖魔是什么渊鞋? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任司倚,我火速辦了婚禮,結(jié)果婚禮上篓像,老公的妹妹穿的比我還像新娘动知。我一直安慰自己,他們只是感情好员辩,可當(dāng)我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布盒粮。 她就那樣靜靜地躺著,像睡著了一般奠滑。 火紅的嫁衣襯著肌膚如雪丹皱。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天宋税,我揣著相機(jī)與錄音摊崭,去河邊找鬼。 笑死杰赛,一個胖子當(dāng)著我的面吹牛呢簸,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播乏屯,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼根时,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了辰晕?” 一聲冷哼從身側(cè)響起蛤迎,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎含友,沒想到半個月后替裆,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體校辩,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年辆童,在試婚紗的時候發(fā)現(xiàn)自己被綠了召川。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡胸遇,死狀恐怖荧呐,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情纸镊,我是刑警寧澤倍阐,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站逗威,受9級特大地震影響峰搪,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜凯旭,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一概耻、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧罐呼,春花似錦鞠柄、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至计螺,卻和暖如春夯尽,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背登馒。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工匙握, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人陈轿。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓圈纺,卻偏偏與公主長得像,于是被迫代替她去往敵國和親济欢。 傳聞我的和親對象是個殘疾皇子赠堵,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容