文件格式如图:
1.txt中第二列数字与2.txt中第二列数字相加,行数一致的数字;
相加之和,替换2.txt第二列数字.
求大神帮忙!
windows还是linux?
windows下和linux下当然是用python最简单了(首先windows下得装一个python)
文件里面内容还这么乱,建议先把产生这个文件的程序输出改一改,改成容易处理的格式
https://www.cnblogs.com/wangyuebo/p/5907684.html
##代码
#! /bin/bash
f1=$1
f2=$2
a1=($(cat $f1 | grep -v '序号' | awk '{print $2}'| tr "\n" " "))
a2=($(cat $f2 | grep -v '序号' | awk '{print $2}'| tr "\n" " "))
echo "#序号 值" > $f2
for i in ${!a1[@]}; do echo "#$((i + 1)) $((a1[i] + a2[i]))" >> $f2; done
##运行
$ cat 1.txt
#序号 值
#1 1
#2 3
#3 5
#4 6
#5 4
#6 2
$ cat 2.txt
#序号 值
#1 2
#2 3
#3 5
#4 6
#5 4
#6 1
$ ./test.sh 1.txt 2.txt
$ cat 2.txt
#序号 值
#1 3
#2 6
#3 10
#4 12
#5 8
#6 3
这个问题有点意思,我尝试来回答下:
1、如果 filleB 只关注 " #kn vn" 的内容,其他注释类内容不需要保留的话,实现起来就比较简单,一行命令搞定
paste fileA fileB | grep "#[0-9]" | awk '{print $1 " " $2+$4}' > filleB
2、如果需要完整保留 file2 注释的话,那就再加两行搞定
grep -n "#[0-9]" fileA | cut -d ":" -f 1 > fileAtmp
paste fileA fileB | grep "#[0-9]" | awk '{print $1 " " $2+$4}' > fileBtmp
paste fileAtmp fileBtmp | awk '{cmd="sed -i \""$1"c " $2 " " $3"\" fileB"; system(cmd)}'