For this ansible task:
- name: wheel sudoers
shell: sed -i -e "s/# %wheel\tALL=(ALL)\tNOPASSWD: ALL/%wheel\tALL=(ALL)\tNOPASSWD: ALL/g" /etc/sudoers
Need to replace # %wheel\tALL=(ALL)\tNOPASSWD: ALL with %wheel\tALL=(ALL)\tNOPASSWD: ALL.
But this syntax got an error after the first NOPASSWD: area:
X mapping values are not allowed in this context
Why can't I use this syntax?
First of all:
You should not use shell with sed in ansible, but the replace module.
E.g.:
- name: Allow sudo without password
ansible.builtin.replace:
path: /etc/sudoers
regexp: '# %wheel\tALL=\(ALL\)\tNOPASSWD: ALL'
replace: '%wheel ALL=(ALL) NOPASSWD: ALL'
Check out the python doc on how to write a correct regex, espc. for tabs (\t) and brackets.
Check out this question about how to manage sudoers as well.
Second:
Your code is not working because you do not quote your string correctly.
This should work (kudos to Zeitonautor):
shell: "sed -i -e \"s/# %wheel\\tALL=(ALL)\\tNOPASSWD: ALL/%wheel\\tALL=(ALL)\\tNOPASSWD: ALL/g\" /etc/sudoers"
Also check this post about using \t with sed. Using \t will not work with every version of sed.
Again: Do NOT use this! Use the replace module instead!
Related
I am attempting to use sed to replace a regular expression capture group. I've read that this requires enabling extended regular expressions with the -E flag. However, the following command is still not updating the text as expected.
echo "master-abcdef" | sed -i '' -E "s/IMAGE_TAG:\s*(\S+)$/\1/g" values.yaml
Where values.yaml has contents of:
global:
env: default
default:
IMAGE_TAG: dev-0be3323.zgm9a
... (more text below)
I am expecting values.yaml to be replaced to:
global:
env: default
default:
IMAGE_TAG: master-abcdef
... (more text below)
You may use this in any version sed:
sed -i.bak -E 's/(IMAGE_TAG:[[:blank:]]+)[^[:blank:]]+/\1master-abcdef/' file.yml
cat file.yml
global:
env: default
default:
IMAGE_TAG: master-abcdef
... (more text below)
Here [[:blank:]] matches a space or tab.
If you are using gnu-sed then use:
sed -i -E 's/(IMAGE_TAG:\s+)\S+/\1master-abcdef/' file.yml
With yq, you can write
yq eval '.default.IMAGE_TAG = "master-abcdef"' values.yaml
You can use
repl="master-abcdef"
sed -i '' -E "s/(IMAGE_TAG:[[:space:]]*).*/\\1$repl/" values.yaml
Here,
(IMAGE_TAG:[[:space:]]*) - captures into \1 an IMAGE_TAG: string and then any zero or more whitespaces
.* - matches the rest of the string (here, line).
The \1$repl replacement puts the captured value + the repl value in place of the matched text.
So I have the following string in my config.fish, and init.vim:
Fish: eval sh ~/.config/fish/colors/base16-monokai.dark.sh
Vim: colorscheme base16-monokai
Vim: let g:airline_theme='base16_monokai'
And I have the following shell script:
#!/bin/sh
theme=$1
background=$2
if [ -z '$theme' ]; then
echo "Please provide a theme name."
else
if [ -z '$background' ]; then
$background = 'dark'
fi
base16-builder -s $theme -t vim -b $background > ~/.config/nvim/colors/base16-$theme.vim &&
base16-builder -s $theme -t shell -b $background > ~/.config/fish/colors/base16-$theme.$background.sh &&
base16-builder -s $theme -t vim-airline -b $background > ~/.vim/plugged/vim-airline-themes/autoload/airline/themes/base16_$theme.vim
sed -i -e 's/foo/eval sh ~/.config/fish/colors/base16-$theme.$background.sh/g' ~/Developer/dotfiles/config.fish
sed -i -e 's/foo/colorscheme base16-$theme/g' ~/Developer/dotfiles/init.vim
sed -i -e 's/foo/let g:airline_theme='base16_$theme'/g' ~/Developer/dotfiles/init.vim
fi
Basically the idea is the script will generate whichever theme is passed through using this builder.
I have tried referring this documentation but I am not very skilled at regex so if anybody could give me a hand I would appreciate it.
What I need to happen is once the script is generated sed will look for the above strings and replace theme with the newly generated theme ones.
Try this :
sed -i "s|\(eval sh ~/\.config/fish/colors/base16-\)\([^.]*\)\.\([^.]*\)\\(.*\)|\1$theme.$background\4|
" ~/Developer/dotfiles/config.fish
sed -i "s/\(base16\)\([-_]\)\([a-zA-Z]*\)/\1\2$theme/g" ~/Developer/dotfiles/init.vim
Assuming in the second sed command that the theme is an alphanumeric string. If not, you can complete the character range : [a-zA-Z] with additional characters (eg [a-zA-Z0-9]).
You can replace something in sed using this syntax: sed "s#regex#replacement#g". Because you have /s and 's in your strings, it's easiest not to need to escape them.
There are some characters that need to be escaped to make the regexes. . and $ need to be escaped with a \. The $ in the replacement string needs to be escaped too.
If you want to capture a certain part from match, it's easiest to use char classes. For example, eval sh ~/\.config/fish/colors/base16-([^.]+)\.dark\.sh would be the regex to use if you want your replacement to be airline_theme='$1_base16_\$theme'. In that case, the $1 in the replacement is the thing captured in the regex.
[^.]+ will capture everything up to the next .
I hope this helps you to better understand regexes! This should be detailed enough to show you how to write your own.
You need to use double quotes for parameter expansion not single quotes.
You need to escape the single quotes: 'hello'\''world'
I will make one line for you and leave it as an exercise to fix the other lines
sed -i -e 's~\(let g:airline_theme='\''\)[^'\'']*\('\'\)'~base16_'"$theme"~' ~/Developer/dotfiles/init.vim
The first character after the s in the sed expression string is used as the pattern separator, so by putting / first you have specified / as the separator.
Additionally using the single quote tells the shell not to expand any variables, you are going to want to use double quotes instead.
try something like
sed -i -e "s#foo#eval sh ~/.config/fish/colors/base16-$theme.$background.sh#g" ~/Developer/dotfiles/config.fish
as you've now commented that you needed to find the previous theme string instead of foo
sed -i -e "s#eval sh \~/\.config/fish/colors/base16-.*?\..*?\.sh#eval sh ~/.config/fish/colors/base16-$theme.$background.sh#g" ~/Developer/dotfiles/config.fish
I am having trouble with expanding variables and ignoring their forward slashes.
I have written a simple script that finds text in my git repository and replaces it with other text. This works fine, but now I want to expand it using regex. This shouldn't be too much of a problem since both git grep and sed support regex. However, when I try to use regex in my input variables the forward slashes are removed which ruins the script.
If I run git grep "\bPoint" in the terminal I will get many results. However, I can't figure out how to get the same results when I use user input in my script. The git grep file will change my input to bPoint instead of \bPoint, and won't find any results to give to sed.
#!/bin/bash
# This script allows you to replace text in the git repository without damaging
# .git files.
read -p "Text to replace: " toReplace
read -p "Replace with: " replaceWith
git grep -l ${toReplace}
# The command I want to run
#git grep -l "${toReplace}" | xargs sed -i "s,${toReplace},${replaceWith},g"
I've tried a lot of different combinations of quotations, but nothing seems to work for me.
You must use read -r. As per help read:
-r do not allow backslashes to escape any characters
Examples:
# without -r
read -p "Text to replace: " toReplace && echo "$toReplace"
Text to replace: \bPoint
bPoint
# with -r
read -rp "Text to replace: " toReplace && echo "$toReplace"
Text to replace: \bPoint
\bPoint
Im trying to iterate over each file and folder inside a directory and extract part of the file name into a variable, but I can't make sed work correctly. I either get all of the file name or none of it.
This version of the script should capture the entire file name:
#!/bin/bash
for f in *
do
substring=`echo $f | sed -E -n 's/(.*)/\1/'`
echo "sub: $substring"
done
But instead I get nothing:
sub:
sub:
sub:
sub:
...
This version should give me just the first character in the filename:
#!/bin/bash
for f in *
do
substring=`echo $f | sed -E 's/^([a-zA-Z])/\1/'`
echo "sub: $substring"
done
But instead I get the whole file name:
sub: Adlm
sub: Applications
sub: Applications (Parallels)
sub: Desktop
...
I've tried numerous iterations of it and what it basically boils down to is that if I use -n I get nothing and if I don't I get the whole file name.
Can someone show me how to get just the first character?
Or, my overall goal is to be able to extract a substring and store it into a variable, if anybody has a better approach to it, that would be appreciated as well.
Thanks in advance.
If you want to modify a shell parameter, you probably want to use a parameter expansion.
for f in *; do
# This version should expand to the whole parameter
echo "$f"
# This version should expand to the first character in the filename
echo "${f::1}"
done
Parameter expansions are not as powerful as sed, but they are built in to the shell (no launching a separate process or subshell necessary) and there are expansions for:
Substrings (as above)
Replacing and substituting characters
Altering the case of strings (bash 4+)
and more.
This version of the script should capture the entire file name:
sed -E -n 's/(.*)/\1/'
But instead I get nothing.
You used -n so naturally it won't yield anything. Perhaps you should remove -n or add p:
sed -E -n 's/(.*)/\1/p'
This version should give me just the first character in the filename:
sed -E 's/^([a-zA-Z])/\1/'
But instead I get the whole file name,
You didn't replace anything there. Perhaps what you wanted was
sed -E 's/^([a-zA-Z]).*/\1/'
Also I suggest quoting your arguments well:
substring=`echo "$f" | sed ...'`
Finally the simpler method is to use substring expansion if you're using Bash as suggested by kojiro.
You forget to add .* after the capturing group in sed,
$ for i in *; do substring=`echo $i | sed -E 's/^(.).*$/\1/'`; echo "sub: $substring"; done
It's better to use . instead of [a-zA-Z] because it may fail if the first character starts with any special character.
I prefer awk to sed. It seems to be easier for me to understand.
#!/bin/bash
#set -x
for f in *
do
substring=`echo $f | awk '{print substr($1,1,1)}'`
echo "sub: $substring"
done
I am trying to get from user a path as an input.
The user will enter a specific path for specific application:
script.sh /var/log/dbhome_1/md5
I've wanted to convert the number of directory (in that case - 1) to * (asterisk). later on, the script will do some logic on this path.
When i'm trying sed on the input, i'm stuck with the number -
echo "/var/log/dbhome_1/md5" | sed "s/dbhome_*/dbhome_\*/g"
and the input will be -
/var/log/dbhome_*1/md5
I know that i have some problems with the asterisk wildcard and as a char...
maybe regex will help here?
Code for GNU sed:
sed "s#1/#\*/#"
.
$echo "/var/log/dbhome_1/md5" | sed "s#1/#\*/#"
"/var/log/dbhome_*/md5"
Or more general:
sed "s#[0-9]\+/#\*/#"
.
$echo "/var/log/dbhome_1234567890/md5" | sed "s#[0-9]\+/#\*/#"
"/var/log/dbhome_*/md5"
use this instead:
echo "/var/log/dbhome_1/md5" | sed "s/dbhome_[0-9]\+/dbhome_\*/g"
[0-9] is a character class that contains all digits
Thus [0-9]\+ matches one or more digits
If your script is in bash (which I assume when I see the tag, but I also doubt it when I see its name script.sh which seems to have the wrong extension for a bash script), you might as well use pure bash stuff: /var/log/dbhome_1/md5 will very likely be in positional parameter $1, and what you want will be achieved by:
echo "${1//dbhome_+([[:digit:]])/dbhome_*}"
If this seems to fail, it's probably because your extglob shell optional behavior is turned off. In this case, just turn it on with
shopt -s extglob
Demo:
$ shopt -s extglob
$ a=/var/log/dbhome_1234567/md5
$ echo "${a//dbhome_+([[:digit:]])/dbhome_*}"
/var/log/dbhome_*/md5
$
Done!