Merge pull request #3281 from strugglingyouth/master

翻译完成20150827 Linux or UNIX--Bash Read a File Line By Line.md
This commit is contained in:
joeren 2015-08-29 07:36:54 +08:00
commit 28af00eb8a

View File

@ -1,19 +1,19 @@
translation by strugglingyouth
Linux/UNIX: Bash Read a File Line By Line
Linux/UNIX: Bash 下如何逐行读取一个文件
================================================================================
How do I read a file line by line under a Linux or UNIX-like system using KSH or BASH shell?
在 Linux 或类 UNIX 系统下如何使用 KSH 或 BASH shell 逐行读取一个文件?
You can use while..do..done bash loop to read file line by line on a Linux, OSX, *BSD, or Unix-like system.
在 Linux, OSX, * BSD ,或者类 Unix 系统下你可以使用while..do..done bash 的循环来逐行读取一个文件。
**Syntax to read file line by line on a Bash Unix & Linux shell:**
**在 Bash Unix 或者 Linux shell 中逐行读取一个文件的语法:**
1. The syntax is as follows for bash, ksh, zsh, and all other shells -
1.对于 bash, ksh, zsh,和其他的 shells 语法如下 -
1. while read -r line; do COMMAND; done < input.file
1. The -r option passed to red command prevents backslash escapes from being interpreted.
1. Add IFS= option before read command to prevent leading/trailing whitespace from being trimmed -
1.通过 -r 选项传递给红色的命令阻止反斜杠被解释。
1.在 read 命令之前添加 IFS= option来防止 leading/trailing 尾随的空白字符被分割 -
1. while IFS= read -r line; do COMMAND_on $line; done < input.file
Here is more human readable syntax for you:
这是更适合人类阅读的语法:
#!/bin/bash
input="/path/to/txt/file"
@ -22,9 +22,9 @@ Here is more human readable syntax for you:
echo "$var"
done < "$input"
**Examples**
**示例**
Here are some examples:
下面是一些例子:
#!/bin/ksh
file="/home/vivek/data.txt"
@ -34,7 +34,7 @@ Here are some examples:
echo "$line"
done <"$file"
The same example using bash shell:
在 bash shell 中相同的例子:
#!/bin/bash
file="/home/vivek/data.txt"
@ -44,7 +44,7 @@ The same example using bash shell:
printf '%s\n' "$line"
done <"$file"
You can also read field wise:
你还可以看看这个更好的:
#!/bin/bash
file="/etc/passwd"
@ -54,15 +54,15 @@ You can also read field wise:
printf 'Username: %s, Shell: %s, Home Dir: %s\n' "$f1" "$f7" "$f6"
done <"$file"
Sample outputs:
示例输出:
![Fig.01: Bash shell scripting- read file line by line demo outputs](http://s0.cyberciti.org/uploads/faq/2011/01/Bash-Scripting-Read-File-line-by-line-demo.jpg)
Fig.01: Bash shell scripting- read file line by line demo outputs
图01Bash shell scripting- 读取文件并逐行输出文件
**Bash Scripting: Read text file line-by-line to create pdf files**
**Bash Scripting: 逐行读取文本文件并创建为 pdf 文件**
My input file is as follows (faq.txt):
我的输入文件如下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?
@ -75,7 +75,7 @@ My input file is as follows (faq.txt):
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
My bash script:
我的 bash script:
#!/bin/bash
# Usage: Create pdf files from input (wrapper script)
@ -106,15 +106,16 @@ My bash script:
done <"$_db"
fi
**Tip: Read from bash variable**
**提示:从 bash 的变量开始读取**
Let us say you want a list of all installed php packages on a Debian or Ubuntu Linux, enter:
让我们看看如何在 Debian 或者 Ubuntu Linux 下列出所有安装过的 php 包,请输入:
# 我将输出内容赋值到一个变量名为$list中 #
# My input source is the contents of a variable called $list #
list=$(dpkg --list php\* | awk '/ii/{print $2}')
printf '%s\n' "$list"
Sample outputs:
示例输出:
php-pear
php5-cli
@ -127,7 +128,7 @@ Sample outputs:
php5-readline
php5-suhosin-extension
You can now read from $list and install the package:
你现在可以从 $list 中看到安装的包:
#!/bin/bash
# BASH can iterate over $list variable using a "here string" #
@ -138,7 +139,7 @@ You can now read from $list and install the package:
done <<< "$list"
printf '*** Do not forget to run php5enmod and restart the server (httpd or php5-fpm) ***\n'
Sample outputs:
示例输出:
Installing php package php-pear...
Installing php package php5-cli...
@ -150,14 +151,16 @@ Sample outputs:
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) ***
*** 不要忘了运行php5enmod并重新启动服务httpd 或 php5-fpm ***
--------------------------------------------------------------------------------
via: http://www.cyberciti.biz/faq/unix-howto-read-line-by-line-from-file/
作者:[作者名][a]
译者:[译者ID](https://github.com/译者ID)
译者:[strugglingyouth](https://github.com/strugglingyouth)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出