当前位置: 技术问答>linux和unix
求一个多文件编辑的shell
来源: 互联网 发布时间:2017-03-10
本文导语: 现在有以下几个文件 1、a.txt aa.dat 381263 bb.dat 515821 2、b.dat CLASS 20120622192745 N GRADE COUNT 3、aa.bat aa 20120107000000000 19950427000000 20120107000000 01001500 aa 20120107000000000 19950427000000 20120107000000 01001500 aa 20120107000000000 19950427...
现在有以下几个文件
1、a.txt
aa.dat 381263
bb.dat 515821
2、b.dat
CLASS 20120622192745 N GRADE COUNT
3、aa.bat
aa 20120107000000000 19950427000000 20120107000000 01001500
aa 20120107000000000 19950427000000 20120107000000 01001500
aa 20120107000000000 19950427000000 20120107000000 01003640
aa 20120107000000000 19950427000000 20120107000000 01003640
aa 20120107000000000 20060124000000 20120107000000 02080011
aa 20120107000000000 20060124000000 20120107000000 02080011
aa 20120107000000000 20051216000000 20120107000000 02080012
4、bb.dat
bb 20120107000000000 19950427000000 20120107000000 01001500
bb 20120107000000000 19950427000000 20120107000000 01001500
bb 20120107000000000 19950427000000 20120107000000 01003640
bb 20120107000000000 19950427000000 20120107000000 01003640
bb 20120107000000000 20060124000000 20120107000000 02080011
bb 20120107000000000 20060124000000 20120107000000 02080011
bb 20120107000000000 20051216000000 20120107000000 02080012
求一个shell,在a.txt文件中取每个dat对应的数字,替换b.dat文件中的COUNT,再把替换后的b.dat文件内容,加到aa.dat,bb.dat文件的最前面。(文件字段间用tab分割)谢谢大家啊~~~~
生成后的文件:
1、aa.bat
CLASS 20120622192745 N GRADE 381263
aa 20120107000000000 19950427000000 20120107000000 01001500
aa 20120107000000000 19950427000000 20120107000000 01001500
aa 20120107000000000 19950427000000 20120107000000 01003640
aa 20120107000000000 19950427000000 20120107000000 01003640
aa 20120107000000000 20060124000000 20120107000000 02080011
aa 20120107000000000 20060124000000 20120107000000 02080011
aa 20120107000000000 20051216000000 20120107000000 02080012
2、bb.dat
CLASS 20120622192745 N GRADE 515821
bb 20120107000000000 19950427000000 20120107000000 01001500
bb 20120107000000000 19950427000000 20120107000000 01001500
bb 20120107000000000 19950427000000 20120107000000 01003640
bb 20120107000000000 19950427000000 20120107000000 01003640
bb 20120107000000000 20060124000000 20120107000000 02080011
bb 20120107000000000 20060124000000 20120107000000 02080011
bb 20120107000000000 20051216000000 20120107000000 02080012
1、a.txt
aa.dat 381263
bb.dat 515821
2、b.dat
CLASS 20120622192745 N GRADE COUNT
3、aa.bat
aa 20120107000000000 19950427000000 20120107000000 01001500
aa 20120107000000000 19950427000000 20120107000000 01001500
aa 20120107000000000 19950427000000 20120107000000 01003640
aa 20120107000000000 19950427000000 20120107000000 01003640
aa 20120107000000000 20060124000000 20120107000000 02080011
aa 20120107000000000 20060124000000 20120107000000 02080011
aa 20120107000000000 20051216000000 20120107000000 02080012
4、bb.dat
bb 20120107000000000 19950427000000 20120107000000 01001500
bb 20120107000000000 19950427000000 20120107000000 01001500
bb 20120107000000000 19950427000000 20120107000000 01003640
bb 20120107000000000 19950427000000 20120107000000 01003640
bb 20120107000000000 20060124000000 20120107000000 02080011
bb 20120107000000000 20060124000000 20120107000000 02080011
bb 20120107000000000 20051216000000 20120107000000 02080012
求一个shell,在a.txt文件中取每个dat对应的数字,替换b.dat文件中的COUNT,再把替换后的b.dat文件内容,加到aa.dat,bb.dat文件的最前面。(文件字段间用tab分割)谢谢大家啊~~~~
生成后的文件:
1、aa.bat
CLASS 20120622192745 N GRADE 381263
aa 20120107000000000 19950427000000 20120107000000 01001500
aa 20120107000000000 19950427000000 20120107000000 01001500
aa 20120107000000000 19950427000000 20120107000000 01003640
aa 20120107000000000 19950427000000 20120107000000 01003640
aa 20120107000000000 20060124000000 20120107000000 02080011
aa 20120107000000000 20060124000000 20120107000000 02080011
aa 20120107000000000 20051216000000 20120107000000 02080012
2、bb.dat
CLASS 20120622192745 N GRADE 515821
bb 20120107000000000 19950427000000 20120107000000 01001500
bb 20120107000000000 19950427000000 20120107000000 01001500
bb 20120107000000000 19950427000000 20120107000000 01003640
bb 20120107000000000 19950427000000 20120107000000 01003640
bb 20120107000000000 20060124000000 20120107000000 02080011
bb 20120107000000000 20060124000000 20120107000000 02080011
bb 20120107000000000 20051216000000 20120107000000 02080012
|
应该是aa.dat吧?不然那样子似乎就不对应了。
$ awk '{if(NR==FNR){insert=$0}else{to_insert=insert;sub("COUNT", $2, to_insert);cmd="sed -i.bak "1i"to_insert"" "$1;system(cmd)}}' b.txt a.txt
$ awk '{if(NR==FNR){insert=$0}else{to_insert=insert;sub("COUNT", $2, to_insert);cmd="sed -i.bak "1i"to_insert"" "$1;system(cmd)}}' b.txt a.txt
|
主要用sed命令吧