1

I'm looking for a UNIX one-liner that will output to a file all occurrences of NSLocalizedString (from that word to the end of the line) in all files in the current directory and all subdirectories. I've googled, but haven't found a solution.

2 Answers 2

3
find . -type f -exec fgrep NSLocalizedString {} \+ | \
    sed -e 's/^.*\(NSLocalizedString.*\)$/\1/' > ../your_output_file
5
  • You could potentially use grep -ri 'NSLocalizedString' | sed right? Jun 4, 2012 at 19:09
  • 1
    This gets the substring from files in the CWD, but not the subdirectories. Mark, using your suggestion leads to a "grep: warning: recursive search of stdin" error Jun 4, 2012 at 19:28
  • I'm sorry, I was intending to provide a high level command. grep -ri 'NSLocalizedString' * | sed <rest of command>. You could potentially use ack-grep for this as well, but that's not usually installed by default. Jun 4, 2012 at 19:36
  • @user1161418 - By This gets the substring from files in the CWD, but not the subdirectories do you mean Mark's suggestion or my answer? I assure you, find . starts in the current directory but descends through subdirectories. (I tested this one-liner before posting it)
    – Stephen P
    Jun 4, 2012 at 20:22
  • Good solution: Though I will suggest to use ':' in sed substitute command instead of / to make it visually less harmful
    – mawia
    Jun 5, 2012 at 4:49
0

find <directory> -type f -print | xargs grep NSLocalizedString | tee <outputfile> should do what you're looking for, if I understand the question right...

2
  • 1
    You want to do find ... -print0 | xargs -0 ... to handle files/directories with spaces in the names. Jun 4, 2012 at 19:09
  • add -n 100 or something after xargs to limit the number of files it will process in one batch
    – twalberg
    Jun 4, 2012 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.