LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-08-2011, 07:19 AM   #1
danraider
Member
 
Registered: Feb 2004
Distribution: Slackware 12 - dropline Gnome
Posts: 43

Rep: Reputation: 15
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?
 
Old 03-08-2011, 07:37 AM   #2
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,320

Rep: Reputation: 252Reputation: 252Reputation: 252
You could use the tool paste to make one file from these three ones. This single file you can then process line by line.
 
1 members found this post helpful.
Old 03-08-2011, 07:53 AM   #3
danraider
Member
 
Registered: Feb 2004
Distribution: Slackware 12 - dropline Gnome
Posts: 43

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Reuti View Post
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!
 
Old 03-08-2011, 08:54 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH: read every line in the files and use the line as parameters as another program tam3c36 Programming 10 12-07-2010 02:42 PM
line by line reading and writing in Bash nesrin Programming 4 12-01-2010 09:19 AM
BASH: Each line of multiple text files gets added to one line Gavin Harper Programming 3 09-12-2010 08:31 PM
a little bash help: cat multiple files together except first line? sanimfj Linux - General 3 02-09-2010 05:27 PM
Search values within multiple files line to line Chrizzieej Programming 5 09-26-2008 05:11 PM


All times are GMT -5. The time now is 05:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration