linux – bash用空替换连接多个文件(-e选项)
发布时间:2020-12-13 19:50:20 所属栏目:Linux 来源:网络整理
导读:我有以下代码将多个文件连接在一起.它工作正常,但我想将空值替换为0,所以我使用-e“0”.但它不起作用. 有任何想法吗? for k in `ls file?`do if [ -a final.results ] then join -a1 -a2 -e "0" final.results $k tmp.res mv tmp.res final.results else cp
我有以下代码将多个文件连接在一起.它工作正常,但我想将空值替换为0,所以我使用-e“0”.但它不起作用.
有任何想法吗? for k in `ls file?` do if [ -a final.results ] then join -a1 -a2 -e "0" final.results $k > tmp.res mv tmp.res final.results else cp $k final.results fi done 例: file1: a 1 b 2 file2: a 1 c 2 file3: b 1 d 2 Results: a 1 0 1 0 b 2 1 0 c 2 d 2 expected: a 1 1 0 b 2 0 1 c 0 2 0 d 0 0 2 解决方法
它的记录很少,但在使用join时,-e选项只能与-o选项一起使用.每次循环时都需要修改订单字符串.以下代码应生成所需的输出.
i=3 orderl='0,1.2' orderr=',2.2' for k in $(ls file?) do if [ -a final.results ] then join -a1 -a2 -e "0" -o "$orderl$orderr" final.results $k > tmp.res orderl="$orderl,1.$i" i=$((i+1)) mv tmp.res final.results else cp $k final.results fi done 如你所见,它开始变得凌乱.如果你需要进一步扩展这一点,可能值得推迟使用更强大的工具,如awk或python. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |