Issue
The question is how to combine the following commands in one line and use exec.
find . -name '*.txt' -exec sh -c 'echo "$(sed -n "\$p" "$1"),$1"' _ {} \;
The result: path and name of all .txt files.
find . -name '*.txt' -exec sed -n '/stringA/,/stringB/p' {} \;
The result: lines between start and end parameters over all .txt files.
The requested result: give me lines between start and end parameters. The first line must be contain path and name of the .txt file.
find . -name '*.txt' -exec ???? {} \;
./alpha/file01.txt
stringA
line1
line2
stringB
./beta/file02.txt
stringA
line1
line2
stringB
Thanks. T.
Solution
If the files are non-empty then all you need is:
find . -name '*.txt' -exec awk 'FNR==1{print FILENAME} /StringA/,/StringB/' {} +
If they can be empty then the simplest way to handle it is to use GNU awk for BEGINFILE:
find . -name '*.txt' -exec awk 'BEGINFILE{print FILENAME} /StringA/,/StringB/' {} +
Answered By - Ed Morton