次のような内容のテキストファイルがあります。
body
font-size: 12px
color: blue
td
font-size: 14px
...
;
を含む行に:
を追加したいので、内容は次のようになります。
body
font-size: 12px;
color: blue;
td
font-size: 14px;
...
Linuxでこれを行う最も簡単な方法は何ですか?
正規表現の置換を使用します。Vimを含む多くのエディターは正規表現をサポートしています。
Sed(Stream EDitor)を使用して、コマンドラインから実行する方法は次のとおりです。
sed -i -e "s/.*:.*/&;/" INPUT_FILE.css
Sedの一部のバージョンは、インプレース編集(出力ファイルを入力ファイルに書き込む)をサポートしていません。
sed -e "s/.*:.*/&;/" INPUT_FILE.css > OUTPUT_FILE.css
説明:
sed invoke Stream EDitor commmand line tool
-i edit in-place
-e the next string will be the regular expression: s/.*:.*/&;/
INPUT_FILE.css the name of your text file
正規表現(RegEx)は、詳細に説明されています。
s RegEx command indicates substitution
/ RegEx delimiter: separates command and match expression
.* any string followed by...
: a colon character followed by...
.* any string
/ RegEx delimiter: separates match expression and replacement expression
& RegEx back reference, entire string that was matched by match expression
; the semicolon you wish to add
/ RegEx delimiter: ends replacement expression
Vim、または正規表現を適切にサポートするその他のエディター
:%s/\(:.*\)$/\1;/
ExモードでVimを使用できます:
ex -sc 'g/:/s/$/;/' -cx file
g
グローバル検索
s
代替
$
行の終わり
x
保存して閉じる