2

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.

2

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}"
| improve this answer | |
1

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.

| improve this answer | |
1

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
| improve this answer | |

Your Answer

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

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