2013-01-22 11 views
0

我在寫一個csh腳本,它將從文件xyz中提取一行。 xyz文件包含一個no。代碼行和我感興趣的行出現在文件的2-3行之後。 我嘗試下面的代碼使用csh從文件中提取一行

set product1 = `grep -e '<product_version_info.*/>' xyz` 

我想這是一個方式,以便爲腳本找出線路,則應在一些變量線保存爲一個字符串&立即終止讀取文件即。它不應該在提取該行時進一步閱讀。

請幫忙!!

回答

2

grep具有-m--max-count標誌,告訴它匹配指定數量之後停止。希望你的grep版本支持它。

set product1 = `grep -m 1 -e '<product_version_info.*/>' xyz` 

從手冊頁上面鏈接:

-m NUM,--max數= NUM​​

Stop reading a file after NUM matching lines. If the input is 
    standard input from a regular file, and NUM matching lines are 
    output, grep ensures that the standard input is positioned to 
    just after the last matching line before exiting, regardless of 
    the presence of trailing context lines. This enables a calling 
    process to resume a search. When grep stops after NUM matching 
    lines, it outputs any trailing context lines. When the -c or 
    --count option is also used, grep does not output a count 
    greater than NUM. When the -v or --invert-match option is also 
    used, grep stops after outputting NUM non-matching lines. 

作爲替代方案,可以隨時以下命令只檢查前幾行(因爲它總是出現在前2-3行中):

set product1 = `head -3 xyz | grep -e '<product_version_info.*/>'` 
0

我想你要求返回文件中的第一個匹配行。如果是這樣,一個解決方案是將管grep結果head

set product1 = `grep -e '<product_version_info.*/>' xyz | head -1` 
+0

該解決方案也適用於我。 –

相關問題