My goal is to take text from a standard .txt document, and use (preferably) Terminal or Applescript to extract text between the symbols ▶
and •
. While there is only one ▶
symbol there are multiple bullet points, so being able to extract data between only the first bullet is vital. I am partially educated in the ways of bash, so explanation would help me out a lot, otherwise I might screw up. TIA, Jake
-
1Welcome to Ask Different. While I am not entirely convinced this question isn't off topic, I can tell you it is quite unclear as to what you are asking. You want to extract text between symbols? Where would this text go? If it has to go from a "▶" to a "•", but there are multiple "•", what exactly does the script do with that conflict? What do you want to happen? At minimum, you should clarify what you want even if this question does get migrated to another site.– AllanCommented May 12, 2016 at 23:43
-
Scripting questions are on-topic. Until the OP amends the question, let's assume they want to have the extracted text in a variable or the clipboard and that text should be extracted up to the first bullet only (it says so in the text actually).– nohillside ♦Commented May 13, 2016 at 6:08
Add a comment
|
1 Answer
This runs with Bash, but it evokes some Ruby code:
ruby - <<EOF
puts /▶[^•]*•/.match("▶the first •2•3•4•5•666643")
EOF
If you decide to use AppleScript, you have a lot of options for what to match against:
on run {input, parameters}
set var to "▶the first •2•3•4•5•666643"
set output to (do shell script ¬
"ruby - <<EOF
puts /▶([^•]*)•/.match('" & var & "')[1]
EOF")
return output
end run
Alternatively, sed can be used, which is easier for working with files:
sed -E 's/.*▶([^•]*)•.*/\1/' filename.txt
-
thank you for the response. I am just confused about where to put the path for the file we would be extracting the text from. Once again, I am sorry, but i have little experience with anything other than bash. Thanks Again! Here is an example document of which i would be extracting data from.– JakeCommented May 13, 2016 at 5:58
-
@Jake No problem. I translated it into sed, so it should be easier to do file things now.– LaurelCommented May 13, 2016 at 15:55
-
Do you realize that the output of your
ruby
command andsed
command, against the same input is different? That said though, thesed
command is producing only the text between the ▶ and the first •, which is what was expressed in the OP, so you should just remove theruby
portion of the answer since it's not as user friendly as thesed
command and it outputs the ▶ and the first • with the text too. Commented May 13, 2016 at 18:20 -
@user3439894 I can alter my Ruby to output things the same as the sed command. There's no harm in showing both ways, in any case.– LaurelCommented May 13, 2016 at 18:22
-
If you modify the
ruby
command so its output is the same and it takes the source file as an argument like thesed
command does, then I agree keeping both is fine. However, in its present form it's not practical. Commented May 13, 2016 at 18:33