Find the answer to your Linux question:
Results 1 to 5 of 5
I have a folder which includes bunch of folders each having data files in them. [ Folder A has F1, F2 F3 ..... F1000 folders in it, and F1, F2, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    voo
    voo is offline
    Just Joined!
    Join Date
    Mar 2011
    Posts
    3

    reading data from multiple files.


    I have a folder which includes bunch of folders each having data files in them. [ Folder A has F1, F2 F3 ..... F1000 folders in it, and F1, F2, F3 ... each has about 10 different files named FILE 1, FILE2, FILE3 .... in them.

    I am interested in File 1 of each Folder, because that contains the data I need in it. More specifically, that File1 s have a line "ANSWER=..." in them, and i need to get that value of the ANSWER from each file. So doing it by hand is so hard, so I need to write a script that will scan all folders and give me a list of values of eache ANSWERs.

    Any suggestions, where shoud I start?

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,411
    Code:
    find A -type f -name "FILE1" -print0 | while read -r -d '' WORKFILE; do grep "ANSWER=" ${WORKFILE}|cut -d "=" -f2 ; done
    This assumes, that the value is in the second field, for = as a delimiter.


    If (and only if) the directory structure is static,
    then you could create a list of the files once and feed that to grep in a for loop.

    Doesnt make too much sense to parallize that, unless the directories are on different spindles.
    You must always face the curtain with a bow.

  3. #3
    voo
    voo is offline
    Just Joined!
    Join Date
    Mar 2011
    Posts
    3
    thank you for the answer, however i couldn't get it work. it gives the error: "Illegal option -d"

    To be more precise, let me rephrase my problem. In folder A I have subfolders named: 1.11, 1.12, 1.13 etc... And in each subfolder, i have a file named OSX. And in each OSX there is a line "ANSWER=1212" (some number). I need to obtain those numbers from each subfolder of A.

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,411
    The error you get is related to a "bash-ism" I used.
    It seems, your shell´s "read" doesnt know -d

    You have at least two options:
    1) start a bash, then that line from above.
    2) use this line below. Should be safe enough for filenames without any special chars, like you have:
    Code:
    find A -type f -name "OSX" | while read WORKFILE; do grep "ANSWER=" ${WORKFILE}|cut -d "=" -f2 ; done
    You must always face the curtain with a bow.

  6. #5
    voo
    voo is offline
    Just Joined!
    Join Date
    Mar 2011
    Posts
    3
    thank you, the second line worked fine

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •