pre-commit --comment-style regex - pre-commit-hook

I have a precommit file which checks for commented line format in .cfg files. The current comment style is a semicolon, so this will fail if a pound sign is used for file comments. Can RegEx somehow be used to match multiple comment patterns? For example:
- --comment-style
- ';'|'#'|'some other comment'
files: |
(?x)(
\.cfg
)$
This is the source of the precommit test:
https://github.com/Lucas-C/pre-commit-hooks/blob/master/tests/insert_license_test.py
Thanks.

it cannot
https://github.com/Lucas-C/pre-commit-hooks/blob/556aa509296132b8e2477f0a3e33c9916232e748/pre_commit_hooks/insert_license.py#L32-L35
parser.add_argument('--comment-style', default='#',
help='Can be a single prefix or a triplet: '
'<comment-start>|<comment-prefix>|<comment-end>'
'E.g.: /*| *| */')
there is only one comment type that is allowed by that hook

Related

How to replace a matching pattern inside a file to another using bash

My script has many lines starting with slo. How can I replace all the strings that are starting with slo to fwd using bash commands? Any help would be appreciated.
Here is a snippet of my script
template_version: 2018-03-02
resources:
instance01:
type: ../../../templates/nf.yaml
properties:
vm_name: 'slol2lvdl1'
vm_flavour: 'dns_19te'
image_name: 'pdns_dnsd_slo_211214121207'
vm_az: 'az-1'
vm_disk_root_size: '50'
vm_disk_data_size: '50'
network_mgmt_refs: 'int:dns_ox_slo_507:c3dns_slo_live_nc_vnns_pcg'
My requirement is to replace all slo to fwd in the above code. I have 5 files like this in the same directory.
sed is the go-to for file content replacements with regular expressions. If every slo you want to replace is between _ characters it's fairly easy with a command like this (in GNU sed which ships with just about all linuxes):
sed -i -e 's/_slo_/_fwd_/g' files to replace
-i replaces the text inline, replacing existing file contents with updated contents.
If not all slo are within _ characters you need to worry about unintentional matches.
Be sure to make a backup of these files or if they're in a git repo work from a clean state in case you don't like the change. Using git to track the changes might make sense even if you don't currently have the files in a git repo as this will make it trivial to compare before and after.
sed -i 's/slo/fwd/' worked! Also found many alternatives but sed was straight forward!

Linux sed replacing word in generic way

I would like to do a sed command in Linux to uncomment the "#auth"
Original file
#%PAM-1.0
auth sufficient pam_rootok.so
# Uncomment the following line to implicitly trust users in the "wheel" group.
#auth sufficient pam_wheel.so trust use_uid
I can write this command to do it:
sed 's/#auth.*sufficient.*pam_wheel.so trust use_uid/auth\t sufficient\t pam_wheel.so trust use_uid/' /etc/pam.d/su
But I think it is too long. Is there any better way to do this (more generic)?
I don't want to specific the line number to replace it, because if someone changed the file, the script will not run normally.
For example:
Search keyword "#auth.*sufficient.*pam_wheel.so trust use_uid", if found, replace this the word "#auth" to "auth", and then append the later wording in the line
With GNU sed, look up the -i option that allows in-place modification and then anchor the regular expression. For instance:
sed -i '/^#auth.*pam_wheel/s/^#//' INPUTFILE
will look for lines beginning with "#auth" that include "pam_wheel" later on the line and replace the "#" at the beginning with nothing.

Ansible: Is it possible to search replace single word

In the lineinfile module, it replaces the full line.
If the line is long I have to repeat the whole line again.
Let us suppose I want to replace the single word in the file:
#abc.conf
This is my horse
this is the playbook:
- lineinfile: dest=abc.conf
state=present
regexp='horse'
line='This is my dog'
backup=yes
is there any way to achieve someting like sed 's/horse/dog/g' ?
New module replace available since 1.6 version:
- replace:
dest=abc.conf
regexp='horse'
replace='dog'
backup=yes
You can use backreferences to retrieve other parts(that should not be changed) of the line:
- lineinfile: dest=abc.conf
state=present
regexp='^(.*)horse(.*)$'
line='\1dog\2'
backup=yes
backrefs=yes
If you need to do more replace operations in one block and you have the file locally, you might want to consider using template, which substitutes variables in the template file and copies the file to the remote:
- template: src=/mytemplates/foo.j2 dest=/etc/file.conf
In the local file you can write a variable with ansible sintax like
{{variable}}
and it will be substituted if it is in the scope of the script. Here the docs.

vim: comment/uncomment all lines in file containing a string

I have a big python file as follows:
#login_required
#user_passes_test(lambda u: u.is_superuser)
def foo():
//function body
#login_required
#user_passes_test(lambda u: u.is_superuser)
def foobar():
//function body
.
.
.
Like this there are many functions in the file. I want to comment all the lines which contains the pattern login_required or user_passes_test. How to comment those lines?
I use tComment plugin. So I can toggle line comment using gcc key-mapping. Can it be used?
There are also other files in the project which contains similar functions. So how can I comment these lines in all files in the project?
And again if I need to uncomment those lines how do I?
The :global/{pat}/{cmd} command will run a command, {cmd}, on every line matching pattern, {pat}. You can execute your tComment command via the :normal command. All together it looks like this:
:g/#login_required/norm gcc
For more help see:
:h :g
:h :norm
If you want to comment certain lines, then uncomment those same lines later, I'd use some kind of "marker" in the comment to make the job easier.
So to comment, for example:
1,$s/^\(.*#login_require\)/#FOO \1/
Then to uncomment:
1,$s/^#FOO //
You would choose #FOO so as not to be using it anywhere else for another purpose. You can even pick something simpler like ##... really anything that starts with # that you're not already using.
This will not be with VIM, but i think easier way to comment out in all project,multiple files:
sed -i 's/#login_required/#login_required/g' *
or for files in directories:
find ./ -type f -exec sed -i 's/#login_required/#login_required/g' {} \;
Comment lines containing string:
:%s/\(.*string\)/# \1/c
%s - global substitute
\(.*string\) - pattern to match
# \1 - replacement, \1 is for the matched pattern
c - confirm before substitution
Similarly, to uncomment:
:%s/# \(.*string\)/\1/c

How to delete a particular line which has occurred many times in the file in vim

I want to delete a line: This is an example, which occurs multiple times in a file. How do I go about it.
Thanks,
Alisha
You could do:
:g/This is an example/d
:%s/This is an example\n//gc
% indicates all lines of a file
s indicates pattern to be searched.
g for global replacement
c for confirmation on each replace
If you want to delete the lines containing only the exact match you could:
:g/^This is an example$/d
You can do this using an external command:
:%!grep -v "This is an example"
This filters the entire file through the given command. The grep -v command selects all the lines of the file that do not match the given regular expression.

Resources