[英]How can I use perl/awk/sed to search for all occurrences of text wrapped in quotes within a file and then delete them?
How can I use perl, awk, or sed to search for all occurrences of text wrapped in quotes within a file, and print the result of deleting those occurrences from the file? 如何使用perl,awk或sed搜索文件中所有用引号引起来的文本,并打印出从文件中删除这些出现的结果? I do not want to actually alter the file, but simply print the result of altering the file like sed does.
我不想实际更改文件,而只是像sed一样打印更改文件的结果。
For example, say the file contains the following : 例如,假设文件包含以下内容:
data|more data|"not important"|"more unimportant stuff"
I need it to print out: 我需要它打印出来:
data|more data||
But I want to leave the file intact. 但我想保留文件原样。 I tried using sed but I could not get it to accept regexs.
我尝试使用sed,但无法接收正则表达式。
I have tried something like this: 我已经尝试过这样的事情:
sed -e 's/\<["]+[^"]*["]+\>//g' file.txt
but it does nothing and prints the original file. 但它不执行任何操作并打印原始文件。 Any Thoughts?
有什么想法吗?
Using a perl one-liner: 使用perl单线:
perl -pe 's/".*?"//g' file
Switches : 开关 :
-p
: Creates a while(<>){...; print}
-p
:创建while(<>){...; print}
while(<>){...; print}
loop for each line in your input file. while(<>){...; print}
循环。 -e
: Tells perl
to execute the code on command line. -e
:告诉perl
在命令行上执行代码。 You seem to have a few extra characters in your sed
command. sed
命令中似乎还有一些其他字符。
sed -e 's/"[^"]*"//g' file.txt
Input: 输入:
"quoted text is here" but not quoted there
never more
"hello world" foo bar
data|more data|"not important"|"more unimportant stuff"
Output: 输出:
but not quoted there
never more
foo bar
data|more data||
echo 'data|more data|"not important"|"more unimportant stuff"' | sed -E 's/"[^"]*"//g'
您不需要只为一个字符声明一个字符类(括号)...
my $cnt=qq(data|more data|"not important"|"more unimportant stuff");
my @arr = $cnt =~ m{(?:^|\|)([^"][^\|]*[^"])(?=\||$)}ig;
print "@arr";
This code might help you.. 此代码可能会帮助您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.