Shell-Script
如何在包含前一行模式的文件中添加一個額外的行?
我有一個文件-
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
我不確定您要擷取的確切內容,但是
$ 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}
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}