이 파일이 있어요
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
. 패턴과 라인은 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
암소 비슷한 일종의 영양 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