解释SED,grep和切语法 [英] Explaining sed, grep and cut syntax

查看:179
本文介绍了解释SED,grep和切语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我分析一个批处理文件,并有它编辑文本文件(输入)的线,使一个txt文件(输出)。

I am analyzing a batch file and there is a line that it edit a text file (input) and making a txt file (output).

批量使用三个帮助tools.exe:的grep SED 。我试着阅读他们的手册进行使用,但它是不容易的。

The batch is using three helping tools.exe: grep, sed and cut. I tried to read their manual use but it wasn't easy.

该行是:

type input.txt | sed "s#""#'#g" | grep -o "class='name[^>]*" | sed -n "/id=/p" | grep -o "surname=[^>]*" | cut -d"'" -f2 >output.txt

我想知道如何线路间preTED?什么是规则?有没有(使用一个工具,而不是所有的三个为例)这样做的更聪明的方式?

I want to know how the line is interpreted? What are the rules? Is there a smarter way of doing this (for example using one tool instead of all three)?

推荐答案

我会加入到杰布的回答,虽然它涵盖了大部分的你问什么。结果
这三个命令模拟移植自Linux的命令,他们执行以下操作:

I'll add to jeb's answer, although it covers most of what you asked.
These three commands are emulated commands ported from Linux, and they do the following:


  1. SED :用于过滤和转换文本的流编辑器

  2. 的grep :用于打印匹配模式的行工具

  3. 剪切:切割出一个文件的每一行的选定部分工具

  1. sed: a stream editor for filtering and transforming text.
  2. grep: a tool for printing lines matching a pattern.
  3. cut: a tool for cutting out selected portions of each line of a file.

我建议您无论是打字阅读更多关于这三个命令男人&LT;命令名称&gt; 在Linux或谷歌搜索同一个字符串(例如,<一个href=\"https://www.google.co.il/search?rlz=1C1CHEU_enIL452IL452&aq=f&sugexp=chrome,mod=5&sourceid=chrome&ie=UTF-8&q=man+grep\"相对=nofollow>男人的grep )。结果
此外,仰望定期EX pressions 的。虽然他们通常对于初学者不清楚,他们是重新presenting模式的通用和紧凑的方式。

I recommend that you read more about these three commands by either typing man <command name> in Linux, or Googling that same string (for instance, "man grep").
Also, look up regular expressions. Though they are usually unclear for beginners, they are a common and compact way for representing patterns.

就问题的具体用法:

sed的S#,##G

有关每一行,这个替换任何引号()与撇号(

For each line, this replaces any quotation marks ("") with an apostrophes (').

的grep -o类='名[^>] *?

这仅打印该行的部分开始类='名称,但没有跟着&GT;

This prints only the part of the line starting with class='name but without a following >.

SED -n/ ID = / P

在默认情况下桑达打印每行。在另一方面, SED -n&LT;有的模式与GT; / P只打印符合指定模式的行。在这种情况下,桑达只打印包含行 ID =

By default Sed prints every line. On the other hand, sed -n "<some pattern> /p" prints only the lines that match the specified pattern. In this case, Sed prints only the lines containing id=.

的grep -o姓= [^>] *?

这将打印与姓=名称'开头的行只把部分,但没有跟着&GT;

This prints only the part of the line that starts with surname=name' but without a following >.

切-d'-f2

这解析每个行撇号( )分隔开的连续领域,并挑选了第二个。

This parses each line as successive fields separated by an apostrophe ('), and picks the second one.

一切<青霉>管道的,这意味着每个命令的输出作为下一个命令到右输入。的input.txt的的内容被送入桑达命令,该命令的输出被送入grep命令,等等。最终的输出显然是印入名为output.txt的。

Everything is piped, meaning that the output of the each command serves as input for the next command to the right. The contents of "input.txt" are fed into the Sed command, the output of which is then fed into the grep command, and so on. The final output is obviously printed into a new file named "output.txt".

是的,像杰布提到的,这看起来像一个笨拙的解决方案,因为这里的一切都可以做到 SED 独自一人,presumably只由一个或两个命令。

And yes, like jeb mentioned, this looks like an awkward solution, because everything here can be done sed alone, presumably by only one or two commands.

这篇关于解释SED,grep和切语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆