LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 07-02-2010, 09:03 AM   #1
Syed Tarique Moin
LQ Newbie
 
Registered: Dec 2008
Posts: 13

Rep: Reputation: 0
combine two files


Hello,

i have two files with thousands of line, I am trying to combine these two files but i want to insert each line of one file to the another file after certain lines. I am using awk with the following command but it does not work.

cat file1 | awk ' { print $0; if (NR%3004==0) {print "file2"}}' > outputfile

Regards
 
Old 07-02-2010, 09:18 AM   #2
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
Please give us an example of the input file contents, and the desired output format. Also, what do you mean specifically by "it doesn't work"? It's very hard to give specific advice without knowing what we're working with.
 
Old 07-02-2010, 09:30 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Also the 'cat' at the beginning is not required and you are simply printing the string "file2" at the chosen line, ie no contents.

David is correct though that a lot more information is needed prior to any real help being available.
 
Old 07-02-2010, 11:34 AM   #4
Syed Tarique Moin
LQ Newbie
 
Registered: Dec 2008
Posts: 13

Original Poster
Rep: Reputation: 0
Hello,

Thanks for the reply, The example is as follows!

for example file 1

AAAAAAAAAAAAAA BBBBBBBBBBBBB CCCCCCCCCCCCCCC
AAAAAAAAAAAAAA BBBBBBBBBBBBB CCCCCCCCCCCCCCC
AAAAAAAAAAAAAA BBBBBBBBBBBBB CCCCCCCCCCCCCCC
AAAAAAAAAAAAAA BBBBBBBBBBBBB CCCCCCCCCCCCCCC
AAAAAAAAAAAAAA BBBBBBBBBBBBB CCCCCCCCCCCCCCC
------------
------------
------------

File 2
111111111111111111111111111111111111111
222222222222222222222222222222222222222
333333333333333333333333333333333333333
444444444444444444444444444444444444444
----------------
---------------
----------------
-------------

I want to get out put like this

AAAAAAAAAAAAAABBBBBBBBBBBBBCCCCCCCCCCCC
111111111111111111111111111111111111111
AAAAAAAAAAAAAABBBBBBBBBBBBBCCCCCCCCCCCC
222222222222222222222222222222222222222
AAAAAAAAAAAAAABBBBBBBBBBBBBCCCCCCCCCCCC
333333333333333333333333333333333333333
AAAAAAAAAAAAAABBBBBBBBBBBBBCCCCCCCCCCCC
444444444444444444444444444444444444444


This is long file of the simulation trajectory. I want to put each different and consecutive line from file 2 to file 1 after specific lines e.g after every 50 lines.

Regards
 
Old 07-02-2010, 11:57 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well if using awk then this could help:

http://www.gnu.org/manual/gawk/html_...tline_002fFile
 
Old 07-05-2010, 05:57 AM   #6
Syed Tarique Moin
LQ Newbie
 
Registered: Dec 2008
Posts: 13

Original Poster
Rep: Reputation: 0
combining two file with getline

Hello,

Thanks for the reply i have two files traj_H2O2.xyz and t.xyz, I want to insert each line of file t.xyz in another file traj_H2O2.xyz using the following command with get line.

cat traj_H2O2.xyz | awk ' { print $0; if (NR%3005==0) {getline $0 < "t.xyz"}}' > c.xyz

The output c.xyz is the same as the traj_H2O2.xyz.

Kindly i need guidance.

Regards
 
Old 07-05-2010, 06:12 AM   #7
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,

Is this what you are looking for:

diff -y traj_H2O2.xyz t.xyz | sed 's/[[:blank:]][[:blank:]]*|[[:blank:]]/\n/' > c.xyz

Sample run:
Code:
$ cat traj_H2O2.xyz
A1AAAAAAAAAAAA BBBBBBBBBBBBB CCCCCCCCCCCCCCC
A2AAAAAAAAAAAA BBBBBBBBBBBBB CCCCCCCCCCCCCCC
A3AAAAAAAAAAAA BBBBBBBBBBBBB CCCCCCCCCCCCCCC
A4AAAAAAAAAAAA BBBBBBBBBBBBB CCCCCCCCCCCCCCC
A5AAAAAAAAAAAA BBBBBBBBBBBBB CCCCCCCCCCCCCCC

$ cat t.xyz
111111111111111111111111111111111111111
222222222222222222222222222222222222222
333333333333333333333333333333333333333
444444444444444444444444444444444444444
555555555555555555555555555555555555555

$ diff -y traj_H2O2.xyz t.xyz | sed 's/[[:blank:]][[:blank:]]*|[[:blank:]]/\n/'
A1AAAAAAAAAAAA BBBBBBBBBBBBB CCCCCCCCCCCCCCC
111111111111111111111111111111111111111
A2AAAAAAAAAAAA BBBBBBBBBBBBB CCCCCCCCCCCCCCC
222222222222222222222222222222222222222
A3AAAAAAAAAAAA BBBBBBBBBBBBB CCCCCCCCCCCCCCC
333333333333333333333333333333333333333
A4AAAAAAAAAAAA BBBBBBBBBBBBB CCCCCCCCCCCCCCC
444444444444444444444444444444444444444
A5AAAAAAAAAAAA BBBBBBBBBBBBB CCCCCCCCCCCCCCC
555555555555555555555555555555555555555
Hope this helps.

Last edited by druuna; 07-05-2010 at 06:13 AM.
 
Old 07-05-2010, 07:56 AM   #8
Syed Tarique Moin
LQ Newbie
 
Registered: Dec 2008
Posts: 13

Original Poster
Rep: Reputation: 0
combining two file with getline

Hi,

Thanks for the reply. I don't want the difference of these file because these two files are not equal in numbers (of lines). It didn't work.

Regards
 
Old 07-05-2010, 07:59 AM   #9
Syed Tarique Moin
LQ Newbie
 
Registered: Dec 2008
Posts: 13

Original Poster
Rep: Reputation: 0
It just puts every line of other file (file2) with every line of first file (file1) but i want to put the each line of a file (file2) into another file (file1) after specific interval then another line of file (file2) to another file (file1) after same specific interval.

Regards
 
Old 07-05-2010, 08:16 AM   #10
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,

Output form your example (end post #4) looks like my output (end post #7).

I did change your input file a bit (second character is 1 to 5, to make sure the correct line is used).

If your example isn't what you want, then please post a relevant one......

How does this specific interval supposed to work/look?
 
Old 07-05-2010, 08:35 AM   #11
Syed Tarique Moin
LQ Newbie
 
Registered: Dec 2008
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks, I have a very large file. But i can give real input of my work below.

file 1
O -6.230600 3.314400 -12.287100
H -6.119800 3.047300 -11.368100
H -6.533100 4.204800 -12.096600
O 12.104800 2.198100 -15.244801
H 13.015100 2.087200 -15.000700
H 11.760301 2.773000 -14.558399
O 2.163200 14.690200 -12.447701
H 1.316700 14.269799 -12.596399
H 1.994400 15.476899 -13.053499
O 15.386072 6.185700 10.372772
H 14.869772 6.957000 10.262774
H -15.260300 6.326600 11.209572
O -0.915700 -6.342100 10.467173
H -0.108700 -6.814900 10.350771
H -1.477300 -6.455300 9.654371
O 6.318500 9.950600 2.828700
H 6.415500 8.971100 2.673700
H 7.237900 10.170699 2.589300


file2
Q 0.4402 -0.49655 -0.19745
Q 0.4392 -0.4938 -0.1963
Q 0.43845 -0.4914 -0.19525
Q 0.43775 -0.48955 -0.1947
Q 0.4373 -0.4882 -0.19495
Q 0.43705 -0.48725 -0.1964

output would be like this.

output
O -6.230600 3.314400 -12.287100
H -6.119800 3.047300 -11.368100
H -6.533100 4.204800 -12.096600
Q 0.4402 -0.49655 -0.19745
O 12.104800 2.198100 -15.244801
H 13.015100 2.087200 -15.000700
H 11.760301 2.773000 -14.558399
Q 0.4392 -0.4938 -0.1963
O 2.163200 14.690200 -12.447701
H 1.316700 14.269799 -12.596399
H 1.994400 15.476899 -13.053499
Q 0.43845 -0.4914 -0.19525
O 15.386072 6.185700 10.372772
H 14.869772 6.957000 10.262774
H -15.260300 6.326600 11.209572
Q 0.43775 -0.48955 -0.1947
O -0.915700 -6.342100 10.467173
H -0.108700 -6.814900 10.350771
H -1.477300 -6.455300 9.654371
Q 0.4373 -0.4882 -0.19495
O 6.318500 9.950600 2.828700
H 6.415500 8.971100 2.673700
H 7.237900 10.170699 2.589300
Q 0.43705 -0.48725 -0.1964

It shows that the both input file are different and each different line of file2 come after certain lines of file1. Its a short illustration but i have to put each different lines of file2 after specific interval of 3004 line of file 1.
I hope that this post would help you to understand my problem.
Regards
 
Old 07-05-2010, 09:04 AM   #12
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 again,

Another try

awk ' { print } NR % 3 == 0 { getline < "file2" ; print } ' file1

I guess you need to change 3 into 3004, but this seems to generate the example output given in post #11:
Code:
$ cat file1
O -6.230600 3.314400 -12.287100
H -6.119800 3.047300 -11.368100
H -6.533100 4.204800 -12.096600
O 12.104800 2.198100 -15.244801
H 13.015100 2.087200 -15.000700
H 11.760301 2.773000 -14.558399
O 2.163200 14.690200 -12.447701
H 1.316700 14.269799 -12.596399
H 1.994400 15.476899 -13.053499
O 15.386072 6.185700 10.372772
H 14.869772 6.957000 10.262774
H -15.260300 6.326600 11.209572
O -0.915700 -6.342100 10.467173
H -0.108700 -6.814900 10.350771
H -1.477300 -6.455300 9.654371
O 6.318500 9.950600 2.828700
H 6.415500 8.971100 2.673700
H 7.237900 10.170699 2.589300

$ cat file2
Q 0.4402 -0.49655 -0.19745
Q 0.4392 -0.4938 -0.1963
Q 0.43845 -0.4914 -0.19525
Q 0.43775 -0.48955 -0.1947
Q 0.4373 -0.4882 -0.19495
Q 0.43705 -0.48725 -0.1964

$ awk ' { print } NR % 3 == 0 { getline < "file2" ; print } ' file1
O -6.230600 3.314400 -12.287100
H -6.119800 3.047300 -11.368100
H -6.533100 4.204800 -12.096600
Q 0.4402 -0.49655 -0.19745
O 12.104800 2.198100 -15.244801
H 13.015100 2.087200 -15.000700
H 11.760301 2.773000 -14.558399
Q 0.4392 -0.4938 -0.1963
O 2.163200 14.690200 -12.447701
H 1.316700 14.269799 -12.596399
H 1.994400 15.476899 -13.053499
Q 0.43845 -0.4914 -0.19525
O 15.386072 6.185700 10.372772
H 14.869772 6.957000 10.262774
H -15.260300 6.326600 11.209572
Q 0.43775 -0.48955 -0.1947
O -0.915700 -6.342100 10.467173
H -0.108700 -6.814900 10.350771
H -1.477300 -6.455300 9.654371
Q 0.4373 -0.4882 -0.19495
O 6.318500 9.950600 2.828700
H 6.415500 8.971100 2.673700
H 7.237900 10.170699 2.589300
Q 0.43705 -0.48725 -0.1964
Hope this helps.
 
Old 07-05-2010, 09:14 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
druuna is on the money, it can be a little shorter but you get the drift:
Code:
awk '!(NR % 4){getline < "file2"}1' file1
Of course you need to redirect to new file.
 
1 members found this post helpful.
Old 07-05-2010, 09:19 AM   #14
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
@grail: Nice!

A bit unreadable (for the not so awk-savvy), but a very elegant solution!
 
Old 07-05-2010, 09:33 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
A bit unreadable (for the not so awk-savvy)
Yeah my bad ... just like to play
 
  


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
Using GAWK to combine files paragkalra Programming 4 11-14-2009 07:43 AM
How would you combine files excluding the lines that are different? darcman Linux - Software 10 01-19-2009 07:23 PM
howto combine pdf files JUbuntu Linux - Newbie 2 04-29-2008 08:15 PM
combine shadow files packets Linux - Newbie 2 04-18-2008 05:14 AM
Best way to combine files? JockVSJock Programming 4 04-25-2004 07:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:02 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