[英]How do I replace/filter out "$\n (quote end-of-line newline) using grep, awk, and sed in bash?
我有一个Java项目,其中有很多行如下所示:
myMethod("some text here ..."
+ " ... more text here"
+ " ... and even more text here");
我需要对此进行bash搜索:
"some text here ... ... more text here ... and even more text here";
我已经尝试过像这样的事情:
# Filtering for text between the two parenthesis
$ grep -rn "myMethod" . | awk -F\( '{print $2}' | awk -F\) '{print $1}' | sort | uniq
# replacing the `"$\n` with nothing
$ grep -rn "myMethod" . | sed -e 's/"$\n\s//g' | sort | uniq
# same
$ grep -rn "myMethod" . | sed -e ':a;N;$!ba;s/"$\n/,/g' | sort | uniq
但是,这些都不给我我想要的东西,这是所有唯一的Strings都传递给myMethod方法。
因此,我该如何使用grep,awk和bash中的sed替换或过滤掉"$\\n
(引用行尾换行符)?
试试这个( GNU grep
和GNU sed
,我相信您正在使用它们):
$ cat file
myMethod("some text here ..."
+ " ... more text here"
+ " ... and even more text here");
$ grep -rzn "myMethod" . | sed -rn '/myMethod/{:a;s/\)//;tb;N;ba;:b;s/\n//g;s/[^"]*$//;:c;s/^[^"]*"([^"]*)"(.*)/\2\1/;tc;p;}'
some text here ... ... more text here ... and even more text here
$ grep -rzn "myMethod" . | sed -rn '/myMethod/{:a;s/\)//;tb;N;ba;:b;s/\n//g;s/[^"]*$//;:c;s/^[^"]*"([^"]*)"(.*)/\2\1/;tc;s/^/"/;s/$/";/;p}'
"some text here ... ... more text here ... and even more text here";
我相信它将在多个文件和多次出现时起作用。
我使用sed
读取行之后,直到找到close paren )
,然后进行连接。
这是您要执行的操作吗(将GNU sed用于-z
并识别\\n
)?
$ sed -z 's/"\n+ "//g' file
myMethod("some text here ... ... more text here ... and even more text here");
$ sed -z 's/"\n+ "//g' file | sed -n 's/^myMethod("\([^"]*\).*/\1/p'
some text here ... ... more text here ... and even more text here
如果传递给myMethod的字符串可以包含转义的"
s,那么您只需要告诉我们如何转义(加倍?反斜杠?其他?),然后它们就易于处理。
尝试一下是否适合您:
awk -F'"' '/^myMethod\(/,/\);$/{str = str " " $2}END{print str}' file
对于您的输入,这将产生“这里有一些文本……这里有更多文本……这里有更多文本”。 您可以根据需要轻松修复领先空间。
基本上使用范围模式:仅在字符串myMethod(
和函数调用的结尾);
之间搜索);
然后抓取并连接$2
。 如果在同一行上有多个函数参数,则将无法使用。 您可能还需要考虑myMethod(
和)之后的空格);
。
完全可以在您提供的字符串和格式(包括引号和“ +”符号)上运行的内容如下所示:
>cat my_file.txt
myMethod("some text here ..."
+ " ... more text here ..."
+ " ... and even more text here");
other lines
and some other");
>sed -n '/myMethod/,/");/p' my_file.txt | sed -e ':a;N;$!ba;s/\n/ /g' -e "s/\"//g" -e "s/\+//g" -e "s/myMethod//g"
(some text here ... ... more text here ... ... and even more text here);
first sed提取字符串[myMethod]和首次出现的字符串[“);]之间的所有内容
然后我们有另一个sed,第一个表达式消除所有换行符,第二个表达式消除双引号,第三个表达式消除“ +”符号,最后一个表达式从最终输出中消除“ myMethod”字符串
如果您想荒唐地完成此操作,则可以添加[-e“ s / [(()] / \\” / g“]将输出的开头和结尾括号()转换为双引号”
LE:如果您的代码中包含以下任何字符,这显然会弄乱您的代码:[+“()myMethod]
无需其他工具; 这可以单独使用bash来完成。
$ s=$'myMethod("some text here ..."\n+ " ... more text here"\n+ " ... and even more text here");'
$ echo "$s"
myMethod("some text here ..."
+ " ... more text here"
+ " ... and even more text here");
$ t="${s//$'\n'/ }"
$ t="${t//\" + \"/ }"
$ t="${t#myMethod(\"}"
$ t="${t%\");}"
$ echo "$t"
some text here ... ... more text here ... and even more text here
它使用了一种称为“模式替换”的bash功能,该功能是“参数扩展”的一部分,您可以在bash的手册页或bash的官方文档中进行了解 。
这将遍历其中所有包含字符串myMethod
所有文件(在当前目录中本地),读取该方法直到将signal myMethod
起来);
并用一个衬板替换它:
>>cat my_file.txt
random first line
random second line
myMethod(first line of code
second line of code
third line of code);
# notice above method ending in string ");". This is important to mark the enclosing of the method.
# this string should not be present anywhere else withing the content of the method
other lines
and some other");
>>cat other_file.txt
myMethod("text in other file ..."
+ " ... yet more text from other file ..."
+ " ... and even more text here from the second file"); # ending of method
other lines
and some other");
ACTUAL COMAND
>>for file_containing_myMethod in `grep -l "myMethod" *`; do ONE_LINER=`sed -n '/myMethod/,/);/p' ${file_containing_myMethod} | sed -e ':a;N;$!ba;s/\n/ /g'`; sed -i "/myMethod/,/);/{/);/ s/.*/${ONE_LINER}/; t; d}" ${file_containing_myMethod}; done
random first line
random second line
myMethod(first line of code second line of code third line of code);
# notice above method ending in string ");". This is important to mark the enclosing of the method.
# this string should not be present anywhere else within the content of the method
other lines
and some other");
myMethod("text in other file ..." + " ... yet more text from other file ..." + " ... and even more text here from the second file"); # ending of method
other lines
and some other");
请注意,在测试时,应从最后一个sed
删除-i
标志。 -i
将立即更改/重写您的文件,并且您在测试之前不希望这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.