简体   繁体   English

了解SED命令

[英]understanding SED commands

I need to understand a shell code which uses the following command to fetch directions from a source to destination using GOOGLE MAPS API: 我需要了解一个Shell代码,该代码使用以下命令来使用GOOGLE MAPS API从源到目的地获取路线:

wget --no-parent -O - https://maps.googleapis.com/maps/api/directions/json?origin=$begin\&destination=$finish\&sensor=false > new.txt

Next we fetch the following line of the output: 接下来,我们获取输出的以下行:

**"html_instructions" : "Head \u003cb\u003enorthwest\u003c/b\u003e"**

grep -n html_instructions  new.txt > new1.txt

Can somebody please tell me the meaning of using: 有人可以告诉我使用的含义:

sed -e 's/\\u003cb//g'

etc in the following command: 在下面的命令中:

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

Which outputs Head northwest only. 仅输出Head northwest

Thanks in advance! 提前致谢!

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

The string after each -e is a sed command. 每个-e后面的字符串是sed命令。 The sed command s/\\\ searches for all occurrences of the unicode character 003CB (which is a greek small letter upsilon with dialytika ) and replaces it with nothing. sed命令s/\\\搜索所有出现的Unicode字符003CB(这是带有Dialytika的希腊小写字母upsilon ),然后将其替换为空 In other words, it remove the character from the string. 换句话说,它将字符串中的字符删除。

Thus, that sed command removes every occurrence of unicode characters 003cb, u003e, and u003c from the lines and new1.txt and sends the output to new2.txt. 因此,该sed命令从行和new1.txt中删除每次出现的unicode字符003cb,u003e和u003c,并将输出发送到new2.txt。

Additionally, s/div.*div//g causes any string that begins and ends with "div" to be removed. 另外, s/div.*div//g导致所有以“ div”开头和结尾的字符串都将被删除。 The command s/.*://g removes any text from the beginning of the line to the last colon in the line. 命令s/.*://g从行的开头到行的最后一个冒号删除所有文本。 s/"//g removes the every occurence of the double-quote character. s/ "//g removes every occurrence of space followed by double-quote. s/"//g删除所有出现的双引号字符。s s/ "//g删除所有出现的空格和双引号。

In general, the sed command s/new/old/ searches for the first occurrence of new and replaces it with old. 通常, sed命令s/new/old/搜索第一次出现的new并将其替换为old。 With a g appended at the end, as in s/new/old/g , it makes the substitution globally: looks for every occurrence of new and replaces it with old. g在末尾附加,如在s/new/old/g ,它使全球取代:寻找新的每次出现,并与旧的替换它。 Adding a lot of power to these commands, new may be a regular expression. 给这些命令增加很多功能, new可能是一个正则表达式。 Consider 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 考虑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 .*:` means zero or more of any characters followed by a colon. . 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 。*:`表示零个或多个任何字符,后跟一个冒号。

You can take all in one go with awk : 您可以使用awk

awk -F\" '/html_instructions/ {gsub(/(\\u003(c|cb|e)|\/b)/,x);print $4}'
Head northwest

So whole line should be: 所以整行应该是:

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

to get it into a variable 把它变成一个变量

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