LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-28-2008, 11:38 AM   #1
LinuxCrayon
Member
 
Registered: Nov 2007
Location: Georgia, USA
Distribution: FreeBSD
Posts: 274

Rep: Reputation: 31
Copy from file1 to file2 - Trying sed


I'm learning to program in an environment without a GUI. During the course of my programming, I've found more and more that it would be better from an organization standpoint to move certain things from one file to another. At first, it was simple one or two line stuff that I could manually type out. Now, however, I'm trying to copy 78 lines of code, and that's just not going to work by hand. So I'm trying to use sed to do it for me. Right now, here's my code:

Code:
sed '1,78 p' file1 > file2
It works...in a sense. Except that for some weird reason, it copies the top portion 8 times, then the upper middle four times, lower middle 2 times, and the bottom part is copied correctly.

If there's a better way to copy only lines 1 through 78 from file1 to file2, I'd be interested in hearing it.

Thanks!
 
Old 03-28-2008, 11:54 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
SED prints everything unless told otherwise. In your example, it prints lines 1 thru 78 twice.

How about:
sed '79,$ d' file1 > file2

SED tutorial here:
http://www.grymoire.com/Unix/Sed.html
 
Old 03-28-2008, 12:29 PM   #3
LinuxCrayon
Member
 
Registered: Nov 2007
Location: Georgia, USA
Distribution: FreeBSD
Posts: 274

Original Poster
Rep: Reputation: 31
Thanks, Pixellany.

i was able to accomplish everything I needed between your advice and that tutorial!
 
Old 03-30-2008, 07:51 AM   #4
Tischbein
Member
 
Registered: Oct 2006
Distribution: debian
Posts: 124

Rep: Reputation: 15
Quote:
Originally Posted by pixellany View Post
SED prints everything unless told otherwise. In your example, it prints lines 1 thru 78 twice.
To print nothing unless explicitly commanded to, use the -n flag:

Code:
sed -n '5p'
Ha det.

Regards, Pute.
 
  


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
How to read keywords from file 1 and replace them in file2? Sid2007 Programming 8 10-09-2007 02:47 AM
[Comp] How to create a file from the differences between file1 and file2 Xeratul Linux - Software 2 11-19-2006 01:20 PM
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
Finding lines in file1,but not in file 2 subu_s Programming 2 12-14-2004 09:56 AM
Avoid cat file1>>file2 automatic add return after file2. AshesOfTime Programming 5 11-25-2004 07:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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