使用 Linux 中的文件名和文件内容创建 CSV 文件
Create CSV file using file name and file contents in Linux
我有一个包含超过 400K 个 txt 文件的文件夹。
名字像
deID.RESUL_12433287659.txt_234323456.txt
deID.RESUL_34534563649.txt_345353567.txt
deID.RESUL_44235345636.txt_537967875.txt
deID.RESUL_35234663456.txt_423452545.txt
每个文件内容不同
我想抓取文件名和文件内容并放入CSV。
类似于:
file_name,file_content
deID.RESUL_12433287659.txt_234323456.txt,Content 1
deID.RESUL_34534563649.txt_345353567.txt,Content 2
deID.RESUL_44235345636.txt_537967875.txt,Content 3
deID.RESUL_35234663456.txt_423452545.txt,Content 4
我知道如何使用 CSV 格式获取目录中的所有文件:
find * > files.csv
我怎样才能同时抓取文件的内容?
find *
有点奇怪,find
已经递归扫描了。 find .
足以包含所有 find *
(好吧,除非您考虑了一些奇怪的 shell glob 规则)。
- 我们需要遍历文件。也最好删除换行符。
# create file for a MCVE
while IFS=' ' read -r file content; do echo "$content" > "$file"; done <<EOF
deID.RESUL_12433287659.txt_234323456.txt Content 1
deID.RESUL_34534563649.txt_345353567.txt Content 2
deID.RESUL_44235345636.txt_537967875.txt Content 3
deID.RESUL_35234663456.txt_423452545.txt Content 4
EOF
{
# I'm using `|` as the separator for columns
# output header names
echo 'file_name|file_content';
# this is the hearth of the script
# find the files
# for each file execute `sh -c 'printf "%s|%s\n" "" "$(cat "")"' -- <filename>`
# printf - nice printing
# "$(cat "")" - gets file content and also removes trailing empty newlines. Neat.
find . -type f -name 'deID.*' -exec sh -c 'printf "%s|%s\n" "" "$(cat "")"' -- {} \;
} |
# nice formatting:
column -t -s'|' -o ' '
将输出:
file_name file_content
./deID.RESUL_44235345636.txt_537967875.txt Content 3
./deID.RESUL_35234663456.txt_423452545.txt Content 4
./deID.RESUL_34534563649.txt_345353567.txt Content 2
./deID.RESUL_12433287659.txt_234323456.txt Content 1
我有一个包含超过 400K 个 txt 文件的文件夹。
名字像
deID.RESUL_12433287659.txt_234323456.txt
deID.RESUL_34534563649.txt_345353567.txt
deID.RESUL_44235345636.txt_537967875.txt
deID.RESUL_35234663456.txt_423452545.txt
每个文件内容不同
我想抓取文件名和文件内容并放入CSV。
类似于:
file_name,file_content
deID.RESUL_12433287659.txt_234323456.txt,Content 1
deID.RESUL_34534563649.txt_345353567.txt,Content 2
deID.RESUL_44235345636.txt_537967875.txt,Content 3
deID.RESUL_35234663456.txt_423452545.txt,Content 4
我知道如何使用 CSV 格式获取目录中的所有文件:
find * > files.csv
我怎样才能同时抓取文件的内容?
find *
有点奇怪,find
已经递归扫描了。find .
足以包含所有find *
(好吧,除非您考虑了一些奇怪的 shell glob 规则)。- 我们需要遍历文件。也最好删除换行符。
# create file for a MCVE
while IFS=' ' read -r file content; do echo "$content" > "$file"; done <<EOF
deID.RESUL_12433287659.txt_234323456.txt Content 1
deID.RESUL_34534563649.txt_345353567.txt Content 2
deID.RESUL_44235345636.txt_537967875.txt Content 3
deID.RESUL_35234663456.txt_423452545.txt Content 4
EOF
{
# I'm using `|` as the separator for columns
# output header names
echo 'file_name|file_content';
# this is the hearth of the script
# find the files
# for each file execute `sh -c 'printf "%s|%s\n" "" "$(cat "")"' -- <filename>`
# printf - nice printing
# "$(cat "")" - gets file content and also removes trailing empty newlines. Neat.
find . -type f -name 'deID.*' -exec sh -c 'printf "%s|%s\n" "" "$(cat "")"' -- {} \;
} |
# nice formatting:
column -t -s'|' -o ' '
将输出:
file_name file_content
./deID.RESUL_44235345636.txt_537967875.txt Content 3
./deID.RESUL_35234663456.txt_423452545.txt Content 4
./deID.RESUL_34534563649.txt_345353567.txt Content 2
./deID.RESUL_12433287659.txt_234323456.txt Content 1