awkまたはsedを使用して単語のリストを検索し、新しい行に挿入する方法は?

awkまたはsedを使用して単語のリストを検索し、新しい行に挿入する方法は?

おすすめの皆様に感謝します。

私のファイル:

File1.sh(bash シェル)

prefix_ud =名前;

path_file=/パス/ファイルスクリプト;

sed -i "$(grep -n 'uname.*' "$path_file" |tail -1|cut -f1 -d':')a "$prefix_ud" yahoo.com" "$path_file"

sed -i "$(grep -n 'uname.*' "$path_file" |tail -1|cut -f1 -d':')a "$prefix_ud" twitter.com" "$path_file"

ファイル2の予想出力:

text1
text2
uname google.com
uname gmail.com
uname hotmail.com

uname yahoo.com

uname twitter.com

text3
text4

ベストアンサー1

醜いけど、これはOPが欲しいものだと思います。

一時ファイルを作成し、各行に「uname」を追加します。

sed 's/^/uname /' file_1 > tmp_file

"uname..."の最後の項目をfile_2変数に保存します。

last=$(awk '/uname.*/{a=$0}END{print a}' file_1)

最後の発生の後に以下を追加します。tmp_file

sed -e "/$last/rtmp_file" file_2

出力:

text1
text2
uname google.com
uname gmail.com
uname hotmail.com
uname yahoo.com
uname twitter.com
text3
text4

おすすめ記事