1

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
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
bugnet17
  • 173
  • 1
  • 1
  • 12

2 Answers2

1

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
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • @bugnet17, please try my EDIT command also once, if that helps you. – RavinderSingh13 Dec 16 '18 at 12:58
  • Thae `tac` could be avoided if you simply do the processing in the `END` block. – tripleee Dec 16 '18 at 13:18
  • @tripleee, yes I thought that too but I remember Ed's comment that some of awk do not support it, I will add it too now. Moreover we need NOT to read whole file in this case since I am exiting after reading last line. – RavinderSingh13 Dec 16 '18 at 13:20
  • @tripleee, sir, added both kind of `awk` in solution(one solution in which their awk support reading last line in end block and another where it do not). – RavinderSingh13 Dec 16 '18 at 13:25
1

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
The fourth bird
  • 154,723
  • 16
  • 55
  • 70