2009-07-28 8 views
1

sedを使用してファイルの最初の行にテキストを挿入しようとしました。 私はこれをsh スクリプトの中で行います。シェルスクリプト内でSedを使ってファイルにテキストを挿入

しかし、なぜsed実行の行でハングするのですか?

#! /bin/sh 

# Command to execute 
# ./mybashcode.sh test.nbq 

nbqfile=$1 
nbqbase=$(basename $nbqfile nbq) 
taglistfiletemp="${nbqbase}taglist_temp" 
taglistfile="${nbqbase}taglist" 


./myccode $nbqfile | 
sort | 
uniq -c | 
awk '{print $2}' > $taglistfiletemp 

noftags=$(wc -l $taglistfiletemp | awk '{print $1}') 
echo $noftags 


# We want to append output of noftags 
# to the first line of taglistfile 

sed '1i\ 
$noftags' > $taglistfile 

# why it hangs here 
# the content of taglistfile is NIL 

答えて

6

したいスクリプト(通常検索/を交換)し、データがそれを実行するために、私はあなたがsedでやろうとしているかわからないが、それは2つの入力を必要とします。 1つだけを指定すると、正規表現を取得したとみなし、stdinのデータを待機します。 stdinで何も提供していないので、無期限にハングアップします。

さらに、「$noftags」ではなく「$noftags」となります。前者は$noftagsを出力し、後者は変数の内容を出力します。一重引用符は変数展開を許可しません。

+0

@MA:ありがとうございます。確かに私はsedのための標準を見逃した。 – neversaint

+1

別の問題は、doubleの代わりに一重引用符を使用していて、環境変数の拡張を妨げている可能性があります。したがって、あなたは文字通り '$ noftags'を追加しています –

+0

@DJ:そうです、ありがとうございます。 – neversaint

2

ここに何か問題がありますか?
または、別のファイルの先頭にテキストを挿入するだけですか?

# $NewInitialText 
# $fileToInsertInto 
echo $NewInitialText > temp.file.txt 
cat $fileToInsertInto >> temp.file.txt 
mv temp.file.txt $fileToInsertInto 

sedより簡単ですか? - Punは私が推測することを意図しました。

+0

言い伝えをしています:D –

2

sedに入力ファイルを忘れたためにハングします。

.... 
... 
sed -i.bak "1i $noftags" $taglistfile 
... 
+0

私は今日、この最高の答え、IMOを使いました。 '$> sudo sed -i" 1i/usr/local/bin/etc/path'にあります。 – Droogans

関連する問題