![](/img/trans.png)
[英]Read from file all lines that have an index `n` lower than the lines that match a given regex
[英]print all lines from regex to end of file
長期潛伏,第一次來海報:)我必須寫一個代碼,其中有一部分我需要一個文件的所有行,從正則表達式開始到文件結尾
我的代碼:
if [ -z "$start" ]
then
if [ -z "$stop" ]
then
echo "all functions"
else
echo "from beginning till stop function"
sed -n "/$stop/I,\$!p" timings.txt > newtimings.txt
fi
else
if [ -z "$stop" ]
then
echo "start function to end "
sed -n "/$start/I,\$p" timings.txt > newtimings.txt
else
echo "start func to stop func"
sed -n "/$start/I,/$stop/Ip" timings.txt > newtimings.txt
fi
fi
我假設我的代碼行中有一個start值,而stop則為NULL,即第二個sed語句,該語句應從START regex打印,直到文件末尾似乎不起作用。 已經看過很多帖子,但仍然無法正常工作
問題在於,由於您的sed表達式用雙引號引起來,因此shell會擴展其中的所有變量。 結果,第二個sed
命令中的$p
被擴展為空字符串,因此sed
看到/startPattern/,
這是無效的。
嘗試像這樣逃逸美元:
sed -n "/$start/,\$p" timings.txt > newtimings.txt
或者,在$p
周圍使用單引號, $p
使外殼程序不展開它:
sed -n "/$start/,"'$p' timings.txt > newtimings.txt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.