2

I want to extract sentences which start with

https://www.instagram.com/p/

and end with

/

For example, I want to extract the following without the x's

××××××https://www.instagram.com/p/BRhNDg5jne7/××××××××

I have already tried

grep "https://www.instagram.com/p/*/"

However, it is not working.

4 Answers 4

1

Try the following regular expression, https://www.instagram.com/p/[^/]\+/

#!/bin/bash
data="××××××https://www.instagram.com/p/BRhNDg5jne7/××××××××"
echo "$data" | grep -o 'https://www.instagram.com/p/[^/]\+/'

The magic part is [^/]\+/, it grabs everything up to and including the next forward slash.

Sample output from the above script.

zb@server ~ $ ./tmp.sh 
https://www.instagram.com/p/BRhNDg5jne7/
0
1

Using grep :

echo "××××××https://www.instagram.com/p/BRhNDg5jne7/××××××××"  | grep -Po "(?s)(http(.*?)(\/p\/.*\/|\/\Z))"

output:

https://www.instagram.com/p/BRhNDg5jne7/
0
0

no need of perl regex You can try :

grep -o "https://www.instagram.com/.*/"
1
  • 1
    You probably want a non-greedy match, or something like https://www.instagram.com/foo/ bar baz other stuff/ will match the entire thing. You can do it by passing -P and changing .* to .*? Aug 6, 2018 at 19:04
0

EDIT: since the question had some changes since I posted my answer, so did my understanding of it.

If all your rows have the pattern xxxx, then all you gotta do is a regex replace with sed. I.e.:

sed 's/xxxx*//g'

If you first need to grep the rows, then pipe sed after grep. I.e.:

grep "https://www.instagram.com/p/" | sed 's/xxxx*//g'

Depending of the real pattern you have, this approach may or may not be of use.

2
  • hope you are removing, instead of printing.
    – Siva
    Aug 6, 2018 at 17:27
  • @SivaPrasath please see my edited answer. Aug 6, 2018 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.