Reflection to grab strings from an objective c file - objective-c

I have a large objective c file filled with hardcoded objects with a description String field on them. They are all constructed like so:
Item *43 = [[Item alloc] initWithFieldId:#"43" description:#"This is test 43 of 100"];
I would like to know if there is a way for me to extract all of these strings in this .m file and write them to a text file. Is there some kind of reflection library that would let me walk this file and grab all the strings starting from description:# ending at the second quotation mark?

Not 100% sure if this is what you're looking for, but you could do it from the terminal pretty easily with a find command and some regex like Bob recommends: (edited to only echo descriptions, not entire file)
find . -type f -name \*.m -exec sed -n 's/Item .*description:#"\(.*\)"].*/\1/p' {} \;
This will return
This is test 43 of 100
for all of the matches.
For the entire line that the search finds use:
find . -type f -name \*.m -exec grep Item\.*description:# {} \;

Since there's a specific pattern to the file you can use Regular Expressions to extract what you need.
There are various editors that internally support regex, but you can always use regex through the Unix command line tools.
Here's a place to start.

Related

Remove specific suffix from all files containing it

Long story short, OneDrive has taken all my files and renamed them to include the string "-DESKTOP-9EI0FN7" at the end of the file name, resulting in files such as:
myTextFile-DESKTOP-9EI0FN7.txt
myVideo-DESKTOP-9EI0FN7.mp4
So I'd like to write a batch script that finds all the files with that string in them, and renames them to remove the string, so:
myTextFile-DESKTOP-9EI0FN7.txt becomes myTextFile.txt
The problem is, I know nothing about writing batch files. Any advice?
Test out with this bad boy:
find . -type f -exec rename -n -e 's/(.*)\-DESKTOP\-9EI0FN7(.*)/$1$2/' {} \;
If the output satisfies you, remove the -n portion and it does actually apply the changes.
Good luck, sir!

Find specific text in pdf file and print file name and row with text

I've searched online and I cannot find the answer I need..I need to search recursively in Linux environment through multiple directories for a file named "monthly_spd.pdf" for a key word phrase such as "MOS-corrected" and then print that row where "MOS-corrected" is found and also print the path/filename.
I've tried this below and I can print or display the file/path name but I have not figured out how to print the row where the key word phrase ("MOS-corrected") is found in each file. Thank you.
find . -name 'monthly_spd.pdf' -exec echo {} \: -exec pdftotext {} - \; | grep "MOS-corrected, r\|pdf"
I have found an answer here in the link below by adding the "-C5" after "grep" in the solution I tried above for printing the context around the key word phrase, which is exactly what I needed. See remark/comment from Colin D Bennett!
How to search contents of multiple pdf files?
Use:
pdfgrep -HinR 'FWCOSP' DatenModel/
In this command I'm searching for the word FWCOSP inside the folder DatenModel/.
As you can see in the output you can have the file name wit the line numbers:
The options I'm using are:
-i : Ignores, case for matching
-H : print the file name for each match
-n : prefix each match with the number of the page where it is found
-R : same as -r, but it also follows all symlinks.

How to extract the strings in double quotes for localization

I'm trying to extract the strings for localization. There are so many files where some of the strings are tagged as NSLocalizedStrings, and some of them are not.
I'm able to grab the NSLocalizedStrings using ibtool and genstrings, but I'm unable to extract the plain strings without NSLocalizedString.
I'm not good at regex, but I came up with this "[^(]#\""
and with the help of grep:
grep -i -r -I "[^(]#\"" * > out.txt
It worked, and all the strings were actually grabbed into a txt file, but the problem is ,
if in my code there is a line:
..... initWithTitle:#"New Sketch".....
I only expect the grep to grab the #"New Sketch" part, but it grabs the whole line.
So in the out.txt file, I see initWithTitle:#"New Sketch", along with some unwanted lines.
How can I write the regex to grab only the strings in double quotes ?
I tried the grep command with the regex mentioned in here, but it gave me syntax error .
For ex, I tried:
grep -i -r -I (["'])(?:(?=(\\?))\2.)*?\1 * > out.txt
and it gave me
-bash: syntax error near unexpected token `('
In xcode, open your project. Go to Editor->Export For Localization...It will create the folder of files. Everything that was marked for localization will be extracted there. No need to parse it yourself. It will be in the XML format.
If you wanna go hard way, you can then parse those files the way you're trying to do it now ?! It will also have Storyboard strings there too, btw.

add prefix to files with rename - error argument too long

I have thousands of files inside a directory I need to rename adding a prefix like "th_" so that files will be th_65461516846.jpg
but I can't due to the error "argument too long"
I have used this command
rename 's/^/th_/' *
thanks!
The xargs program is used to break command lines into multiple commands to avoid blowing the shell line length limit. In your case, you'd use:
ls | xargs rename 's/^/th_/'
Which repeatedly executes rename with a portion of the output of ls until the list of files is exhausted. Do be aware this idiom requires special attention if the file names have spaces or other funny characters in them (which I'm assuming isn't so based on your example).
This one did the job
for f in *; do mv "$f" "${f/9/th_}";done
or
for f in * ; do mv $f th_${f#} ; done
I don't know what differs between the 2 but in my case they both work.

Trying to cat a header into source files but a Unicode BOM is getting in the way

Following the instructions at Add header (copyright) information to existing source files, I need to add copyright headers to a bunch of source files we're sending out of the building. (I know, I hate copyright headers too, but it's policy for when we release proprietary source files. Please consider "persuade someone to waive the policy" as unhelpful and as not answering the question.)
I have two copies of all the files (in dir and dir.orig) and, from within dir.orig, I'm using
find . -name \*.cs -exec sh -c "mv '{}' tmp && cp ../header.txt '../dir/{}'
&& cat tmp >> '../dir/{}' && rm tmp" \;
This is working, but it's ending up with the header, then the BOM from the original source file, whereas I'd prefer either the BOM to move to the start or be removed.
(Looking at this, I realise that moving the file to tmp is unnecessary, given I'm not overwriting the original, but I didn't bother removing that from the example from the other SO question.)
How can I remove (or move) the BOM so that I end up without it appearing immediately after the newly-added header?
I think I may have found my solution, thanks to being pointed to uconv from this answer from Steven R. Loomis on a related question.
If I use
find . -name *.cs -exec sh -c "cp ../header.txt '../dir/{}'
&& uconv --remove-signature -f UTF-8 -t UTF-8 '{}' >> '../dir/{}'" \;
, then uconv assumes both input (-f) and output (-t) encodings should be UTF-8, but --remove-signature causes it to remove any BOM it finds.