Reprinted from Jrckkyy final edit Zengzhaonong
Using shell variables in sed and awk
----------------------------------------------
x=mm
Sed ' s/ab/' $x '/g ' B.C
Or
Sed ' s/ab/' "$x" '/g ' B.C
Sed ' s/' "$val" '//' Urfile
The most recent process of anticipation, using bash scripts, requires the use of shell variables in the script's sed and awk to control the loop, and there are some problems to find the solution as follows:
1.sed using shell variables
This is easier to do, the SED command can be used in single and double quotes, generally we used to use single quotes, as follows:
Sed-i ' s/pattern1/pattern2/g ' inputfile
If you want to use a shell variable, you need to use double quotes
Pattern1=xxx
Pattern2=xxx
Sed-i "s/$pattern 1/$pattern 2/g" Inputfile
2. awk uses shell variables
There are a number of ways, the simplest of which is a record as follows:
Line=xxx
awk ' $1== ' $line ' "{print $} ' Inputfile
Note that the awk command itself needs to refer to the pattern and the action part in single quotes, so the inside reference to the shell variable is double quotes + single quotes, and double quotes are spaces that guarantee the correct handling of the variable values, magnified:
"' $line '"
Other than that:
awk ' $1= ' win "{$" ' $curcls ' "=$" ' $curcls ' "+1;print $} ' NWF
is another kind of indirect reference mode, because the shell's indirect reference is usually the use of the
Eval echo/$ $var
A higher level of
a=10
B=a
C=${!B}
Then c=10
The use of variables in sed, and the case of special characters in a variable?
----------------------------------------------
A=hellowww
B=xxx
echo $a | Sed "s/www/$b/"
Output: helloxxx no problem
But below:
A= ' ${asdf}/bin/ok '
b= '/one/two '
echo $a | Sed "s/${.*}/$b/"
bash:s/${.*}/$b/: Bad substitution
If it is:
echo $a | Sed ' s/${.*}/aaa/'
Aaa/bin/ok also no problem.
Please explain the reasons above and give a solution.
The feeling is because the $b contains the path symbol "/" reason. How to solve it.
echo $a | Sed "s#${.*}# $b #"
SED supports reverse matching
----------------------------------------------
Sed-n '/bsp/!p ' file
Match rows that do not contain a BSP
How do the results of SED assign values to variables
----------------------------------------------
var=$ (sed ...)
--This article is from: Using shell variables in sed and awk