Issue
Based on this question: Bash sed - find hashtags in string; with no solutions for this case (when you have special characters).
This question is well-researched and not a duplicate of this unrelated question as the referred doesn't covers all the asked topics (support to special characters and numbers; grep both between and after/before).
echo "Text and #hashtag" | grep -o '#[[:alpha:]]\+*' | tr -d '"'
works successfully, returning #hashtag
; that's still related to the mentioned question...
...About this new question with mine own needs (that can be useful to you), this is my version, parsing text between doublequotes instead of after hashtag:
echo '#first = "Yes"' | grep -o '"[[:alpha:]]\+*"' | tr -d '"'
and it works, returning Yes
.
However, when it have an emoji or other characters such as >
and /
(example: echo '#first = "✅ Yes"' | grep -o '"[[:alpha:]]\+*"' | tr -d '"'
) it returns an empty output.
It have to support any kind of character (emojis, html tags, numbers).
This should be useful not only for parsing between characters, but also after a character (such as parsing any #hashtag text) or before.
Solution
Thanks to @Aserre's pointings, I could come up with an answer.
In order for the "get every text when it appear AFTER a charater" and "get every text when it appear BETWEEN quotes" (grep) to work with any character, we have to replace [[:alpha:]]
in the block to ...
So, it is:
echo '#first = "✅ Yes"' | grep -o '"...\+"' | tr -d '"'
(get anything which is between double quotes)
and:
echo "Text and #hashtag" | grep -o '#...\+' | tr -d '"'
(get anything which is after a hashtag)
Update:
If you want to support things with only 1 character (such as numbers ranging from 0 to 9), replace ...
to .
(single dot)
It works, as in the question, for: emojis, letters, numbers and other special characters.
Answered By - dani 'SO learn value newbies' Answer Checked By - Katrina (WPSolving Volunteer)