我有一些文本文件,每个文件都包含一个单词的定义,如下所示:
word1
<TAB> some text
<TAB> some text
title 1
<TAB> some text
<TAB> some text
title 2
<TAB> some text
.
.
我想创建一个简单的 lingvo .DSL 字典,所以所需的输出应该是这样的:
word1
[m2][trn]
<TAB> some text
<TAB> some text
[b]title 1[/b]
<TAB> some text
<TAB> some text
[b]title 2[/b]
<TAB> some text
<TAB> some text
.
.
[/m2][/trn]
所以我需要做的是:
[m2][trn]
。[b]title[/b]
[/m2][/trn]
添加到文件末尾。任何帮助将不胜感激。
答案 0 :(得分:1)
我会说
sed '1! { /^[[:alnum:]]/ s/.*/[b]&[\/b]/; }; 1 s/$/\n[m2][trn]/; $ s/$/\n[\/trn][\/m2]/' filename
那是:
1! { /^[[:alnum:]]/ s/.*/[b]&[\/b]/; } # If the current line is not the first and
# starts with a letter or number, encase
# it in [b][/b]
1 s/$/\n[m2][trn]/ # If the current line is the first, put
# [m2][trn] behind it
$ s/$/\n[\/trn][\/m2]/ # If the current line is the last, put
# [/trn][/m2] behind it.
答案 1 :(得分:1)
这个sed命令应该这样做:
sed -e '1s/$/\n[m2][trn]/' \
-e '1!s/^[a-Z0-9].*/[B]&[\/B]/' \
-e '$s/$/\n[\/m2][\/trn]/' \
file
答案 2 :(得分:0)
<强>分析强>
在第一个单词后添加[m2] [trn]。
use a head splitter to handle the first line differently from the rest. and just printf this start tag.
如果一行以字母或数字(不是标签)开头,那么它就是一个标题,应该是[b]标题[/ b]
sed to search for lines starting with word characters \w
- 醇>
将[/ m2] [/ trn]添加到文件末尾。
printf to add end tag
示例脚本
head -n 1 input.txt 1>output.txt;
printf "[m2][trn]\n" 1>>output.txt;
tail -n +2 input.txt |
sed 's/^\(\w\+.\+\)/[b]\1[\/b]/g' 1>>output.txt;
printf "[/m2][/trn]\n" 1>>output.txt;
<强>输出强>
word1
[m2][trn]
some text
some text
[b]title 1[/b]
some text
some text
[b]title 2[/b]
some text
[/m2][/trn]