0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

全ての行の先頭に文字を挿入する

Posted at

 ファイルのフォーマットを合わせたりする際に先頭行に # だとか // だとかを挟むことが往々にしてあります。

 Vidual Studio Code や Atom などの正規表現機能付きテキストエディタで置換処理すれば大抵は済みますが挿入パターンが多いと大変になってきます。

 以下のコードは全ての行の先頭に様々な文字列を挿入する例です。

 先頭への挿入を行うシェルスクリプト

bindSeq.sh
# !/bin/bash
numLine=1
cat $1 | while read line
do
    sed "s/^/$line/g" $2 > temp/$numLine
    numLine=$((numLine + 1))
done

挿入する文字列リスト

patt.txt
<1>
(2)
[3]

挿入される元テキスト

Laziness: The quality that makes you go to great effort to reduce overall energy expenditure. It makes you write labor-saving programs that other people will find useful and document what you wrote so you don't have to answer so many questions about it.
Impatience: The anger you feel when the computer is being lazy. This makes you write programs that don't just react to your needs, but actually anticipate them. Or at least pretend to.
Hubris: The quality that makes you write (and maintain) programs that other people won't want to say bad things about.

実行例

入力

mkdir temp
./bindSeq.sh insert.txt original.txt

出力

temp/1
<1>Laziness: The quality that makes you go to great effort to reduce overall energy expenditure. It makes you write labor-saving programs that other people will find useful and document what you wrote so you don't have to answer so many questions about it.
<1>Impatience: The anger you feel when the computer is being lazy. This makes you write programs that don't just react to your needs, but actually anticipate them. Or at least pretend to.
<1>Hubris: The quality that makes you write (and maintain) programs that other people won't want to say bad things about.

temp/2
(2)Laziness: The quality that makes you go to great effort to reduce overall energy expenditure. It makes you write labor-saving programs that other people will find useful and document what you wrote so you don't have to answer so many questions about it.
(2)Impatience: The anger you feel when the computer is being lazy. This makes you write programs that don't just react to your needs, but actually anticipate them. Or at least pretend to.
(2)Hubris: The quality that makes you write (and maintain) programs that other people won't want to say bad things about.

temp/3
[3]Laziness: The quality that makes you go to great effort to reduce overall energy expenditure. It makes you write labor-saving programs that other people will find useful and document what you wrote so you don't have to answer so many questions about it.
[3]Impatience: The anger you feel when the computer is being lazy. This makes you write programs that don't just react to your needs, but actually anticipate them. Or at least pretend to.
[3]Hubris: The quality that makes you write (and maintain) programs that other people won't want to say bad things about.

 こういう些細な操作に時間をとられないようにしたいですね。

 以上です。ありがとうございました。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?