[Sed] Several Sao sed operation

A, sed command, in front of the row match, adding a line behind

 

Options in single quotation marks: a representative of after, i representatives in front

sed i represents the latter is operating directly in the text, the text is changed directly, without the i, that is, the changed output screen, the text and no real change!

 

Reference template:

# Sed -i '/ matching lines / a \ add content' file name

# Sed -i '/ matching lines / a \ add content' file name

# Sed -i '/ matching lines / a \ add content \' file name

Specific example:

# sed -i '/name/a\zhangsan' filename

# sed -i '/name/a \zhangsan' filename

# sed -i '/name/a \zhangsan\' filename

 

To be inserted in front of the matching line, so the above becomes a i can!

# Sed -i '/ matching lines / i \ add content' file name

# Sed -i '/ matching lines / i \ add content' file name

# Sed -i '/ matching lines / i \ add content \' file name

 

Two, Linux shell program of sed s / $ // s / * // g / ^ $ / d What does this mean?

 

s / $ // be appended to each row empty. The search is S s / a / b / a search to replace is b, and replaces only once.

s / * // g spaces will be deleted. g represents the search to replace all. "Space Star" ( "*") on behalf of multiple spaces

/ ^ $ / D delete blank lines.

 

 

Third, how additional content specific trekking tail with sed?

 

$ Represents the end of the line, split symbol flexibility to choose a file to append the beginning of the end of the line is the line XXX YYY,

# sed -i '/^XXX/ s/$/YYY/'       filename

# sed  -i  '/^XXX/ s:$:YYY:'    filename

# sed  -i  '/^XXX/ s#$#YYY#'    filename

在文件的以XXX开头的行的行首追加内容为YYY,^代表行尾,分割符号可以灵活选择

# sed -i '/^XXX/ s/^/YYY/'       filename

# sed  -i  '/^XXX/ s:^:YYY:'    filename

# sed  -i  '/^XXX/ s#^#YYY#'    filename

 

下面方式同样可以实现在在特定行行尾追加内容

 

#   sed -i  '/XXX/s/$/& YYY/'    filename

 

 

#   sed -i  '/XXX/s/^/& YYY/'    filename 

 


可以看出 &符合和追加的内容之间的空格决定了,追加的空格。

 

 

四、怎样在sed中做变量引用?

sed中支持变量的几种处理方法

1.  eval sed ’s/$a/$b/’ filename

2. sed   "s/$a/$b/"    filename

3. sed   ’s/’$a’/’$b’/’   filename

4. sed   s/$a/$b/       filename

 

 

五、综合应用举例

 

#  sed \

    -e '/^nexus-args/ s:$:,${jetty.etc}/jetty-https.xml:' \

    -e '/^application-port/a \application-port-ssl=8443\' \

    -i /etc/nexus-default.properties

 

 

-e 表示多点编辑,用于在一条命令中对同以文件做多次操作

-i   在这个位置表示直接修改文件,不是插入行的意思

 

 

 

 

#  sed \

    -r  '/<Set name="(KeyStore|KeyManager|TrustStore)Password">/ s:>.*$:>nexus@123</Set>:'  \

    -i  /etc/jetty/jetty-https.xml

 

#  cat   /etc/jetty/jetty-https.xml

 

-

-r 表示启用sed的扩展正则

# man sed

 

#  cat   /etc/jetty/jetty-https.xml

 

 

六、参考

 

Linux sed命令

https://www.runoob.com/linux/linux-comm-sed.html

 

sed操作:指定行增加删除内容

https://blog.csdn.net/m0_37886429/article/details/79043158

 

sed中支持变量的处理方法

https://www.cnblogs.com/tiantianhappy/p/9139751.html

Guess you like

Origin blog.csdn.net/michaelwoshi/article/details/94185665
sed