Linux: 从 find 命令快速创建格式化输出文件 (csv)
Linux: fast creating of formatted output file (csv) from find command
我有几个设备,我想将它们收集在一个列表 (csv) 中,以便将它们放入 mysql 数据库中。我从一个设备开始,目标是从使用 'find' 创建的 infile 文件创建一个新的格式化输出文件。该设备是 /mnt/sda4,我跳过了所有包含“.cache”的条目。我也已经剪了 /mnt/sda4/
:
find /mnt/sda4 | grep -v '.cache' | cut -d'/' -f4- > infile
infile 是这样的:
Extern-500GB-btrfs/root/usr/lib64/libreoffice/share/config/soffice.cfg/dbaccess/ui/mysqlnativesettings.ui
Extern-500GB-btrfs/root/usr/lib64/libreoffice/share/config/soffice.cfg/dbaccess/ui/namematchingpage.ui
...
这部分很快。
real 0m1,432s
user 0m1,079s
sys 0m0,873s
现在,我有两个解决方案,都(非常)慢,我想要一个新的输出列表,其中包含处理的每一行; “06;” basename ";/" 处理整行,像这样:
06;mysqlnativesettings.ui;/Extern-500GB-btrfs/root/usr/lib64/libreoffice/share/config/soffice.cfg/dbaccess/ui/mysqlnativesettings.ui
06;namematchingpage.ui;/Extern-500GB-btrfs/root/usr/lib64/libreoffice/share/config/soffice.cfg/dbaccess/ui/namematchingpage.ui
...
time while read p; do bn=$(basename "$p"); echo "06;""$bn"";/""$p" >> outfile.csv; done < infile
需要的时间是:
real 27m44,937s
user 10m4,539s
sys 18m6,491s
我再次尝试使用一个命令行同时结合查找和格式化:
time find /mnt/sda4/ | while read p; do g=$(echo $p | grep -c -v '\.cache'); case "$g" in 1) echo "06;$(basename "$p")"';/'$(cut -d'/' -f4- <<<"$p") >>outfile.csv;; *) : ;; esac; done
忘记时间了,不过也花了很长时间。
所以,我的问题是:是否有(快得多)创建第二个 table 的方法,也许在使用 find 时直接创建?
提前谢谢你,
-Linuxfluesterer
我想问题出在循环和所有重定向上;你考虑过使用awk吗?我认为以下内容应该可以满足您的所有需求 - 不过我显然没有要测试的目录结构 - 而且要相当快。
time find /mnt/sda4/ | awk 'BEGIN{FS=OFS="/"}!/.cache/ {==""; new=sprintf("%s",[=10=]);gsub(/^\/\/\//,"",new); printf "06;%s;/%s\n",$NF,new }' > outfile.csv
我有几个设备,我想将它们收集在一个列表 (csv) 中,以便将它们放入 mysql 数据库中。我从一个设备开始,目标是从使用 'find' 创建的 infile 文件创建一个新的格式化输出文件。该设备是 /mnt/sda4,我跳过了所有包含“.cache”的条目。我也已经剪了 /mnt/sda4/
:
find /mnt/sda4 | grep -v '.cache' | cut -d'/' -f4- > infile
infile 是这样的:
Extern-500GB-btrfs/root/usr/lib64/libreoffice/share/config/soffice.cfg/dbaccess/ui/mysqlnativesettings.ui
Extern-500GB-btrfs/root/usr/lib64/libreoffice/share/config/soffice.cfg/dbaccess/ui/namematchingpage.ui
...
这部分很快。
real 0m1,432s
user 0m1,079s
sys 0m0,873s
现在,我有两个解决方案,都(非常)慢,我想要一个新的输出列表,其中包含处理的每一行; “06;” basename ";/" 处理整行,像这样:
06;mysqlnativesettings.ui;/Extern-500GB-btrfs/root/usr/lib64/libreoffice/share/config/soffice.cfg/dbaccess/ui/mysqlnativesettings.ui
06;namematchingpage.ui;/Extern-500GB-btrfs/root/usr/lib64/libreoffice/share/config/soffice.cfg/dbaccess/ui/namematchingpage.ui
...
time while read p; do bn=$(basename "$p"); echo "06;""$bn"";/""$p" >> outfile.csv; done < infile
需要的时间是:
real 27m44,937s
user 10m4,539s
sys 18m6,491s
我再次尝试使用一个命令行同时结合查找和格式化:
time find /mnt/sda4/ | while read p; do g=$(echo $p | grep -c -v '\.cache'); case "$g" in 1) echo "06;$(basename "$p")"';/'$(cut -d'/' -f4- <<<"$p") >>outfile.csv;; *) : ;; esac; done
忘记时间了,不过也花了很长时间。
所以,我的问题是:是否有(快得多)创建第二个 table 的方法,也许在使用 find 时直接创建?
提前谢谢你,
-Linuxfluesterer
我想问题出在循环和所有重定向上;你考虑过使用awk吗?我认为以下内容应该可以满足您的所有需求 - 不过我显然没有要测试的目录结构 - 而且要相当快。
time find /mnt/sda4/ | awk 'BEGIN{FS=OFS="/"}!/.cache/ {==""; new=sprintf("%s",[=10=]);gsub(/^\/\/\//,"",new); printf "06;%s;/%s\n",$NF,new }' > outfile.csv