[英]How to exclude patterns in regex conditionally in bash?
这是input.txt的内容:
hello=123
1234
stack=(23(4))
12341234
overflow=345
=
friends=(987)
然后,我尝试匹配所有行,并平等地删除外部括号(如果行中有)。
要明确的是, 这是我要寻找的结果 :
hello=123
stack=23(4)
overflow=345
friends=987
我在这样的事情上坚强:
cat input.txt | grep -Poh '.+=(?=\()?.+(?=\))?'
但是不会返回任何内容。 我究竟做错了什么? 你有什么想法吗? 我很感兴趣
使用awk:
awk 'BEGIN{FS=OFS="="} NF==2 && $1!=""{gsub(/^\(|\)$/, "", $2); print}' file
hello=123
stack=23(4)
overflow=345
friends=987
这是sed
的另一种方法:
sed -nr ' # Use n to disable default printing and r for extended regex
/.+=.+/ { # Look for lines with key value pairs separated by =
/[(]/!ba; # If the line does not contain a paren branch out to label a
s/\(([^)]+)\)/\1/; # If the line contains a paren find a subset and print that
:a # Our label
p # print the line
}' file
$ sed -nr '/.+=.+/{/[(]/!ba;s/\(([^)]+)\)/\1/;:a;p}' file
hello=123
stack=23(4)
overflow=345
friends=987
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.