[英]understanding SED commands
我需要了解一個Shell代碼,該代碼使用以下命令來使用GOOGLE MAPS API從源到目的地獲取路線:
wget --no-parent -O - https://maps.googleapis.com/maps/api/directions/json?origin=$begin\&destination=$finish\&sensor=false > new.txt
接下來,我們獲取輸出的以下行:
**"html_instructions" : "Head \u003cb\u003enorthwest\u003c/b\u003e"**
grep -n html_instructions new.txt > new1.txt
有人可以告訴我使用的含義:
sed -e 's/\\u003cb//g'
在下面的命令中:
sed -e 's/\\u003cb//g' -e 's/\\u003e//g' -e 's/\\u003c\/b//g' -e 's/\\u003c//g' -e 's/div.*div//g' -e 's/.*://g' -e 's/"//g' -e 's/ "//g' new1.txt > new2.txt
僅輸出
Head northwest
。
提前致謝!
sed -e 's/\\u003cb//g' -e 's/\\u003e//g' -e 's/\\u003c\/b//g' -e 's/\\u003c//g' -e 's/div.*div//g' -e 's/.*://g' -e 's/"//g' -e 's/ "//g' new1.txt > new2.txt
每個
-e
后面的字符串是sed
命令。 sed命令
s/\\\搜索所有出現的Unicode字符003CB(這是帶有Dialytika的希臘小寫字母upsilon ),然后將其替換為空 。 換句話說,它將字符串中的字符刪除。
因此,該
sed
命令從行和new1.txt中刪除每次出現的unicode字符003cb,u003e和u003c,並將輸出發送到new2.txt。
另外,
s/div.*div//g
導致所有以“ div”開頭和結尾的字符串都將被刪除。 命令s/.*://g
從行的開頭到行的最后一個冒號刪除所有文本。 s/"//g
刪除所有出現的雙引號字符。s s/ "//g
刪除所有出現的空格和雙引號。
通常,
sed
命令s/new/old/
搜索第一次出現的new並將其替換為old。 用g
在末尾附加,如在s/new/old/g
,它使全球取代:尋找新的每次出現,並與舊的替換它。 給這些命令增加很多功能, new
可能是一個正則表達式。 考慮s/.*:
g . The dot character has the special meaning of "any character at all". The star character means zero or more of the preceding character. Thus the regular expression
. The dot character has the special meaning of "any character at all". The star character means zero or more of the preceding character. Thus the regular expression
. The dot character has the special meaning of "any character at all". The star character means zero or more of the preceding character. Thus the regular expression
。*:`表示零個或多個任何字符,后跟一個冒號。
您可以使用awk
:
awk -F\" '/html_instructions/ {gsub(/(\\u003(c|cb|e)|\/b)/,x);print $4}'
Head northwest
所以整行應該是:
wget --no-parent -O - https://maps.googleapis.com/maps/api/directions/json?origin=$begin\&destination=$finish\&sensor=false | awk -F\" '/html_instructions/ {gsub(/(\\u003(c|cb|e)|\/b)/,x);print $4}'
Head northwest
把它變成一個變量
d=$(wget --no-parent -O - https://maps.googleapis.com/maps/api/directions/json?origin=$begin\&destination=$finish\&sensor=false | awk -F\" '/html_instructions/ {gsub(/(\\u003(c|cb|e)|\/b)/,x);print $4}')
echo $d
Head northwest
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.