Adiciona uma linha no arquivo com o awk chamado do bash

1

Eu tenho este arquivo

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

e eu quero usar o awk para adicionar uma linha após / antes do padrão 127.0.0.1 . Padrão e linha são variáveis 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"

Não funciona ...

    
por centurian 24.09.2016 / 22:24

4 respostas

3

sed é mais simples:

sed "/$pattern/a\
$line" "$file"

Saída:

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 permite uma versão de uma linha do acima:

sed $file -e "/$pattern/a $line"

... e para produzir $line antes $pattern , altere o a (ppend) para um i (nsert) :

sed $file -e "/$pattern/i $line"
    
por 25.09.2016 / 04:28
1

Fechar. Isso procura o padrão (literal) pattern .

Você precisa usar $0 ~ pattern para corresponder a uma variável.

$ 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
    
por 24.09.2016 / 22:33
0

Antes da linha correspondente:

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

Após a linha correspondente:

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/ corresponde ao padrão

  • Se o padrão for correspondido, a saída formatada desejada será impressa por printf com base em se a variável line seria antes ou depois da linha correspondente

Exemplo:

$ 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
    
por 24.09.2016 / 22:32
0

Acabou de fazer uma função que faz isso:

################################################################################
## 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"
}
    
por 25.09.2016 / 19:44

Tags