[英]How to get the contents specified in the brackets?
I want to extract the content written in a file. 我想提取写在文件中的内容。 Let us take an example 让我们举个例子
{{{
How
R
U
}}}
{{{ Hi }}}
I want to print the output like this 我想这样打印输出
How
R
U
Hi
Well, this is not regex
this is using gawk
multi-character RS
. 好吧,这不是regex
而是使用gawk
多字符RS
。
awk -v RS='}}}|{{{' 'NF' inputfile
How
R
U
Hi
You can pipe
above command with something like this sed -r 's/^ +//g'
to remove extra spaces at the start of the line. 您可以使用诸如sed -r 's/^ +//g'
类的命令通过pipe
传递上述命令,以删除行首的多余空格。
By using regular expressions this becomes tricky, mainly because grep
or awk
seem not to support the lookahead . 通过使用正则表达式,这变得很棘手,主要是因为grep
或awk
似乎不支持lookahead 。
To extract or match only the text within the curly braces this regex could be used: https://regex101.com/r/ASFIFa/1 要仅提取或匹配花括号中的文本,可以使用此正则表达式: https : //regex101.com/r/ASFIFa/1
[^{\}]+(?=})
Therefore you could give a try to the ag - The Silver Searcher , something like this produce your required output: 因此,您可以尝试一下ag-Silver Searcher ,如下所示会产生所需的输出:
$ ag -o '[^{\}]+(?=})' input.txt
To remove the leading spaces: 删除前导空格:
$ ag -o '[^{\}]+(?=})' input.txt | sed 's/^ *//'
With sed 与sed
sed '/{{{/!d;:A;/\}\}\}/!{N;bA};s/{{{//;s/}}}//;s/ *//g' infile
Or 要么
sed -E '/\{{3}/!d;:A;/\}{3}/!{N;bA};s/\{{3}| *|\}{3}//g' infile
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.