![各ファイル間に空白行を使用して複数のテキストファイルをリンクする方法は? [コピー]](https://linux33.com/image/11869/%E5%90%84%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E9%96%93%E3%81%AB%E7%A9%BA%E7%99%BD%E8%A1%8C%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%97%E3%81%A6%E8%A4%87%E6%95%B0%E3%81%AE%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%82%92%E3%83%AA%E3%83%B3%E3%82%AF%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95%E3%81%AF%EF%BC%9F%20%5B%E3%82%B3%E3%83%94%E3%83%BC%5D.png)
私は3つのhtmlファイルが使用されるシェルスクリプトを書いています。 3つのファイルをすべてリンクして、内容を電子メールで送信しようとしています。
以下で実行したい3つのファイルを参照してください。hello.html
、、、hello1.html
。hello2.html
これで、これら3つのファイルのすべての出力がFinal.Html
以下のファイルに追加され、電子メールで送信されます。
>Final.html
cat hello.html >> Final.html
cat hello1.html >> Final.html
cat hello2.html >> Final.html
cat Final.html | sendmail -t
今私が入力した3つのファイルはすべて次のようになります。
$ cat hello.html
Hello world
$ cat hello1.html
india is my world
$ cat hello2.html
India is the best
メール送信後に出力される内容は次のとおりです。
Hello world
india is my world
India is the best
私が探している出力は次のとおりです。各ファイルの間に空行があります。きれいで鮮明な出力を得る。
Hello world
india is my world
India is the best
答え1
これにより、必要な出力を提供する必要があります。
awk 'NR>1 && FNR==1{print ""};1' ./*.html > /path/to/Final.html
(出力ファイルが入力ファイルリストにないことを確認してください)
答え2
すべての操作を実行するには一度だけ使用し、通常はcat
表示に使用します。ㅏ実際に端末にファイルを保存します。猫- ファイルの作成(つまり、一緒に追加):
$ cat hello.html hello1.html hello2.html | mail -s "Subject Goes Here" [email protected]
答え3
これはうまくいきます。空行を追加するコードを追加しました。
echo
より多くの出力にも使用できる改行文字を出力します。
>Final.html
cat hello.html >> Final.html
echo >> Final.html
cat hello1.html >> Final.html
echo >> Final.html
cat hello2.html >> Final.html
次に、括弧を使用してコード量を減らします。
{
cat hello.html
echo
cat hello1.html
echo
cat hello2.html
}> Final.html
答え4
GNUを使用するawk
(ENDFILE
特殊モードをサポート):
awk '1; ENDFILE { print "" }' hello.html hello1.html hello2.html >Final.html
スクリプトawk
は各ファイルの変更されていないデータを順番に渡します(同じ操作を実行すること1;
で置き換えることができます)。{ print }
各ファイルの終わりに、このENDFILE
ブロックは空行を出力します。
各ファイルのデータの前にファイル名を追加するには(質問の一部ではありません):
awk 'BEGINFILE { print FILENAME } 1
ENDFILE { print "" }' hello.html hello1.html hello2.html >Final.html
特殊モードはBEGINFILE
同様に機能しますENDFILE
が、新しいファイルの最初の行を読み取る前にトリガーされます。このFILENAME
変数は標準awk
変数であり、現在の入力ファイルのパス名を含みます。