Unix/Linux Go Back    


UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

Re-arranging lines of text... help?

UNIX for Dummies Questions & Answers


 
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-25-2009
shorte85 shorte85 is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 6 March 2009, 11:30 PM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Error Re-arranging lines of text... help?

Hello,

I am new to Linux, and I am learning slowly but for surely. I am trying to currently figure out how to go about re-arranging lines of text of a *.txt file... Like for example:

Say pool.txt has 20 lines of text, and the last 5 lines of text I want to move to the top of the *.txt file and take the first 5 lines of *.txt file and move them to the bottom (more less swapping the lines) and then save it to a different file name to pool2.txt, how would I go about doing that?

I thought just by using the command cat > pool and typing whatever it is I needed, and then ctrl+d so that it enters, I know I did that correctly because when I type in the command cat pool it shows up. But when I try to take the lines I want to swap in place with, within that file it doesn't seem to work.

What am I doing wrong? Any suggestions?Linux
Sponsored Links
    #2  
Old Unix and Linux 02-26-2009
aerostar700 aerostar700 is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 26 February 2009, 12:57 PM EST
Location: France
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Well, basicaly you have 2 ways to do that. If it's just a matter of editing one file once in a while, I'd use vi for that. If you want to do that on a regular basis, a shell script would be better suited.

vi method :

vi pool.txt
(position cursor on the 1st line)
type : 5dd (the first 5 lines will disappear, don't panic)
type : Shift-G (cursor will jump to the last line)
type : p (5 lines will magically reappear at the end of text)
type : 5 and (Up Arrow) (cursor will jump up 5 lines)
type : 5dd (those 5 lines will vanish)
type : :0 (the colon sign and the number 0), that will position you at the top of the file
type : Shift-P (5 lines reappear on top of the file)
type : :w pool2.txt (colon sign and the letter w + name of the new file)
type : :q! (colon sign, the letter q and exclamation mark) to quit without saving the changes to the original file

This seems rather obnoxious, but in fact, it take just about 3 seconds to actually do that Linux

Actually, nobody would want to write a script to do that, vi is quick enough. But if you really want to do it without using vi then :

$ tail -5 pool.txt > pool2.txt ; sed -n '6,15p' pool.txt >>pool2.txt ; head -5 pool.txt >>pool2.txt

... will do the trick nicely Linux

Last edited by aerostar700; 02-26-2009 at 01:58 AM..
Sponsored Links
    #3  
Old Unix and Linux 02-26-2009
shorte85 shorte85 is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 6 March 2009, 11:30 PM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you, I appreciate your help with this!!
Sponsored Links
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replace the text between two lines with different text ukatru UNIX for Advanced & Expert Users 6 08-24-2008 01:17 AM
Arranging files Vivek788 Shell Programming and Scripting 2 06-07-2008 10:18 PM
Help with arranging data file yahyaaa Shell Programming and Scripting 7 05-29-2008 04:59 PM
How to delete first 5 lines and last five lines in all text files ragavendran31 Shell Programming and Scripting 10 02-21-2008 06:58 AM
re-arranging text in a file with AWK th3g0bl1n UNIX for Dummies Questions & Answers 1 02-12-2008 11:54 AM



All times are GMT -4. The time now is 04:00 PM.