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
分类:
linux shell
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
2021-12-25 利用GEMMA进行GWAS分析
2020-12-25 C语言获取数值的最后几位数