bashから呼び出されたawkでファイルに行を追加する

bashから呼び出されたawkでファイルに行を追加する

このファイルを持っています

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

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

関連情報