我试图去掉URL中的最后一个斜杠,但前提是它存在。有什么想法吗?
tail -1 /var/script/string.txt | grep -oP '(?<=expanded_url":")[^"]+'
响应:
https://research.checkpoint.com/ramnits-network-proxy-servers/
所需输出:
https://research.checkpoint.com/ramnits-network-proxy-servers
您可以使用负前瞻(?:(?!/?").)+
来检查右边的内容不是可选的前斜杠和双引号。如果不是这样,那么取任意字符并重复该字符1次以上。
查看regex演示
例如:
echo 'expanded_url":"https://research.checkpoint.com/ramnits-network-proxy-servers/"' | grep -oP '(?<=expanded_url":")(?:(?!/?").)+'
结果
https://research.checkpoint.com/ramnits-network-proxy-servers
如果您对awk
满意,您也可以尝试以下操作。
var="https://research.checkpoint.com/ramnits-network-proxy-servers/"
echo $var | awk '{sub(//$/,"")} 1'
解释:现在添加对上述代码的解释,仅用于解释目的。
var="https://research.checkpoint.com/ramnits-network-proxy-servers/" ##Creating a variable in shell which has value as OP mentioned.
echo $var | awk '{sub(//$/,"")} 1' ##Sending echo output to awk command here. In awk command using sub to substitute / which comes in end of line with NULL.
##awk works on method of condition and action, so mentioning 1 is making condition TRUE and not mentioning any action so by default
##Printing of line/variable value will happen.
编辑:通过看到您试图再添加一个解决方案来避免此处的许多命令组合(这将只读取Input_file的最后一行,然后从命令中出来,因为我已经在其中放入了exit
(。
tac Input_file | awk 'FNR==1 && /xpanded_url":"/{sub(//$/,"");print;exit}'
EDIT2:在此处添加单个awk
命令,在某些awk
的END
块中,我们无法获取最后一行,因此我们可以添加两种解决方案,无论哪种方案对用户有效。
awk 'END{if($0 ~ /xpanded_url":"/){sub(//$/,"");print}}' Input_file
OR
awk '{val=$0}END{if(val ~ /xpanded_url":"/){sub(//$/,"",val);print val}}' Input_file