Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
翻译完成
Browse files Browse the repository at this point in the history
  • Loading branch information
MjSeven committed May 1, 2019
1 parent 350ef26 commit 3e1e15a
Showing 1 changed file with 14 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@
[#]: via: (https://fedoramagazine.org/command-line-quick-tips-cutting-content-out-of-files/)
[#]: author: (Stephen Snow https://fedoramagazine.org/author/jakfrost/)

Command line quick tips: Cutting content out of files
命令行技巧:分割文件内容
======

![][1]

The Fedora distribution is a full featured operating system with an excellent graphical desktop environment. A user can point and click their way through just about any typical task easily. All of this wonderful ease of use masks the details of a powerful command line under the hood. This article is part of a series that shows you some common command line utilities. So let’s drop into the shell, and have a look at **cut**.
Fedora 发行版是一个功能齐全的操作系统,有出色的图形化桌面环境。用户可以很容易地通过单击动作来完成任何典型任务。所有这些美妙的易用性掩盖了其底层强大的命令行细节。本文是向你展示一些常见命令行实用程序的系列文章的一部分。让我们进入 shell 来看看 **cut**

Often when you work in the command line, you are working with text files. Sometimes these files may be quite long. Reading them in their entirety, while feasible, can be time consuming and prone to errors. In this installment you’ll learn how to extract content from text files, and get the information you want from them.
通常,当你在命令行中工作时,你处理的是文本文件。有时这些文件可能很长,虽然可以完整地阅读它们,但是可能会耗费大量时间,并且容易出错。在本文中,你将学习如何从文本文件中提取内容,并从中获取你所需的信息。

It’s important to recognize that there are many ways to accomplish similar command line tasks in Fedora. The Fedora repositories include entire language systems for parsing and working with text, as an example. Also, there are multiple command line utilities available for just about any purpose conceivable in the shell. This article will only focus on using a few of those utility choices, to extract some information from a file and present it in a readable format.
重要的是要意识到,在 Fedora 中有许多方法可以完成类似的命令行任务。例如,Fedora 仓库含有用于解析和处理文本的完整语言系统。此外,还有多个命令行实用程序可用于 shell 中任何可能的用途。本文只关注使用其中几个实用程序选项,从文件中提取一些信息并以可读的格式呈现。

### Making the cut

To illustrate this example use a standard sizable file on the system like _/etc/passwd_. As seen in a prior article in this series, you can execute the _cat_ command to view an entire file:
### cut 使用

为了演示这个例子,在系统上使用一个标准的大文件,如 _/etc/passwd_。正如本系列的前一篇文章所示,你可以执行 _cat_ 命令来查看整个文件:
```
$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
Expand All @@ -31,15 +30,14 @@ adm:x:3:4:adm:/var/adm:/sbin/nologin
...
```

This file contains information on all accounts present on the system. It has a specific format:
此文件包含系统上所有所有账户的信息。它有一个特定的格式:

```
name:password:user-id:group-id:comment:home-directory:shell
```
假设你只想要系统上所有账户名的列表,如果你只能从每一行中删除 _name_ 值。这就是 _cut_ 命令派上用场的地方!它一次处理一行输入,并提取该行的特定部分。

Imagine that you want to simply have a list of all the account names on the system. If you could only cut out the _name_ value from each line. This is where the _cut_ command comes in handy! This command treats any input one line at a time, and extracts a specific part of the line.

The _cut_ command provides options for selecting parts of a line differently, and in this example two of them are needed, _-d_ which is an option to specify a delimiter type to use, and _-f_ which is an option to specify which field of the line to cut. The _-d_ option lets you declare the _delimiter_ that separates values in a line. In this case a colon (:) is used to separate values. The _-f_ option lets you choose which field value or values to extract. So for this example the command entered would be:
_cut_ 命令提供了以不同方式选择一行的部分的选项,在本示例中需要两个,_d_ 是指定要使用的分隔符类型,_f_ 是指定要删除行的哪个字段。_-d_ 选项允许你声明用于分隔行中值的 _delimiter_。在本例中,冒号(:)用于分隔值。_-f_ 选项允许你选择要提取哪个字段或哪些字段值。因此,在本例中,输入的命令是:

```
$ cut -d: -f1 /etc/passwd
Expand All @@ -50,13 +48,13 @@ adm
...
```

That’s great, it worked! But you get the printout to the standard output, which in a terminal session at least means the screen. What if you needed the information for another task to be done later? It would be really nice if there was a way to put the output of the _cut_ command into a text file to save it. There is an easy builtin shell function for such a task, the redirect function ( _>_ ).
太棒了,成功了!但是你将输出打印到标准输出,在终端会话中意味着它需要占据屏幕。如果你需要稍后完成另一项任务所需的信息,这该怎么办?如果有办法将 _cut_ 命令的输出保存到文本文件中,那就太好了。对于这样的任务,shell 有一个简单的内置功能,重定向功能(_>_)。

```
$ cut -d: -f1 /etc/passwd > names.txt
```

This will place the output of cut into a file called _names.txt_ and you can check the contents with _cat:_
这会将 cut 的输出放到一个名为 _names.txt_ 的文件中,你可以使用 _cat_ 来查看它的内容:

```
$ cat names.txt
Expand All @@ -67,19 +65,19 @@ adm
...
```

With two commands and one shell function, it was easy to identify using _cat_ , extract using _cut_ , and redirect the extracted information from one file, saving it to another file for later use.
使用两个命令和一个 shell 功能,可以很容易地使用 _cat_ 从一个文件进行识别、提取和重定向一些信息,并将其保存到另一个文件以供以后使用。

* * *

_Photo by _[ _Joel Mbugua_][2]_ on _[_Unsplash_][3]_._
_[ _Joel Mbugua_][2]_ _[_Unsplash_][3] 上的照片._

--------------------------------------------------------------------------------

via: https://fedoramagazine.org/command-line-quick-tips-cutting-content-out-of-files/

作者:[Stephen Snow][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
译者:[MjSeven](https://github.com/MjSeven)
校对:[校对者ID](https://github.com/校对者ID)

本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
Expand Down

0 comments on commit 3e1e15a

Please sign in to comment.