将 2 个文件合并为一个,并在其间添加自定义行

将 2 个文件合并为一个,并在其间添加自定义行

我有 2 个文件 A 和 B。我想将它们合并到文件 C 中,并在文件 C 之间添加自定义行。

文件A:

Hello

文件B:

Hi

合并的文件应包含:

Records in File A is 
Hello
Records in File B is 
Hi

答案1

你可以这样做:

for file in fileA fileB; do 
    printf 'Records in %s are\n%s\n' "$file" "$(cat "$file")"
done > fileC

或者:

for file in fileA fileB; do 
    echo "Records in $file are"; cat "$file"
done > fileC

两者都产生:

Records in fileA are
Hello
Records in fileB are
hi

答案2

for f in A B
do
 (echo "Records in File $f is"; cat "$f" ) >> C
done

相关内容