Results 1 to 6 of 6
I'm trying to figure out how take multiple files, extract things line by line, and put them into another file next to each other. I'll explain further, as this confuses ...
- 06-21-2009 #1Just Joined!
- Join Date
- Jun 2009
- Posts
- 1
Grep multiple files into one file by line
I'm trying to figure out how take multiple files, extract things line by line, and put them into another file next to each other. I'll explain further, as this confuses me, and I'm the one trying to do it.
I have 5 files. To make it easier to understand the files are as follows:
1 - First name
2 - Last name
3 - Address
4 - Phone #
5 - Country
The files are matched up so that all of the files line 1 is the same person. As is the same for all other lines. I'm trying to figure out how to take the first line of each of the files and have a long line in the new file with all the info. And then do the same for all the other lines. So each line will have the person's name, phone # etc.
How do I go about doing this, or where can I start looking? I'm rather new to linux, and would appreciate any pointers in the right direction.
Thanks,
Timmay
- 06-21-2009 #2
I think your going to have to look at something with a bit more functionality than Grep to get this done. I would consider Perl...Gerard4143
Make mine Arch Linux
- 06-22-2009 #3Linux User
- Join Date
- Aug 2006
- Posts
- 458
@OP , so what's the output look like? i don't understand what you are describing.
- 06-22-2009 #4
So I believe I understand. You have files that look like:
And you want output that looks like:Code:1: Alex John 2: Iselin Smith 3: 1234 Example Road 5678 Some Street 4: 123-456-7890 321-654-0987 5: Spain Iceland
Well, the short answer is that grep isn't going to do this. grep takes a pattern and finds lines in a file that look like that pattern. You want to read five files simultaneously, and process them line-by-line.Code:Alex Iselin, 1234 Example Road, 123-456-7890, Spain John Smith, 5678 Some Street, 321-654-0987, Iceland
The longer answer is that this is not particularly difficult. Your basic design is something like:
1) Create a structure or class that can hold all of the information for a single person.
2) Create a list of these structures.
3) Read the first line of each of the five files, and use this information to create a full person.
4) Add this person to the list.
5) Repeat 3 and 4 until you have read every line.
6) Print out each element of the list in whatever format you want.
This will be very difficult to do with Bash scripting (Bash doesn't really have structures or arrays), but it is very easy with any other sort of scripting language (Perl, Ruby, Python), or pretty much any language.
Does this help at all?DISTRO=Arch
Registered Linux User #388732
- 06-22-2009 #5Just Joined!
- Join Date
- Jun 2009
- Posts
- 1
your script should be something like this:
also you should add some testCode:read -p "Enter last name: " LNAME # get line number where last name is stored in file l_name LN=$(nl -w1 l_name | grep -m 1 -i $LNAME | cut -f 1) for i in f_name l_name address tel country; do OUT=$(nl -w1 $i | grep ^$LN | cut -f 2) echo -n "$OUT " done # new line echo
- 06-23-2009 #6Linux User
- Join Date
- Aug 2006
- Posts
- 458


Reply With Quote
