Using shell variables in sed and awk

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.