简体   繁体   English

如何使用perl / awk / sed搜索文件中所有用引号引起来的文本,然后将其删除?

[英]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

Explanation: 说明:

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.

相关问题 使用sed和/或awk,如何在文件中的所有文本上进行sed交换,直到看到与正则表达式匹配的行? - Using sed and/or awk, how can do a sed swap on all text in a file until I see a line matching a regex? 如何使用perl,sed或awk从具有内容的超大型多行文本文件中剪切html标签? - How to cut html tag from very large multiline text file with content with use perl, sed or awk? 使用sed删除所有出现的 - Use sed to delete all occurrences of <a name=“foo”> 如何使用sed / awk / perl用相等数量的破折号替换匹配的模式? - How can I use sed/awk/perl to replace a matched pattern with an equivalent number of dashes? 如何替换sed中匹配字符串中的所有字符 - How can I replace all occurrences of a character within matched string in sed 如何在文件中搜索所有以某种格式开头的数字开头的文本行并将其移动到新行 - How can I search a file for all text lines prefaced with numbers in a certain format and move them to a new line 如何使用sed或perl或awk等通过正则表达式删除文本块? - How to remove a text block by regular expression with sed or perl or awk etc? 在Perl中,如何删除不在双引号“”内的所有空格? - In Perl, how can I remove all spaces that are not inside double quotes “ ”? 删除引号内的文本 - Delete text within quotes 如何使用`sed`替换用双引号括住目录的单引号 - How can I use `sed` to replace the single quotes enclosing a directory with double quotes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM