sed - Copy lines from multiple files in subfolders into one file -


i'm very new programming , trying learn how make tedious analysis tasks little faster. have master folder (master) 50 experiment folders , within each experiment folder set of folders holding text files. want extract 2 lines 1 of text fiels (experiment title on line 7, slope on line 104) , copy them new single file.

so far, have learned how extract lines , add new file.

sed -n '7p; 104 p' reco.txt >> results.txt

how can extract these 2 lines files 'reco.txt' in subfolder of folder 'master' , export single text file?

as explanation can bear great me learn.

you can use find in combination xargs this. on own, can list of relevant files:

find . -name reco.txt -print 

this finds files named reco.txt in current directory (.) or subdirectories , writes them standard output.

now, can use -exec argument find, run program each file found, except typically multiple results combined single execution (appended command line). particular invocation of sed works on 1 file @ time.

so, instead of -exec, can use xargs same thing more control.

find master -name reco.txt -print0 | xargs -0 -n1 sed -n '7p; 104 p' > results.txt 

this following:

  • searches in directory master or subdirectories file named reco.txt.
  • outputs each filename null-terminator instead of newline (-print0) -- allows full path contain characters need escaping (such spaces)
  • pipes result xargs, following:
    • accepts null-terminated strings (-0)
    • only puts @ 1 file each command (-n1)
    • runs sed -n '7p; 104 p' on file
  • entire output redirected results.txt, overwrite existing contents in file.

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -