LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 02-02-2007, 01:41 PM   #1
khdubya
LQ Newbie
 
Registered: Feb 2007
Posts: 2

Rep: Reputation: 0
Interlacing the lines of two text files


I am trying to merge two large text files. I need to write twenty lines from one file, and then one line from another. I dont know if I should be using sed or awk or something else. Can anyone provide a tip?
 
Old 02-02-2007, 01:45 PM   #2
Dox Systems - Brian
Member
 
Registered: Nov 2006
Posts: 344

Rep: Reputation: 31
head and/or tail, combined with the appropriate pipes might get you what you need.
 
Old 02-02-2007, 03:15 PM   #3
khdubya
LQ Newbie
 
Registered: Feb 2007
Posts: 2

Original Poster
Rep: Reputation: 0
Ok i put some heads and tails in a loop but it is not working out so well. The files are 400 megabytes so it goes way too slow after the first few thousand. Is there a simple way to do it that wont require me to load up the files everytime I loop?
 
Old 02-03-2007, 05:27 PM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi, khdubya.

If you are still interested in doing this, I have been working on a rudimentary perl code that may work.

Consider the two files:
Code:
File: data1

  1 one
  2 two
  3 three
  4 four
  5 five
  6 six
  7 seven
  8 eight
  9 nine
 10 ten

File: data2

  1 alpha
  2 beta
  3 gamma
The perl code would be called with a series of arguments like: filename:line-count (default:1) and produces output like so:
Code:
% ./shuffle data1:3 data2
one
two
three
alpha
four
five
six
beta
seven
eight
nine
gamma
ten
I timed it on sample files for your ratio l1:20 l2 of (line count) size:
Code:
 14547 l1
   766 l2
to get:
Code:
real    0m0.044s
user    0m0.031s
sys     0m0.006s
The drawback is that while the code seems to work, I have not finished it, there could be bugs in it, and so I provide no warranty.

Let me know if you'd like me to post it; about 80 lines ... cheers, makyo
 
  


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
Grab text lines in text file LULUSNATCH Programming 1 12-02-2005 10:55 AM
Splitting Text lines? 0aniel Programming 9 11-30-2005 03:08 AM
reverse order some lines of text files Melsync Programming 4 09-20-2005 04:40 PM
checking for duplicate lines in text files (vb.net) mrobertson Programming 11 08-01-2005 12:40 PM
awk text that is on several lines homey Programming 2 10-31-2004 09:27 AM

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

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