0

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

2
  • 1
    Welcome 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.
    – Allan
    Commented 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

1 Answer 1

1

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
9
  • 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.
    – Jake
    Commented May 13, 2016 at 5:58
  • @Jake No problem. I translated it into sed, so it should be easier to do file things now.
    – Laurel
    Commented May 13, 2016 at 15:55
  • Do you realize that the output of your ruby command and sed command, against the same input is different? That said though, the sed command is producing only the text between the ▶ and the first •, which is what was expressed in the OP, so you should just remove the ruby portion of the answer since it's not as user friendly as the sed 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.
    – Laurel
    Commented 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 the sed 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .