0

I know a fair bit about escape characters in BASH. I know that \ will escape the next character, " " will escape all characters except for $ and ", and finally ' ' will escape all characters except for '. So if for example I have text with some single quotes ' in it, I can put " " around it to make it behave like a single text string. The problem I have is when my file contains both double and single quote characters. Then, I cannot use either type of quotes to escape it. So how do I ask bash to treat it as a single string?

Context: My iPhone's /var/mobile/Library/Safari/History.plist file. It contains something like:

  <key>
    <string>http://example.com/folder/index.html</string>
    <string>Title of Webpage which may include ' and "</string>
  </key>

I want to cat the file, pass it through grep to take the first line containing the text string and extract the URL from that. But grep chokes on all the quotes.

Edit 1: Here are some screenshots of nano failing to read the file. Cat gives similar output. The graphical file editor iFile reads it as text. http://cloudrac3r.ddns.net/strange.html I am told this is what binary files do. I will do some experimenting.

  • I'm simply using cat /path/History.plist to read the file to stdin (works fine) and then putting ` | grep string` on the end. Grep return nothing. – user171641 May 23 '16 at 10:20
  • Try grep 'Title of Webpage which may include '\'' and "' or grep "Title of Webpage which may include ' and \"" – choroba May 23 '16 at 10:22
2

If you enter the string literally into the command, you can escape the characters at your will.

grep 'Title of Webpage which may include '\'' and "' /var/mobile/Library/Safari/History.plist
grep "Title of Webpage which may include ' and \"" /var/mobile/Library/Safari/History.plist

Also notice that cat isn't needed.

Moreover, it's much better to use an XML-aware tool to process XML.

xmllint -xpath \
    '//key[string=concat("Title of Webpage which may include '\'' and ", '\''"'\'')]' \
    /var/mobile/Library/Safari/History.plist
  • Ah, I didn't know that grep took a file name. I always piped cat output to it. Thanks for the tip. However, I have a new problem. Reading the file in the terminal using cat or nano or anything returns garbage (!) whereas using a graphical file manager returns the proper xml file. I can link screenshots if you give me some time... – user171641 May 23 '16 at 10:48
  • @cloudrac3r: maybe the file is compressed? gzip or something? – choroba May 23 '16 at 11:14
  • don_crissti suggested it was in fact a binary file; they were right. The plutil command got me the information from it. Another edit will follow after some experiments. – user171641 May 23 '16 at 11:17
0

(Converted edit to answer)

I used the plutil command to convert the plist to proper text since it was a 'binary file'. Once that was done, grep had no problem managing the file.

Your Answer

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