Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'd like to edit all text files under a certain directory that have one or more lines that are over a certain threshold in length. How can I do that?

To keep things simple, let's assume that all the text files in the directory and its subdirectories have the same file extension - let's say .java.

P.S. My editor of choice is emacs.

share|improve this question

3 Answers 3

up vote 1 down vote accepted

Based on gniourf_gniourf's answer, here is a way that works within emacs, and allows you to jump directly to the overlong lines with TAB followed by RET:

M-x rgrep RET .\{116\} RET *.java RET [insert directory to scan here] RET
share|improve this answer

In a terminal, after changing to the directory in question, type:

find -name '*.java' -exec awk -vORS=$'\\0' 'length > 116 {print FILENAME; nextfile}' {} +|xargs -0 ${EDITOR:=emacs}

This will open all the .java files that have lines longer than 116 characters in a new instance of emacs - unless your EDITOR environment variable is set to the name of some other text editor, in which case it will open them in that instead.

share|improve this answer

If you don't want recursion into subdirectories, it's much simpler to do just this:

grep -lZ '.\{116\}' *.java | xargs -0 "${EDITOR:=emacs}"

If you need recursion into subdirectories, use the globstar option:

shopt -s globstar
grep -lZ '.\{116\}' **/*.java | xargs -0 "${EDITOR:=emacs}"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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