tengo este archivo
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
y quiero usar awk para agregar una línea después/antes del patrón 127.0.0.1
. El patrón y la línea son variables de 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"
No funciona...
Respuesta1
sed
es más simple:
sed "/$pattern/a\
$line" "$file"
Producción:
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
ÑU sed
permite una versión de una sola línea de lo anterior:
sed $file -e "/$pattern/a $line"
...y a la salida$line
antes $pattern
, cambiar ela
(añadir)a unai
(insertar):
sed $file -e "/$pattern/i $line"
Respuesta2
Cerca. Eso busca el patrón (literal) pattern
.
Debe utilizar $0 ~ pattern
para comparar con una variable.
$ 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
Respuesta3
Acabo de crear una función que hace esto:
################################################################################
## 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"
}
Respuesta4
Antes de la línea coincidente:
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
Después de la línea coincidente:
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/
coincide con el patrónSi el patrón coincide, la salida formateada deseada se imprime en
printf
función de si la variableline
estaría antes o después de la línea coincidente.
Ejemplo:
$ 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