如何从一个文件中读取文件名,并将文件名和内容存储到另一个文件中,分隔符为‘|’.
我有一个名为Filenames.txt的文件,它存储文本文件的名称.
> Cat Filenames.txt
File1.txt
File2.txt
File3.txt
> Cat File1
This is content of File1
> Cat File2
This is content of File2
> Cat File3
This is content of File3
所有这些文件Filenames.txt、File1.txt、File2.txt、File3.txt都存储在名为TestDir的目录中.
我的预期输出如下: 生成以下格式的新文件.
> Cat Outputfile.txt
File1|This is content of File1
File2|This is content of File2
File3|This is content of File3
我try 了下面的代码,但遇到了一些错误,
#!usr/bin/env bash
file=$(cat Filenames.txt)
y=$(pwd)
for line in $file
do
x=$(cat "${line}")
echo -e "${line}"|"${x}" >> Outputfile.txt
done
了解以下错误,
$ bash Script.sh
cat: 'file1.txt'$'\r': No such file or directory
Script.sh: line 7: : command not found
cat: 'file2.txt'$'\r': No such file or directory
Script.sh: line 7: : command not found
Script.sh: line 7: This is content of File3: command not found
当前输出:
test1_file
|
test2_file
|
test3_file|
注意:我在WindowsMachine中使用了Bash Shell
请帮帮忙. 向您致敬, 苏拉杰