0

I have a list of 50 folders. Inside every folder I have a text file contain key words and this file has the same name in all the folders but it has different values. How can a script read a certain key words inside those text files and save (collect) those result in a new file?

    folder A 
       text.file (inside it key words let us say Abc=1 Def=2 Ghi=3 )
    folder b
       text.file(inside it key words let us say Abc=5 Def=1 Ghi=4 )
    folder c
       text.file(inside it key words let us say Abc=11 Def=12 Ghi=32 )
    folder d
       text.file(inside it key words let us say Abc=11 Def=27 Ghi=34 )
    folder e
       text.file(inside it key words let us say Abc=16 Def=12 Ghi=22 )
    folder f
       text.file(inside it key words let us say Abc=3 Def=2 Ghi=1)
    .
    .
    .
    .
    folder N

The key words are sorted in the text file as the following (and this sort for the key words is the same in all the files included in the 50 folder)

    Abc= value
    Def= value
    Ghi= value

For example: I need the script to read the key word "Abc" in all the files and save the output (i.e. collect the values for the key word "Abc" separably in a new file) as a text file contain just the "Abc" values from all the folders as the following:

Abc=1
Abc=5
Abc=11
Abc=11
Abc=16
Abc=3
  • Are Abc=1, def=2, etc. on the same line with spaces in between, or on separate lines, or something else? If they're on separate lines, can there be initial whitespace before the keywords? Are the keywords case-insensitive (you've used Abc in some places and abc in others)? – Gilles Nov 4 '14 at 17:55
  • @MJA: Where did you mention this? – cuonglm Nov 4 '14 at 18:01
  • My bad!! I will update the question. Sorry for any confusion – user88036 Nov 4 '14 at 18:04
  • @Gilles. thanks. they are as I mentioned before. They are sorted as a column in the text file (separate lines). yes the keywords are case sensitive (Sorry for the typos) – user88036 Nov 4 '14 at 18:07
0

If all key words are sorted in the text.file as the following (and this sort for the key words is the same in all the files included in the 50 folder)

Abc= value
Def= value
Ghi= value

Then you can read each first line head -n 1 filename of them into for loop and write it in new file like below:

$ for file in ~/Parent_dir/Folder*/*; do  head -n 1 $file; done
 Abc=1
 Abc=5
 Abc=11
 Abc=11
 Abc=16
 Abc=3

Your script will be look like above:

for file in ~/Parent_dir/Folder*/*; do
    head -n 1 $file;
done > newfile
  • Can I use two for loops here. For example I will include the parent folders names in a text file then I will use for i in txt file do; the second for loop? – user88036 Nov 4 '14 at 18:50
  • I want to do it like parent_dir/${i} and i is one of my list of folder names included in a text file – user88036 Nov 4 '14 at 18:56
  • @MJA Yes you can but why not one for loop? – αғsнιη Nov 4 '14 at 18:58
  • can I say for file in and i in the text file. – user88036 Nov 4 '14 at 19:00
  • 1
    @MJA No with that command you will read first 5 lines of file. If you want to read 5th line, sed command is useful. you can use sed 'NUMq;d' filename. Where NUM is the number of line you want to read. ;) If you have a new question, please open it as new one. Thanks and welcome ` – αғsнιη Nov 4 '14 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy