LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 10-07-2008, 05:05 AM   #1
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Rep: Reputation: 32
Script: SED for Copy/Paste Lines from Files


I have two files, say, one.dat and two.dat. I want sed to find the line

Code:
extern MY_FLOAT ...;
in one.dat and then copy the "MY_FLOAT ...;" (without the quotes, of course) to two.dat exactly on the line before
Code:
int main(
. The ellipsis ... indicates a sequence of variables (e.g. a,b,c,d --- which varies from one one.dat file to another). The line with extern MY_FLOAT is always closed by a semicolon and the so should the pasted counterpart in two.dat.

I know it's possible but sed is complex and I know only the very basics.

Thanks!
 
Old 10-07-2008, 05:42 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,506

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
An hybrid solution using sed and grep:
Code:
sed "/int main[(]/ i/$(grep ^extern\ MY_FLOAT one.dat)" two.dat
this insert the line matching "extern MY_FLOAT" in one.dat, above the line matching "int main(" in two.dat. Surely there is a better way to do.
 
Old 10-07-2008, 06:12 AM   #3
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Original Poster
Rep: Reputation: 32
And then another line for sed to remove the "extern" from the pasted line in two.dat:

Code:
sed 's/extern\ MY_FLOAT/MY_FLOAT/' two.dat
Works perfectly, thanks! Only one change should be made:

Code:
sed "/int main[(]/ i $(grep ^extern\ MY_FLOAT one.dat)" two.dat
without the forward slash after i (otherwise the forward slash is used as pasted text).

Last edited by unihiekka; 10-07-2008 at 07:27 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Sed command to print matching lines and 2 lines above.. DX398 Programming 12 10-01-2008 08:25 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
CLI copy & paste between text files? David the H. Linux - General 3 09-15-2004 12:05 PM
shell script to copy lines from a file Warmduvet Programming 2 09-14-2004 09:25 PM
how to copy and paste between files and within files Bheki Linux - General 1 02-05-2002 05:29 AM


All times are GMT -5. The time now is 03:52 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration