The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
grep unix.com with google



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-15-2009
Registered User
 

Join Date: Oct 2009
Posts: 24
How to replace some content of a file and write with a new name

Hi

I have a control file which looks like this

LOAD DATA
INFILE '/home/scott/XXX.dat'
PRESERVE BLANKS
.............
.............

how can i change the content of this file and replace the file in the second line with anothe file name and write it back with another name to the disk?

Thanks
  #2 (permalink)  
Old 11-15-2009
scottn scottn is offline Forum Staff  
Moderator
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,606

Code:
sed "2s@.*@INFILE 'newfile'@" infile > outfile

  #3 (permalink)  
Old 11-15-2009
Registered User
 

Join Date: Oct 2009
Posts: 24
Thanks buddy one more thing. How can i add a new line before the third line (Which is PRESERVE BLANKS) so the new file will look like this

LOAD DATA
INFILE '/home/scott/XXX.dat'
<New Line>
PRESERVE BLANKS
.............
.............
  #4 (permalink)  
Old 11-15-2009
scottn scottn is offline Forum Staff  
Moderator
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,606

Code:
sed "/INFILE/s@.*@INFILE 'newfile/etc'@;/^PRES/i\ " infile > outfile

Or if you meant <New Line> to be a literal string:

Code:
sed "/INFILE/s@.*@INFILE 'newfile/etc'@;/^PRES/i<New Line>" infile > outfile

Bits Awarded / Charged to scottn for this Post
Date User Comment Amount
11-15-2009 Scrutinizer I like the "i\ " 500
  #5 (permalink)  
Old 11-15-2009
Registered User
 

Join Date: Oct 2009
Posts: 24
Thumbs up

Thanks buddy
  #6 (permalink)  
Old 11-15-2009
Registered User
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 1,257

Code:
sed "s#^\(INFILE\).*#\1 '/path/to/newfile'\n#" infile > outfile

Sponsored Links
Reply

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 Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep a pattern and replace a value in it and write to the same file. yesmani Shell Programming and Scripting 6 09-11-2009 07:57 AM
SED to replace file content godfreyyip Shell Programming and Scripting 6 09-04-2009 07:24 AM
Perl search and replace file content. jxh461 Shell Programming and Scripting 3 04-17-2009 12:26 AM
replace a string with content from another file afatguy Shell Programming and Scripting 4 09-14-2006 10:25 PM
How to replace a variable content josephwong Shell Programming and Scripting 4 06-26-2006 12:18 AM



All times are GMT -4. The time now is 10:05 PM.


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