Reputation: 181
I try to get rid of the last slash in the URL, but only if it exists. any ideas?
tail -1 /var/script/string.txt | grep -oP '(?<=expanded_url":")[^"]+'
response:
https://research.checkpoint.com/ramnits-network-proxy-servers/
The desired output:
https://research.checkpoint.com/ramnits-network-proxy-servers
Upvotes: 1
Views: 59
Reputation: 133538
If you are ok with awk
you could try following too.
var="https://research.checkpoint.com/ramnits-network-proxy-servers/"
echo $var | awk '{sub(/\/$/,"")} 1'
Explanation: Adding explanation for above code now, only for explanation purposes.
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.
EDIT: By seeing your attempt trying to add 1 more solution to avoid many commands combinations here(this will read only last line of Input_file and come out of command then since I have put exit
in it).
tac Input_file | awk 'FNR==1 && /xpanded_url\":\"/{sub(/\/$/,"");print;exit}'
EDIT2: Adding single awk
command here, in some awk
at its END
block we can't fetch last line in some we can so adding both kind of solutions whichever works for people.
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
Upvotes: 1
Reputation: 163362
You could use a negative lookahead (?:(?!/?").)+
to check what is on the right is not an optional forward slash followed by a double quote. If that is not the case, then take any character and repeat that 1+ times.
See the regex demo
For example:
echo 'expanded_url":"https://research.checkpoint.com/ramnits-network-proxy-servers/"' | grep -oP '(?<=expanded_url":")(?:(?!/?").)+'
Result
https://research.checkpoint.com/ramnits-network-proxy-servers
Upvotes: 1