XからYまでの数字を含む行をZ行の後に別のファイルに挿入する方法は?

XからYまでの数字を含む行をZ行の後に別のファイルに挿入する方法は?

私は2つのファイルを作成しました。

echo -e "1\n2\n3\n4\n5" > 123.txt
echo -e "a\nb\nc\nd\ne" > abc.txt

123.txt以下を含むファイルを取得したいと思います。

1
2
3
b
c
d
4
5

つまり、abc.txtファイルの3行目の後にファイルの2〜4行を挿入します123.txt

ここで同様の質問をたくさん見ましたが、適切な答えが見つかりませんでした。それでも、次のような行があります。

sed -n '2,4p' abc.txt

ファイルの3行目の後にテキストを入力してください。

sed -i '3a\mytext' 123.txt

以前のコマンドstdoutまたは/and sed/単一のコマンドを使用してawkこれをどのように実行できますか?

ベストアンサー1

システムにGNUバージョンがある場合は、sedGNU拡張コマンドを使用できますr

r filename
    As a GNU extension, this command accepts two addresses.

    Queue the contents of filename to be read and inserted into the
    output stream at the end of the current cycle, or when the next input 
    line is read. Note that if filename cannot be read, it is treated as 
    if it were an empty file, without any error indication.

    As a GNU sed extension, the special value /dev/stdin is supported for 
    the file name, which reads the contents of the standard input. 

例えば、

$ sed '3r /dev/stdin' 123.txt < <(sed -n '2,4p' abc.txt)
1
2
3
b
c
d
4
5

おすすめ記事