Linux   发布时间:2022-04-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了linux – bash用空替换连接多个文件(-e选项)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我有以下代码将多个文件连接在一起.它工作正常,但我想将空值替换为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.
我有以下代码将多个文件连接在一起.它工作正常,但我想将空值替换为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.

大佬总结

以上是大佬教程为你收集整理的linux – bash用空替换连接多个文件(-e选项)全部内容,希望文章能够帮你解决linux – bash用空替换连接多个文件(-e选项)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。