3

I have a directory of many *.org files. I want to select a section of text out of all these org files. There is a * Learnings header in the content. I would like to select from the * Learnings header to the end of the file.

My current attempt is

find ~/org/journal -name "*.org" -type f | xargs sed -n -e '/\*\ Learnings/,$p'

This however just outputs one concatenated stream.

Expected output would be a stream of the content after the * Learnings header for each file returned from the find

also the solution does not have to use sed

2
  • What output do you expect/need?
    – choroba
    Mar 30, 2019 at 18:50
  • @choroba added expected output
    – kevzettler
    Mar 30, 2019 at 21:08

4 Answers 4

2

With the GNU implementation of sed, you can use the -s aka --separate option for each file to be treated separately in that regard.

find . -name '*.org' -type f -exec sed -s '/\* Learnings/,$!d' {} +

With awk:

find . -name '*.org' -type f -exec awk '
  FNR == 1 {found = 0}; /\* Learnings/ {found = 1}; found' {} +
1

In Perl, you can use eof that will be true for each end of file:

find . -type f -name '*.org' -exec perl -ne 'print if /\* Learnings/ .. eof' {} +

Using the + form of -exec works similarly to xargs: it builds the arguments to the specified command by appending all the found files.

0

Unless this is some kind of homework ;-), you can use the same trick as here with GNU or *BSD grep:

grep -hrFA 10000 '* Learnings' directory

Replace 10000 with something bigger if your files have more than 10000 lines.

0

examine it in gnu sed,

find ~/org/journal -iname "*.org" -type f -exec sed -nE '/\*\sLearnings/,$ p' '{}' +

to really edit and save it, with the origin in a file with .o extension,

find ~/org/journal -iname "*.org" -type f -exec sed -i.o -nE '/\*\sLearnings/,$ p' '{}' +

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .