繁体   English   中英

了解SED命令

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM