3 ms·
Widely useful pick of the bunch: # print section of file between two regular expressions (inclusive) sed -n '/Iowa/,/Montana/p' # case sensitiv
by zurn 9y ago
Widely useful pick of the bunch:
# print section of file between two regular expressions (inclusive)
sed -n '/Iowa/,/Montana/p' # case sensitive
- predakanga 9y agoI've found that pattern to be very useful when altering/sanitising MySQL backups. # Set the engine of all tables in schema `foo` to TokuDB sed -re '/^USE `foo`/,/^USE / s/^\) ENGINE=\S+/) ENGINE=TokuDB/'
- majewsky 9y agoI use stuff like that all the time with config files where sections span multiple lines. For example, if ~/.ssh/config has a section like Host example HostName example.com IdentityFile ~/.ssh/id_example Port 1234 Host next ... Then the following selects the identity file: sed -n '/^Host example/,/^Host\>/{/^\s*IdentityFile/{s/^\s*IdentityFile\s*//;p}}' ~/.ssh/config Although I would probably use awk after sed because it handled space-separated fields nicer: sed -n '/^Host example/,/^Host\>/p' ~/.ssh/config | awk '$1=="IdentityFile"{print$2}'