gpt4 book ai didi

linux - 将目录中每个文件的标签之间的文件内容提取到新位置

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:21:57 25 4
gpt4 key购买 nike

假设我在目录 location1/bar 中有一个名为“foo1.txt,v”的文件,其中包含包含在起始标记和结束标记中的所需文本

@@
text
@<content>
@

我的目录栏包含"foo1.txt,v""foo2.txt,v"......"fooN.txt,v"

我想从每个文件中获取标签@@\ntext\n@​​和\n@\n之间的内容并保存在location2/bar with filename foo1.txt foo2.txt ...... fooN.txt

即将文件夹结构复制到另一个位置,同时从文件名中删除“,v”,新文件仅包含明文

最佳答案

这个 awk 一行应该做的:

 awk '/^@@/{p=1;next}/^@$/{p=0}p{f=FILENAME;sub(/,v$/,"",f);print > "/loc2/bar/"f}' loc1/foo/*

小测试:

kent@7pLaptop:/tmp/test/foo$ head *
@@text@
xxx
@

==> foo2.txt,v <==
@@text@
yyy
@
kent@7pLaptop:/tmp/test/foo$ ls -l /tmp/test/bar
total 0
kent@7pLaptop:/tmp/test/foo$ awk '/^@@/{p=1;next}/^@$/{p=0}p{f=FILENAME;sub(/,v$/,"",f);print > "/tmp/test/bar/"f}' *
kent@7pLaptop:/tmp/test/foo$ head /tmp/test/bar/*
==> /tmp/test/bar/foo1.txt <==
xxx

==> /tmp/test/bar/foo2.txt <==
yyy
kent@7pLaptop:/tmp/test/foo$

关于linux - 将目录中每个文件的标签之间的文件内容提取到新位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20134121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com