Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

modify text file

Tags:

python

regex

perl

I need to modify all files that has a ".txt" extension within a directory in the following way:

remove all text lines beginning with the line that starts with "xxx" and the line that ends with "xxx", inclusive.

I know how to do this in Java or C++, but can someone show me a simple script that can get this done?

Thanks!

like image 897
m.larson Avatar asked Apr 21 '26 22:04

m.larson


1 Answers

I assume that you want to lose start and end, and those words appear by themselves on the lines you want lost.

perl -ni.bak -e 'print unless /^start$/../^end$/' *.txt

Note that I made a backup of the modified files so that you can inspect the change and fix it if you want.

like image 196
btilly Avatar answered Apr 23 '26 12:04

btilly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!