linux 中 实现数据中指定的行的内容批量替换

 

001、

复制代码
[root@PC1 test4]# ls
a.txt  coordinate.txt
[root@PC1 test4]# cat a.txt                                     ## 测试数据
dd ff sd 1
ss aa ee 2
xx vv zz 3
ss ww rr 4
aa ff jj 5
nn mm kk 6
ss rr uu 7
yy uu ee 8
ww rr 88 9
[root@PC1 test4]# cat coordinate.txt                          ## 替换的数据
3 MMM 7777
5 GGG 3333
8 QQQ 2222
[root@PC1 test4]# cp a.txt a.txt.bak
[root@PC1 test4]# awk '{if(NR == 1) {print $1 - 1, $0; a=$1} else {print $1 - a - 1, $0; a=$1}}' coordinate.txt    ## 生成打印空行的索引
2 3 MMM 7777
1 5 GGG 3333
2 8 QQQ 2222
[root@PC1 test4]# awk '{if(NR == 1) {print $1 - 1, $0; a=$1} else {print $1 - a - 1, $0; a=$1}}' coordinate.txt > coordinate2.txt     ## 将打印空行的索引保存为文件
[root@PC1 test4]# cat coordinate2.txt
2 3 MMM 7777
1 5 GGG 3333
2 8 QQQ 2222
[root@PC1 test4]# cat coordinate2.txt | while read {i,j,k}; do seq $i | while read m; do echo "" >> temp; done; echo $k >> temp; done     ## 打印出空行
[root@PC1 test4]# ls
a.txt  a.txt.bak  coordinate2.txt  coordinate.txt  temp
[root@PC1 test4]# cat temp


MMM 7777

GGG 3333


QQQ 2222
复制代码

 

 

002、

复制代码
[root@PC1 test4]# ls
a.txt  a.txt.bak  coordinate2.txt  coordinate.txt  temp
[root@PC1 test4]# cat a.txt             ## 测试数据
dd ff sd 1
ss aa ee 2
xx vv zz 3
ss ww rr 4
aa ff jj 5
nn mm kk 6
ss rr uu 7
yy uu ee 8
ww rr 88 9
[root@PC1 test4]# cat temp         ## 替换的数据


MMM 7777

GGG 3333


QQQ 2222
[root@PC1 test4]# cat coordinate2.txt
2 3 MMM 7777
1 5 GGG 3333
2 8 QQQ 2222
[root@PC1 test4]# awk '{print ""}' a.txt | paste - temp | sed 's/^\s\+//' | paste - -d "&" a.txt | sed 's/^&//' | sed 's/&.*//'     ## 替换脚本
dd ff sd 1
ss aa ee 2
MMM 7777
ss ww rr 4
GGG 3333
nn mm kk 6
ss rr uu 7
QQQ 2222
ww rr 88 9
复制代码

 

posted @   小鲨鱼2018  阅读(403)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
历史上的今天:
2021-12-25 利用GEMMA进行GWAS分析
2020-12-25 C语言获取数值的最后几位数
点击右上角即可分享
微信分享提示