2013-07-09 39 views
0

我的代碼如下所示;它不工作。用用戶提供的變量替換特定行sed

line_number=$1; 
variable1=$2; 

sed '\$line_number c\ 
\$variable1' file > tmp.out 

如果我寫下面那麼它的工作。你能建議我怎樣才能讓上面的代碼運行?

sed '1 c\ 
<replace>abc</replace>' file > tmp.out 

回答

1

就拿出來變量單引號的,你不需要變量之前任何反斜線:

sed "$line_number"' c\ 
'"$variable1" file > tmp.out 
+0

仍然無法正常工作。 O/P:sed:命令亂碼:\ 1 c \ –

+0

@PiyushBaijal,更新了答案 – perreal

+0

謝謝。有效。 –

1

你可以試試這個,

#!/bin/bash 

line_number=$1; 
variable1=$2; 

sed "$line_number c\ 
$variable1" file > tmp.out 

如果你想在sed中使用你的變量值,那麼你已經使用"(雙引號)。 然後,它將被shell解釋。