search for all string literals in an xcode project using the command line search for all string literals in an xcode project using the command line unix unix

search for all string literals in an xcode project using the command line


You may use a grep command like

grep -o '"[^"\]*\(\\.[^"\]*\)*"' file > outfile

See an online demo:

s='"String1 \"here\"""String2"'grep -o '"[^"\]*\(\\.[^"\]*\)*"' <<< "$s";

The "[^"\]*\(\\.[^"\]*\)*" POSIX BRE pattern matches ", then any 0+ chars other than \ and ", then any 0+ occurrences of \ and then any char followed with any 0+ chars other than \ and " and then a " char.

If you can use a PCRE enabled grep, you may use a safer expression that will only start matching at the first unescaped double quote:

grep -oP '(?<!\\)(?:\\{2})*\K"[^"\\]*(?:\\.[^"\\]*)*"' file > outfile

Here, "[^"\\]*(?:\\.[^"\\]*)*" is the PCRE equivalent of the above POSIX BRE pattern and (?<!\\)(?:\\{2})*\K makes sure the first " is not escaped: (?<!\\) matches a location not preceded with \, then (?:\\{2})* matches 0 or more occurrences of double backslashes and then \K omits the text matched so far.

See an online demo:

s='A culprit \" escaped quote and "String1 \"here\"""String2"'grep -oP '(?<!\\)(?:\\{2})*\K"[^"\\]*(?:\\.[^"\\]*)*"' <<< "$s";

Both yield the following output:

"String1 \"here\"""String2"