Sed is your friend for applying regex's to files (well, streams really, since it is the 'stream editor'). The regex syntax is the same as for vim, and since that is my primary editor I only tend to use sed where the files are large and will take ages to load into vim.
Some quick examples to illustrate what I'm talking about:
sed s'/user root/user nothingtoseehere/g' < /var/log/auth.log sed s'!session closed for user \([^ ]*\)!\1 closed a session!g' < \ /var/log/auth.log sed s'!session closed for user \([^ ]*\)!&, allegedly!g' < \ /var/log/auth.logEscaped parenthesis
\(\)capture a value, and
&refers to the whole match. You can also use sed like grep. By default it prints every line, but that can be disabled with "-n" and you can cause matching lines to be printed by appending a "p" option:
sed -n 's/pattern/&/p' < file.txtOR, if we aren't making a substitution
sed -n '/pattern/p' < file.txtOR, you can just use grep -e, which is less confusing:
grep -e "pattern" file.txtRewriting a file in-place, making a backup with the .bak extension:
sudo sed -i.bak s'!XKBOPTIONS=""!XKBOPTIONS="ctrl:nocaps"!' /etc/default/keyboard
No comments:
Post a Comment