2016-12-21 75 views
1

我想從shell var中提取子字符串。這是一個例子:如何在bash中獲取子字符串變量

[email protected]:~$ var=$(curl -s 'https://www.youtube.com/') 
[email protected]:~$ echo $var|grep -ob GetBrowse_rid 
[email protected]:~$ 50000:GetBrowse_rid 
[email protected]:~$ echo $var|grep -ob what_to_watch 
[email protected]:~$ 55555:what_to_watch 
[email protected]:~$ echo ${var:50000:55555} 

我想提取文本beetwen 「GetBrowse_rid」 和 「what_to_watch」,但由於某些原因,我的代碼不工作

+0

它做了什麼,以及你期望的是什麼? – chepner

回答

0

您可以使用sed

curl -s 'https://www.youtube.com/' | sed -n 's/.*\(GetBrowse_rid.*what_to_watch\).*/\1/p' 

GetBrowse_rid":"0xd43def10f9d14420","yt_fn":"what_to_watch 
+1

作品!!!!我失去了2天。謝謝anubhava – mike

相關問題