sed — filter lines and more
While programming I often look for different objects and names across many projects. When I find desired files, I’d like to perform some operations on sub group of results. Lets say open 100 files but skipping first 5.
Lets start with just finding all *.java and *.xml files with foo and bar strings. Generally, my spell looks similar to this:
find . -iname '*.java' -o -iname '*.xml' | xargs grep -irl 'foo' | xargs grep -irl 'bar'
Then filtering
# filter
find . -iname '*.java' -o -iname '*.xml' | xargs grep -irl 'foo' | xargs grep -irl 'bar' | sed '5,105!d'
# open files with kwrite
find . -iname '*.java' -o -iname '*.xml' | xargs grep -irl 'foo' | xargs grep -irl 'bar' | sed '5,105!d' | xargs kwrite
Meaning of sed’s parameters:
5– take lines from 5th line105– take lines to 105th line!– take everything except from 5th to 105th lined– delete what you took and print everything else
Similarly, if you want to keep everything except 100 lines starting form 5th, then just drop ‘!’
find . -iname '*.java' -o -iname '*.xml' | xargs grep -irl 'foo' | xargs grep -irl 'bar' | sed '5,105d'