как удалить содержимое в двойных кавычках из терминала в текстовом файле

read -p "Enter path name: " PT
sed -i "/path/ s#.*#& $PT#" textfile

Вам нужно использовать двойные, а не одинарные кавычки. Одинарные кавычки защищают строку от оболочки, но в этом случае вы хотите, чтобы оболочка расширила значение $PT.

Кроме того,

echo "$(... )"

— это то же самое, что и...(материал в$(... )).

0
02.12.2019, 22:42
2 ответа
sed ':a;N;$!ba;s/"[^"]*"//g' file1 > file2
0
28.01.2020, 03:03

Copyright James Daniel Marrs Ritchey. This material was created for submission at 'how to remove content within double quotes from terminal in a plain text file', but can also be alternatively obtained from 'https://snippetly.blogspot.com/2019/12/delete-double-quoted-content.html' under the terms of any of the following licenses: Comprehensible Open License 3.0 (https://jamesdanielmarrsritchey.blogspot.com/2019/06/comprehensible-open-license-30.html), MIT (https://opensource.org/licenses/MIT).

Чтобы удалить содержимое, заключенное в двойные кавычки, вы можете использовать preg _replace в PHP. Просто убедитесь, что он не -жадный, чтобы он не удалял все между первой и самой последней двойной кавычкой.

Для текста:

<?php

$text = <<<'EOT'

This is some example text. Bob, "Yah, but it's not very interesting to read." Yet

there is more example text of a similar nature. Natalie, "Please don't type anymore example

text for me to read. It's really bad." Still, more example text is typed, because it is important

that there be more text after the last dialogue example to ensure proper

function of the code. David, "I think that's plently of example text!" David was correct.

EOT;

$new_text = preg_replace('/\"[\s\S]+?\"/', '', $text);

echo $new_text;

?>

Для текстового файла:

<?php

$text = file_get_contents($file);

$new_text = preg_replace('/\"[\s\S]+?\"/', '', $text);

file_put_contents($new_file, $new_text);

?>
0
28.01.2020, 03:03

Теги

Похожие вопросы