我有这个文件
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
我想使用 awk 在模式之后/之前添加一行127.0.0.1
。 Pattern 和 line 是 bash 变量。
#!/bin/bash
file="test.txt"
pattern='127.0.0.1'
line='127.0.1.1 cent.centurian.com centurian'
awk -vpattern="$pattern" -vline="$line" '/pattern/{print;print line;next}1' "$file"
不起作用...
答案1
sed
更简单:
sed "/$pattern/a\
$line" "$file"
输出:
127.0.0.1 localhost
127.0.1.1 cent.centurian.com centurian
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
GNU sed
允许上述的单行版本:
sed $file -e "/$pattern/a $line"
...并输出$line
前 $pattern
,改变a
(待定)到一个i
(插入):
sed $file -e "/$pattern/i $line"
答案2
关闭。这会寻找(字面意思)模式pattern
。
您需要使用$0 ~ pattern
来匹配变量。
$ pattern='127.0.0.1'
$ line='127.0.1.1 cent.centurian.com centurian'
$ awk -vpattern="$pattern" -vline="$line" '$0 ~ pattern {print; print line; next} 1' $file | head -2
127.0.0.1 localhost
127.0.1.1 cent.centurian.com centurian
答案3
刚刚做了一个函数来执行此操作:
################################################################################
## Adds a line in a file. ##
#------------------------------------------------------------------------------#
# Given the arguments: #
# 1st: file #
# 2nd: string to find #
# 3rd: line to add #
# 4th: 'b' for before, 'a' for after. #
# It adds a line before or after the line containing the search string. #
################################################################################
function addLineInFileOnString() {
local file pattern line where tmp
file="$1"
pattern="$2"
line="$3"
where="$4"
tmp="$( cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1 )"
tmp='tmp.'$tmp
if [[ $where == 'b' ]]; then
awk -v pattern="$pattern" -v line="$line" '$0 ~ pattern {print line; print; next} 1' "$file" | tee 1>/dev/null $tmp
elif [[ $where == 'a' ]]; then
awk -v pattern="$pattern" -v line="$line" '$0 ~ pattern {print; print line; next} 1' "$file" | tee 1>/dev/null $tmp
fi
[[ -e $tmp ]] && cp "$tmp" "$file" && rm "$tmp"
}
答案4
在匹配行之前:
awk -v line='127.0.1.1 cent.centurian.com centurian' '/127\.0\.0\.1/ \
{ printf "%s\n%s\n", line, $0; next }; 1' file.txt
匹配行之后:
awk -v line='127.0.1.1 cent.centurian.com centurian' '/127\.0\.0\.1/ \
{ printf "%s\n%s\n", $0, line; next }; 1' file.txt
/127\.0\.0\.1/
与模式匹配printf
如果模式匹配,则根据变量line
是在匹配行之前还是之后打印所需的格式化输出
例子:
$ cat file.txt
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
$ awk -v line='127.0.1.1 cent.centurian.com centurian' '/127\.0\.0\.1/ { printf "%s\n%s\n", $0, line; next }; 1' file.txt
127.0.0.1 localhost
127.0.1.1 cent.centurian.com centurian
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
$ awk -v line='127.0.1.1 cent.centurian.com centurian' '/127\.0\.0\.1/ { printf "%s\n%s\n", line, $0; next }; 1' file.txt
127.0.1.1 cent.centurian.com centurian
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters