LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat
User Name
Password
Red Hat This forum is for the discussion of Red Hat Linux.

Notices


Reply
  Search this Thread
Old 09-26-2011, 09:47 AM   #1
rockf1bull
LQ Newbie
 
Registered: Feb 2011
Posts: 19

Rep: Reputation: 0
Small Help required for merging two files per line!!!


Hi Guys,

Sorry to bother everyone again here, but I ran into a small problem that has been after me for some time now.

I have a file called file1.txt and has following content -

PHP Code:
/folder1/fold/folder   10k
/folder167fold/folder   10MB
/folder2/fold/folder   10G
etc
..
etc.. 
There is another file called file2.txt and has following content.

PHP Code:
folder1
folder5
test5
test5 
Now I am trying to merge these two into one separate file where it should look like -

PHP Code:
/folder1/fold/folder   10k folder1
/folder167fold/folder   10MB folder5
/folder2/fold/folder   10G test5
etc
..
etc.. 
So basically, I am just trying to append content of the 2nd file into file 1 at the end of each line.

I can certainly use sed 's/$/ ;FS folder1/' file 2 > output.txt however, it doesn't do the work for me since it can't add recursive content on each line of fil1.

I tried using paste -d ';' file file 2 > output.txt but it's not appending and its actually simply adding file 2 content at the end of file 1 in the final list.

Can someone please suggest me where i can append file 2 data into file1??

I am using a do -while loop as well by trying to read content of each line and then adding it to each line of file1.

Any suggestions or recommendation are of course welcome.

Thanks,
Morgan.
 
Old 09-26-2011, 10:09 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Try this:
Code:
diff -ty file1.txt file2.txt | sed 's/[ ]*| / ;/' > file3.txt
mv file3.txt file1.txt
Do the red part only if file3.txt is to your liking!

Hope this helps.
 
Old 10-01-2011, 09:45 PM   #3
ShanxT
Member
 
Registered: Apr 2007
Location: India
Distribution: Ubuntu 10.04, RHEL/Centos 5.x, Knoppix
Posts: 41

Rep: Reputation: 3
You can use the 'paste' command for this as well:
Code:
paste -d\; file1.txt file2.txt
The '-d' flag is the delimiter, with an escape character before the semi-colon.
 
Old 10-02-2011, 03:56 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
@ShanxT, That's exactly the same command the op said he has already tried.

I can't say why it's not pasting correctly for him (dos line-endings, perhaps?), because for me all the lines from file1 and file2 combine as expected. However, it doesn't give exactly the desired output anyway, as each column is separated by only a semicolon, without any space padding.

To get the above formatting, it seems to me that the best solution is to use awk's getline function to read from the second file.
Code:
awk '{ getline f2line < "file2.txt" ; print $0 " ; " f2line }' file1.txt
http://www.gnu.org/software/gawk/man...e/Getline.html

You could also use a two-step process; one command to add a semicolon to the end of each line first, and then a second to combine the lines in the two files.

Fortunately, you can combine both steps into one line using process substitution. This gives me the above output:
Code:
paste -d " " <( sed 's/$/ ;/' file1.txt ) file2.txt
I used -d " " because paste outputs tabs by default. You can change it if desired, of course.

http://mywiki.wooledge.org/ProcessSubstitution


By the way, please use regular [code][/code] tags, and not php code tags. 1) That isn't php, obviously, and 2) for some reason php boxes don't honor the column width limit, meaning long lines will force side-scrolling.
 
1 members found this post helpful.
Old 10-05-2011, 08:06 AM   #5
ShanxT
Member
 
Registered: Apr 2007
Location: India
Distribution: Ubuntu 10.04, RHEL/Centos 5.x, Knoppix
Posts: 41

Rep: Reputation: 3
Ah! My sincere apologies for not reading the original post completely. Also, thanks for the info on 'Process Substitution'!
 
  


Reply



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 many files as one niharikaananth Linux - Newbie 5 08-15-2011 01:07 PM
Advice required merging two domains elliot01 Linux - Networking 1 10-16-2009 04:49 PM
Merging Two Files using C++ ckoniecny Programming 5 09-26-2006 09:00 AM
small shell script required demigor Programming 6 08-08-2005 04:38 PM
WP for cmd-line data merging & printing? MikHud Linux - Software 0 11-06-2002 02:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat

All times are GMT -5. The time now is 08:07 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
Open Source Consulting | Domain Registration