从两个唯一标签或注释之间的文件中提取内容并将其放入具有相同标签或注释的其他文件中?

时间:2021-06-16 06:09:37

标签: linux bash shell sed scripting

我有 2 个包含此内容的文件 test1.pytest2.py

test1.py :

#first

#endoffirst

#second

#endofsecond

#3rd

#endof3rd

test2.py :

#first
this is first command
#endoffirst

#second
this is second command
#endofsecond

#3rd
this is 3rd command
#endof3rd

我想首先检查 test2.py 文件并复制 #first#endoffirst 之间的内容并将其放入相同的标签中test1.py 文件,在 Linux 中使用 bash 脚本或其他操作。我的意思是,一个文件中两个唯一标签或命令之间的所有内容都应该复制并放在另一个文件中的相同标签或注释之间。

我已经用 sed 命令测试了很多东西,但我无法得到正确的答案。

我很感激任何人都可以帮助我

3 个答案:

答案 0 :(得分:1)

如果您只想使用 sed,我会分两次使用。

sed -n '/#first/,/#endoffirst/w tmp' test2.py
sed '/#first/,/#endoffirst/{
       /#endoffirst/!d;
       /#endoffirst/{ z; r tmp
     } }' test1.py

#first
this is first command
#endoffirst

#second

#endofsecond

#3rd

#endof3rd

奇怪的格式是因为如果您使用 r(或 w),那么文件名必须是该行其余部分的唯一内容。 sed 将在文件名中包含分号、空格、结束卷曲或几乎任何其他内容,但换行符除外。

我可能会使用 awk。这是一个笨拙的传球。

$: awk '/#first/,/#endoffirst/{
         if (NR == FNR) { x=x$0; if ($0 ~ "#endoffirst") { nextfile } else { x=x"\n" } }
         else           {        if ($0 ~ "#endoffirst") { print x; } }
         next } {print}' test2.py test1.py
#first
this is first command
#endoffirst

#second

#endofsecond

#3rd

#endof3rd

答案 1 :(得分:0)

from os import fdopen, remove
from tempfile import mkstemp
from shutil import copymode, move

try:
    start_flag_count = end_flag_count = 0
    content_dict = {}
    with open('/tmp/test2.py') as old_file:
        for line in old_file:
            ori_line = line
            line = line.strip()
            if line and line.startswith('#'):    # change '#' for your tags startswith
                if start_flag_count == end_flag_count:
                    cur_tag = line
                    start_flag_count += 1
                    mul_content_lines = ''
                elif start_flag_count == end_flag_count + 1:
                    content_dict[cur_tag] = mul_content_lines
                    end_flag_count += 1
            else:
                if start_flag_count == end_flag_count + 1:
                    mul_content_lines += ori_line

    ori_file = '/tmp/test1.py'
    fd, tmp_file_path = mkstemp()
    with fdopen(fd, 'w') as new_file:
        with open(ori_file) as old_file:
            for line in old_file:
                new_file.write(line)
                line = line.strip()
                if line and line.startswith('#') and line in content_dict:
                    new_file.write(content_dict[line])
    copymode(ori_file, tmp_file_path)
    remove(ori_file)
    move(tmp_file_path, ori_file)
except Exception as e:
    print str(e)

Python 2.7中运行,首先通过读取test2.py获取唯一的标签内容并保存到带有开始标志的字典中;然后扫描 test1.py 并在开始标志行后按标签添加内容。

答案 2 :(得分:0)

这可能就是您想要做的:

$ cat tst.awk
/^#/ {
    inBlock = !inBlock
    if ( inBlock ) {
        tag = $0
    }
}
NR == FNR {
    if ( inBlock ) {
        val[tag] = (tag in val ? val[tag] ORS : "") $0
    }
    next
}
$0 in val {
    print val[$0]
}
!inBlock

$ awk -f tst.awk test2.py test1.py
#first
this is first command
#endoffirst

#second
this is second command
#endofsecond

#3rd
this is 3rd command
#endof3rd
相关问题