LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   bash reading multiple files line by line. (http://www.linuxquestions.org/questions/programming-9/bash-reading-multiple-files-line-by-line-867185/)

danraider 03-08-2011 06:19 AM

bash reading multiple files line by line.
 
I have a set of files containing data that I need to re-arrange into one single property list.

The files that I have look like this:
Code:

# cat uk
<string>10</string>
<string>11</string>
  ...
<string>29</string>

# cat us
<string>A</string>
<string>B</string>
  ...
<string>R</string>

# cat eu
<string>6</string>
<string>7</string>
  ...
<string>25</string>

I've 6 of these files in total. What I need is a property list like this:

Code:

<array>
        <dict>
                <key>uk</key>
                <string>10</string>
                <key>us</key>
                <string>A</string>
                <key>eu</key>
                <string>6</string>
        </dict>
        <dict>
                <key>uk</key>
                <string>11</string>
                <key>us</key>
                <string>B</string>
                <key>eu</key>
                <string>7</string>
        </dict>
  ...
        <dict>
                <key>uk</key>
                <string>29</string>
                <key>us</key>
                <string>R</string>
                <key>eu</key>
                <string>25</string>
        </dict>
</array>

So the lines in the files match up but I haven't found a way of reading several files line by line. What would be the best way to go about this?

Reuti 03-08-2011 06:37 AM

You could use the tool paste to make one file from these three ones. This single file you can then process line by line.

danraider 03-08-2011 06:53 AM

Quote:

Originally Posted by Reuti (Post 4282649)
You could use the tool paste to make one file from these three ones. This single file you can then process line by line.

That did the trick.

I started by
sed -e 's/^/<key>fileName<\/key>/' filename > filename.key

then pasted the files together and then added the <dict> tags to each line as above. My app can read the finished result so I'm happy! :)

grail 03-08-2011 07:54 AM

Just for interest:
Code:

#!/usr/bin/awk -f

BEGIN{
    if(ARGC == 1){
        print "Error. You must enter at least one file name!!"
        exit 1
    }

    ARRAY_S = "<array>"
    ARRAY_E = "</array>"
    DICT_S = "\t<dict>"
    DICT_E = "\t</dict>"
    KEY_S = "\t\t<key>"
    KEY_E = "</key>"

    print ARRAY_S
    while((getline line < ARGV[1]) > 0){
        printf("%s\n%s\n\t\t%s\n", DICT_S, KEY_S ARGV[1] KEY_E, line)
        for(i = 2; i < ARGC;i++){
            getline line < ARGV[i]
            printf("%s\n\t\t%s\n", KEY_S ARGV[i] KEY_E, line)
        }
        print DICT_E
    }
    print ARRAY_E
}

Probably waaaay overkill, but if you wanted something to build on ;)

Call it like so:
Code:

./script.awk uk us eu
You could also just use a asterix (*) assuming only the files you want are in the directory and order will be sorted by name as opposed to set order.


All times are GMT -5. The time now is 01:30 AM.