Заменить строку в текстовом файле, но только там, где ей предшествует определенный многострочный шаблон

[RDDSK / WRDSK]

When the kernel maintains standard io statistics (>= 2.6.20): The [read / write] data transfer issued physically on disk (so writing to the disk cache is not accounted for). This counter is maintained for the application process that writes its data to the cache (assuming that this data is physically transferred to disk later on). Notice that disk I/O needed for swapping is not taken into account. Unfortunately, the kernel aggregates the data tranfer of a process to the data transfer of its parent process when terminating, so you might see transfers for (parent) processes like cron, bash or init, that are not really issued by them.

https://www.systutorials.com/docs/linux/man/1-atop/

(Estoy de acuerdo en que esto es desafortunado. Especialmente dada la función anunciada de encima de mostrar los recursos utilizados incluso por los procesos que salieron en algún momento durante el intervalo de monitoreo, implementado utilizando la contabilidad de procesos, también conocida comopsacct).

-11
29.12.2016, 14:10
2 ответа

Одно из возможных решений:

paste -d' ' -s myfile \
 | sed 's/\(Orange Apple Banana\) Rice/\1 Olive/g' \
 | tr ' ' '\n'

Объяснение:

paste -d '' -s myfile принимает каждую строку myfile и вставляет его последовательно (т. Е. В одну строку) через разделитель (пробел). Вместо ввода по одному слову в строке, теперь у вас есть ввод со всеми словами в одной строке.

sed 's / \ (Orange Apple Banana \) Rice / \ 1 Olive / g' выполняет поиск во входных данных и заменяет каждое вхождение «Orange Apple Banana Rice» на «Orange Apple Banana Olive».

tr '' '\ n' переводит все вводимые пробелы в символы новой строки, конвертируя одну строку ввода обратно в исходный формат с одним словом в строке.

0
28.01.2020, 05:21

Perl отлично справляется со своей задачей:

perl -0pe 's/Orange\nApple\nBanana\n\KRice/Olive/' input.txt

Вы можете тренировать и тестировать регулярные выражения на regex101 .

1
28.01.2020, 05:21

Теги

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