Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

bash sed with variable

# If you want to store your regex in a bash variable 
# and use it in sed, you might need the following
BASE_REGEX="[0-9]{4}.[0-9]+"
REGEX_ESCAPED=$(sed 's/[}{)(+]/&/g; s/^/^/g' <<< "$BASE_REGEX")

# One variable can be used with sed
sed -i "s/${REGEX_ESCAPED}/REPLACEMENT/g" file.txt
# The other can be used with bash internal regex operator
if [[ "2022.42" =~ ^${BASE_REGEX}$ ]]
then
	echo 'Matches !'
fi
Comment

sed with variable

sed -i "s/$var1/ZZ/g" "$file"
Comment

sed with variables in shell script

Use double quotes for the sed expression.

sed "s/$replace_string/$replace_with/"`
Comment

passing bash variable to sed

rr=abcd # in shell

sed -i "s/aaa/$rr/g" file.dat 

# -i will update thefile, 
Comment

PREVIOUS NEXT
Code Example
Shell :: enter user root linux 
Shell :: list inactive services ubuntu 
Shell :: wsl2 settings 
Shell :: show file format snowflake 
Shell :: conda command not found 
Shell :: undo reset HEAD^ 
Shell :: check litespeed version command 
Shell :: parent branch 
Shell :: clipboard manager linux unity 
Shell :: how to push existing project to github 
Shell :: set email and name which gets baked into the every git commit in the local config file found in ./.git/config 
Shell :: Powershell TLS 
Shell :: how to install redis 
Shell :: download kubectl for windows 
Shell :: mkdir powershell 
Shell :: ssh git clone 
Shell :: push a branch with diffrent name 
Shell :: install redis 
Shell :: To see details about a wifi connection on linux 
Shell :: error eacces permission denied mkdir xampp ubuntu 
Shell :: sudoer sudo no password NOPASSWORD 
Shell :: Cannot install windows-build-tools 
Shell :: redis download 
Shell :: git go back to previous commit 
Shell :: docker compose exec compose 
Shell :: mkdir multiple directories windows 
Shell :: instal kdenlive 
Shell :: chmod a+x 
Shell :: github cli 
Shell :: docker install 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =