The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to merge these two files? fedora Shell Programming and Scripting 3 02-12-2008 06:45 PM
merge files koti_rama Shell Programming and Scripting 5 12-24-2007 10:59 PM
How to merge files lestat_ecuador Shell Programming and Scripting 3 06-07-2007 06:45 AM
help in merge files u263066 Shell Programming and Scripting 5 07-24-2006 03:24 AM
Using NAWK to merge two files madhunk Shell Programming and Scripting 7 06-07-2006 10:27 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-28-2007
miwinter miwinter is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 18
use of sed over cat to merge files

I have three files, basically:

file 1 - one line header
file 2 - big data (approx 80GB)
file 3 - a one line trailer

the existing process cats these together i.e cat file 1 file 2 file 3

however... I was thinking, surely it could be more efficient to insert the header (file 1) on the data file (file 2) and then just append the trailer (file 3) on the end.

I wondered if this could be achieved some way via sed instead, i.e. something like:

#!/bin/sh

export HEAD=`cat $1`
export TRAIL=`cat $3`

sed -e '1i\
$HEAD
$a\
$TRAIL' $2 > complete_file.txt

...but it seems this won't substitute in the variable values setup within the shell script already

Thanks in advance for any replies on this one.

Last edited by miwinter; 11-28-2007 at 01:24 PM..
  #2 (permalink)  
Old 11-28-2007
kahuna's Avatar
kahuna kahuna is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 149
Quote:
Originally Posted by miwinter View Post
I wondered if this could be achieved some way via sed instead
You could use sed, but

Quote:
surely it could be more efficient to insert the header (file 1) on the data file (file 2) and then just append the trailer (file 3) on the end.
it's not really more efficient. Sed still needs to read and write all of file 2 and needs to make checks on every line. Cat just needs to read and write, and can do so block by block rather than line by line.
  #3 (permalink)  
Old 11-28-2007
gus2000 gus2000 is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 157
I agree that there's little you can do about making a 80GB edit more efficient.

The reason your "sed" command did not work is because you used single-quotes around the expression, which prevents the variables from being evaluated:

Code:
# i=100
# echo "$i"
100
# echo '$i'
$i
So if you need to reference a variable, you can't use the single-quotes. But if you use double-quotes, you'll need to escape anything that the shell will evaluate but that you need to be literal.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:08 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0