简体   繁体   English

Perl:如何在Linux模式之间取得界限

[英]Perl: how to get lines between patterns in linux

I have a file like this 我有一个这样的文件

number: string(200)
issueDate: datetime
expiryDate: datetime
file_path: string(200)
filename: string(200)
description: text

I am using this in perl to get ouputlike 我在Perl中使用它来获得输出

FIELDS="number: string(200) issueDate: datetime expiryDate: datetime file_path: string(200) filename: string(200) description: text"

This is done by this command 这是通过此命令完成的

perl -plne '$_ = "FIELDS=\\""."$_" if $. == 1; $\\ = " ";$_ = "\\""."$_" if eof' document.txt

Now i have the full file like this 现在我有这样的完整文件

[entity]
JOHN

[BUNDLE]
mybundle

[FIELDS]
number: string(200)
issueDate: datetime
expiryDate: datetime
file_path: string(200)
filename: string(200)
description: text

Now i want the ouput to terminal like this in separate lines 现在我希望输出在这样的终端中分开

ENTITY = JOHN
BUNDLE = Mybundle
FIELDS="number: string(200) issueDate: datetime expiryDate: datetime file_path: string(200) filename: string(200) description: text"

basically the variables NAME like ENTITY, BUNDLE, it should get from the file 基本上是变量NAME,例如ENTITY,BUNDLE,它应该从文件中获取

how can i do that 我怎样才能做到这一点

perl -l -00pe '$q = y|\n\r[]| |d >3 && q("); s|(\S+)\s*|$1 = $q|; $_.= $q' file

output 产量

entity = JOHN
BUNDLE = mybundle
FIELDS = "number: string(200) issueDate: datetime expiryDate: datetime file_path: string(200) filename: string(200) description: text"
  • -l chomps newline on input and add's it when using print -l在输入中添加换行符,并在使用print时添加它
  • -00 reads input in paragraphs (these are terminated by two or more newlines) -00读取段落中的输入(它们以两个或多个换行符终止)
  • y|\\n\\r[]| |d y|\\n\\r[]| |d replaces newlines with spaces, deletes \\r[] chars, and returns number of how many chars were altered y|\\n\\r[]| |d用空格替换换行符,删除\\r[]字符,并返回已更改的字符数
  • thus $q is assigned " char only when more than 3 chars were replaced (used for FIELDS entry) 因此,仅当替换了3个以上的字符时,才$q分配"字符" (用于FIELDS条目)
  • s||| substitution takes first non spaces chars (entity,bundle,fields), and inerts = $q after them 替换使用第一个非空格chars(entity,bundle,fields),其后为inerts = $q

You can do it with awk like this: 您可以使用awk做到这一点:

awk '
   /^\[entity\]/  {getline e;next}
   /^\[BUNDLE\]/  {getline b;next}
   /:/            {f=f " " $0}
   /^description/ {print "entity=" e RS "BUNDLE=" b RS "FIELDS=" f;f=""}' yourfile

Explanation : 说明

If I fiind a line starting with [entity] I grab the following line and save as "e" 如果我找到以[entity]开头的行,则抓取以下行并另存为“ e”

If I find a line starting with [BUNDLE], I grab the following line and save as "b" 如果我发现以[BUNDLE]开头的行,则抓取以下行并另存为“ b”

If I find a line with a colon, I append it to "f" where I save the fields (with added spaces) 如果找到带冒号的行,请将其附加到“ f”,在其中保存字段(带有空格)

If I find a line starting with "description", I print out what I have found so far and clear the fields variable "f". 如果找到以“ description”开头的行,则打印出到目前为止所发现的内容,并清除字段变量“ f”。

Here's a fairly readable perl -ish version of my awk answer: 这是我的awk答案的相当可读的perl -ish版本:

perl -ne '
   $e=<> if /^\[entity\]/;      # save entity as $e from line after [entity]
   $b=<> if /^\[BUNDLE\]/;      # save bundle as $b from line after [BUNDLE]
   if(/:/){                     # if there's a colon in the line
      chomp; $f.= " " . $_;     # .. append this field to $f
   }
   print "entity = ",$e,"BUNDLE = ",$b,"FIELDS = \"$f\"\n" if /^desc/;
' yourfile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM