linux – 移动行以跟随文件中的另一行
发布时间:2020-12-14 02:11:52 所属栏目:Linux 来源:网络整理
导读:我有一个文件在文件中有一行如下: check=('78905905f5a4ed82160c327f3fd34cba') 我希望能够移动此行以遵循如下所示的行: files=('somefile.txt') 该阵列虽然有时可以跨越多行,例如: files=('somefile.txt' 'file2.png' 'another.txt' 'andanother...')tex
我有一个文件在文件中有一行如下:
check=('78905905f5a4ed82160c327f3fd34cba') 我希望能够移动此行以遵循如下所示的行: files=('somefile.txt') 该阵列虽然有时可以跨越多行,例如: files=('somefile.txt' 'file2.png' 'another.txt' 'andanother...') text in between check=('78905905f5a4ed82160c327f3fd34cba' '5277a9164001a4276837b59dade26af2' '3f8b60b6fbb993c18442b62ea661aa6b') 数组/行总是以a)结尾,其间没有文本将包含一个闭括号. 我得到一些建议,awk可以做到这一点: awk '/files/{ f=0 print $0 for(i=1;i<=d;i++){ print a[i] } g=0 delete a # remove array after found next } /check/{ f=1; g=1 } f{ a[++d]=$0 } !g' file 这只会跨越一条线.我被告知要扩大搜索范围: awk '/source/ && /)$/{ f=0 print $0 for(i=1;i<=d;i++){ print a[i] } g=0 delete a # remove array after found next } /md5sum/ && /)$/{ f=1; g=1 } f{ a[++d]=$0 } !g' 刚学习awk所以我很感激你的帮助.或者,如果有其他工具可以做到这一点,我想听听它.有人告诉我’ed’这些类型的功能. 解决方法
首先回答你的上一个问题,是的,awk是典型的Unix工具,其他候选者是非常强大的
Perl ,Python ,或者……我最喜欢的…
Ruby . awk的一个优点是它总是存在;它是基础系统的一部分.解决此类问题的另一种方法是使用控制ed(1)或ex(1)的编辑器脚本.
好的,修改过的问题的新程序.该程序将根据需要向上或向下移动“检查”行,以便它们遵循“文件”行. BEGIN { checkAt = 0 filesAt = 0 scanning = 0 } /check=(/ { checkAt = NR scanning = 1 } /files=(/ { filesAt = NR scanning = 1 } /)$/ { if (scanning) { if (checkAt > filesAt) { checkEnd = NR } else { filesEnd = NR } scanning = 0 } } { lines[NR] = $0 } END { for (i = 1; i <= NR; ++i) { if (checkAt <= i && i <= checkEnd) { continue } print lines[i] if (i == filesEnd) { for (j = checkAt; j <= checkEnd; ++j) { print lines[j] } } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- linux – 运行autogen.sh时出现问题
- metrics – 用于显示修改,添加,删除SLOC和SLOC的Linux工具
- ConEmuC:Root进程活不到10秒,ExitCode = 0
- c – `libusb_attach_kernel_driver`无效
- 我是否需要rsync守护进程来利用rsync的差异复制功能?
- Linux搭建DNS服务器:CentOS7
- linux – shell:当var未设置或为null时,${var: – }的目的
- linux – 如何获取当前svn repo的url?
- linux – 用于Active Directory身份验证用户的主目录和shel
- 如何使用PUT方法通过cURL / PHP将JSON数据发送到API