[英]SED Linux command to replace in exactly in a line
I have the following batch 我有以下一批
#! /bin/bash
PATTERN1="MACRO"
PATTERN2="HI"
sed -e "s/${PATTERN1}/${PATTERN2}/g" config.conf
So, the content of the file config.conf is 因此,文件config.conf的内容为
ABC DEF ERF MACRO ERR MACRO
So, the output of the command is: 因此,该命令的输出为:
ABC DEF ERFHI ERR
HI
but the desired output is 但所需的输出是
ABC DEF ERF MACRO ERRHI
In other words, I need to replace the pattern only with the exactly match per line, without spaces or other words in the same line. 换句话说,我只需要用每行完全匹配的方式替换模式,而同一行中不能有空格或其他单词。
add ^
and $
to the pattern you want to do exact match 将
^
和$
添加到要完全匹配的模式
with your example: you can either do: 以您的示例为例:您可以执行以下操作:
sed -e "s/^${PATTERN1}$/${PATTERN2}/g" ...
or use your original sed line, modify the PATTERN1
: 或使用原始的sed行,修改
PATTERN1
:
PATTERN1="^MACRO$"
尝试这个:
awk '$0==p {$0=r}8' p="${PATTERN1}" r="${PATTERN2}" file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.