![如何添加一条额外的行,该行在包含上一行中的模式的文件中也有一个模式?](https://linux22.com/image/124692/%E5%A6%82%E4%BD%95%E6%B7%BB%E5%8A%A0%E4%B8%80%E6%9D%A1%E9%A2%9D%E5%A4%96%E7%9A%84%E8%A1%8C%EF%BC%8C%E8%AF%A5%E8%A1%8C%E5%9C%A8%E5%8C%85%E5%90%AB%E4%B8%8A%E4%B8%80%E8%A1%8C%E4%B8%AD%E7%9A%84%E6%A8%A1%E5%BC%8F%E7%9A%84%E6%96%87%E4%BB%B6%E4%B8%AD%E4%B9%9F%E6%9C%89%E4%B8%80%E4%B8%AA%E6%A8%A1%E5%BC%8F%EF%BC%9F.png)
我有一个文件——
something
\title{\hypertarget{A64L}{A64L(3)}}
something
\title{\hypertarget{MALLOC}{MALLOC(3)}}
something
\title{\hypertarget{STRCMP}{STRCMP(3)}}
in 中的单词{}
可以是任何内容,但在 Continuous 中是相同的{}
。
我想要得到
something
\title{\hypertarget{A64L}{A64L(3)}}
\addcontentsline{A64L}
something
\title{\hypertarget{MALLOC}{MALLOC(3)}}
\addcontentsline{MALLOC}
something
\title{\hypertarget{STRCMP}{STRCMP(3)}}
\addcontentsline{STRCMP}
我尝试跟随,但失败了。
sed -e /\\\\title\{\\\\hypertarget\{.*\}\{.*\(3\)\}\}/a\\\\\\addcontentsline\{\&\} filename
这是一个扩展https://tex.stackexchange.com/questions/408244/how-to-make-toc-without-having-any-section-parts-name
答案1
我不确定你到底想捕捉什么,但是
$ sed 's/\\title{\\hypertarget\({[^}]*}\).*/&\n\\addcontentsline\1/' file
something
\title{\hypertarget{A64L}{A64L(3)}}
\addcontentsline{A64L}
something
\title{\hypertarget{MALLOC}{MALLOC(3)}}
\addcontentsline{MALLOC}
something
\title{\hypertarget{STRCMP}{STRCMP(3)}}
\addcontentsline{STRCMP}
答案2
j=`grep -o "{[0-9A-Z]\{4\}}" k1.txt | sed -r "s/\s+//g" | sed '/^$/d'`;
grep -B1 "$j" k1.txt | sed "/title/s/.*/&\n\\\addcontentsline$j/g";
for i in `grep -o "{[A-Z]\{6\}}" k1.txt`; do
grep -B1 $i k1.txt| sed "/title/s/.*/&\n\\\addcontentsline$i/g";
done
输出:
something
\title{\hypertarget{A64L}{A64L(3)}}
\addcontentsline{A64L}
something
\title{\hypertarget{MALLOC}{MALLOC(3)}}
\addcontentsline{MALLOC}
something
\title{\hypertarget{STRCMP}{STRCMP(3)}}
\addcontentsline{STRCMP}