Perl:如何在linux中的模式之间获取线 [英] Perl: how to get lines between patterns in linux

查看:132
本文介绍了Perl:如何在linux中的模式之间获取线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的文件

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

我在perl中使用这个来获取ouputlike

I am using this in perl to get ouputlike

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

这是由此命令完成的

perl -plne'$ _ =FIELDS = \。 。== 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"

基本上变量NAME像ENTITY,BUNDLE,它应该从文件中获取

basically the variables NAME like ENTITY, BUNDLE, it should get from the file

我如何做到

推荐答案

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

输出

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




  • -l <​​/ code> chomps输入新行,并使用 print

  • -00 读取段落中的输入(这些由两个或多个换行符终止)

  • y | \\\
    \r [] | | d
    用空格替换换行符,删除 \r [ code> chars,并返回已更改字符数

  • ,因此 $ q 已分配 char只有当超过3个字符被替换(用于 FIELDS 条目)

  • s ||| 替换使用第一个非空格字符(实体,bundle,字段)和inerts = $ q / li>

    • -l chomps newline on input and add's it when using print
    • -00 reads input in paragraphs (these are terminated by two or more newlines)
    • y|\n\r[]| |d replaces newlines with spaces, deletes \r[] chars, and returns number of how many chars were altered
    • thus $q is assigned " char only when more than 3 chars were replaced (used for FIELDS entry)
    • s||| substitution takes first non spaces chars (entity,bundle,fields), and inerts = $q after them
    • 这篇关于Perl:如何在linux中的模式之间获取线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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