LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-18-2011, 02:14 PM   #1
linux002
LQ Newbie
 
Registered: Apr 2011
Location: California, US
Distribution: debian wheezy
Posts: 5

Rep: Reputation: 6
merging files with different number of rows using awk


Does anyone have a solution for merging files if the number of rows in the two (or more) files is non-equivalent.

To exemplify, how about merging the following 3 files:

file1:
1
2


file2:
3
4
5


file3:
6
7
8
9


output after merging:
1.....3.....6
2.....4.....7
......5.....8
............9


or, if this is easier:
6.....3.....1
7.....4.....2
8.....5
9


(sorry, I couldn't figure how to insert tabs, so the periods just denote whitespace)

Thanks!
 
Old 04-18-2011, 08:28 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi, welcome to LQ!

I took the liberty to split your thread out of the
unrelated long dead thread; in the future please
consider starting a new thread.


Cheers,
Tink
 
Old 04-27-2011, 05:03 AM   #3
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Welcome to LQ. Hope your time here helps you as much as mine has helped me.

Please put code, command line output, config files, etc. inside [CODE] tags, aka "Code:" blocks.

It will make your posts easier to read, & that will get you more, faster, better answers. -- Help us help you.
BTW, You can edit your post(s) to do this retroactively.

Thank you, & again, welcome.


Now for your question:
- What have you tried so far?
- Which CLI tools are you most comfortable with?
 
Old 04-27-2011, 06:06 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,493

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Well its not pretty, but this works:
Code:
awk 'FNR == 1 && b{a=a"\t"}{line[FNR]=(line[FNR])?line[FNR]"\t"$0:a$0;b=b<FNR?FNR:b}END{while(++x <= b)print line[x]}' file[1-3]
 
Old 04-29-2011, 02:47 PM   #5
linux002
LQ Newbie
 
Registered: Apr 2011
Location: California, US
Distribution: debian wheezy
Posts: 5

Original Poster
Rep: Reputation: 6
Thanks for the suggestion grail. I will try to parse that. What does b{a=a"\t"} do?

archtoad 6 -- The CLI I am most familiar for this purpose is AWK, though I suppose a combination of other CLIs would be able to perform the task. I wrote a perl script to do this, which involves reading the contents of each file and storing them as an array, sorting the arrays from largest number of lines to smallest, and then printing with nested for loops and conditionals. It basically does the job, but I wanted to see if people on the forums had ideas for a cleaner, more efficient solution.

I have rendered the desired output using [CODE] bracketing per your suggestion. The input files contain the following:
1, 2, 3, 4
5, 6, 7
8, 9

Here is the output:
Code:
1     5     8
2     6     9
3     7
4
 
Old 04-29-2011, 09:09 PM   #6
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
Using the files in the first post
Code:
paste file[123]

1       3       6
2       4       7
        5       8
                9
 
1 members found this post helpful.
Old 04-29-2011, 11:06 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,493

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Would seem Kenhelm's suggestion is the bomb
 
Old 04-30-2011, 02:26 AM   #8
linux002
LQ Newbie
 
Registered: Apr 2011
Location: California, US
Distribution: debian wheezy
Posts: 5

Original Poster
Rep: Reputation: 6
Thanks Kenhelm. Your solution is quite concise and works perfectly : )
 
Old 05-05-2011, 03:26 PM   #9
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Thanks for expressing your appreciation & for marking this "[SOLVED]".
 
  


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
merging content of two files using sed or awk mukherjee_debajyoti Programming 5 11-30-2009 05:43 AM
using awk...merging two files...lines...one after another... visitnag Linux - Newbie 4 07-14-2008 11:37 AM
adding constant (number) to a number in all rows skuz_ball Programming 2 05-20-2008 09:57 PM
data with same number of rows and coloumns C/C++ fs11 Programming 2 04-29-2008 10:17 PM
I there a way to tell awk to select rows instead of collums jsandro7 Linux - Newbie 2 06-14-2004 10:58 AM


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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration