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

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -