2014-09-23 22 views
0

我想通過替換值來檢索「busfile」文件n列SED功能得到錯誤的「i」上的惠普試圖在在使用上HP UNIX箱

下面的代碼工作在RedHat Linux,精UNIX我收到提示

"sed: Function {i}{p} cannot be parsed." 

這裏是我的代碼

acList=/z/temp/busfile 
i=1 
temp1=`sed -n "{i}{p}" $acList` 
echo $temp1 

更新:

即使按照某些答案中的建議添加$,我仍然遇到同樣的問題。

temp1=`sed -n "${i}{p}" $acList` 
+0

你不是應該使用'$ i'?嘗試'sed -n「$ {i} p」' – 2014-09-23 07:41:33

+0

@Jidder我想通過替換「i」 – 2014-09-23 07:43:24

+0

@ user215827中的值來從「busfile」文件中檢索第n列,這將打印特定的_line._如果您想打印一列,'awk'可能更好 - 請參閱我的答案。 – paxdiablo 2014-09-23 07:54:07

回答

0

如果你想使用i變量來打印線,你需要$它前面:

temp1=`sed -n "${i}p" $acList` 

根據以下成績單:

pax> i=3 
pax> echo 'a 
...> b 
...> c 
...> d 
...> e 
...> f 
...> g' | sed -n "${i}p" 
c 

在這種情況下,我傾向於首先嚐試最簡單的解決方案,然後逐漸增加複雜性,直到失敗。

的第一步是創建一個通過四個四線文件(稱爲myfile)的話來說:

one 
two 
three 
four 

然後嘗試使用各種命令,在不斷增加的複雜性:

sed -n "p" myfile       # Print all lines. 
sed -n "3p" myfile       # Print hard-coded line. 
i=3 ; sed -n "${i}p" myfile    # Print line with parameter. 
i=3 ; x=`sed -n "${i}p" myfile` ; echo $x # Capture line with parameter. 

在某個時候,它會有希望「突破」,然後您可以更集中地針對您的調查。


不過,我懷疑這是不必要在這裏,因爲你的本意是使用該命令的提取不正確。如果你想打印一列而不是一條線,然後awk可能是作業的更好的工具:

pax> i=5 
pax> echo 'pax is a really great guy' | awk -vf=$i '{print $f}' 
great 
+0

我也試過這個,仍然有錯誤 sed:功能1 {p}無法解析。 請注意:我使用hp-unix – 2014-09-23 07:54:09

+0

@ user215827,如果它無法解析'1p',那麼它很奇怪。但是,無論如何,你想要列的命令是'awk'而不是'sed'。 – paxdiablo 2014-09-23 08:02:57

+0

@ user215827不知道它會有什麼區別,但你爲什麼要把'p'放在括號'{p}'中? – 2014-09-23 08:07:55

0

您可以使用:

acList=/z/temp/busfile 
i=1 
temp1=`sed -n $i'p' $acList` 
echo "$temp1"