Re-arranging lines of text... help?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Re-arranging lines of text... help?
# 1  
Old 02-25-2009
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?Smilie
# 2  
Old 02-26-2009
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 Smilie

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 Smilie

Last edited by aerostar700; 02-26-2009 at 02:58 AM..
# 3  
Old 02-26-2009
Thank you, I appreciate your help with this!!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. UNIX for Beginners Questions & Answers

Help arranging text

By using this code how can we get the stars in inverted positions? str="*" for i in 1 2 3 4 5 do echo "$str" str="$str *" done The output should be like this * * * * * * * * * * * * * * * Please use CODE tags as required by forum rules!... (5 Replies)
Discussion started by: Meeran Rizvi
5 Replies

3. Shell Programming and Scripting

awk to skip lines find text and add text based on number

I am trying to use awk skip each line with a ## or # and check each line after for STB= and if that value in greater than or = to 0.8, then at the end of line the text "STRAND BIAS" is written in else "GOOD". So in the file of 4 entries attached. awk tried: awk NR > "##"' "#" -F"STB="... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. Shell Programming and Scripting

Read n lines from a text files getting n from within the text file

I dont even have a sample script cause I dont know where to start from. My data lookes like this > sat#16 #data: 15 site:UNZA baseline: 205.9151 0.008 -165.2465 35.8109 40.6685 21.9148 121.1446 26.4629 -18.4976 33.8722 0.017 -165.2243 48.2201 40.6908 ... (8 Replies)
Discussion started by: malandisa
8 Replies

5. Shell Programming and Scripting

Need Help in arranging the output

Hello All, Please find attached input and output files. I want to write a shell script to achieve this. I tried using awk but not getting how to do this as I am new to shell programming. Thanks (4 Replies)
Discussion started by: Sudeep Bhattad
4 Replies

6. Shell Programming and Scripting

sed show lines text between 2 blank lines

I have a file like blah blah blah blah this is the text I need, which might be between 1-4 lines, but always has a blank line above and below it, and is at the end of the text file the code tags don't show the trailing blank line. I started by deleting the last blank line with: ... (2 Replies)
Discussion started by: unclecameron
2 Replies

7. Shell Programming and Scripting

Print lines between two lines after grep for a text string

I have several very large file that are extracts from Oracle tables. These files are formatted in XML type syntax with multiple entries like: <ROW> some information more information </ROW> I want to grep for some words, then print all lines between <ROW> AND </ROW>. Can this be done with AWK?... (7 Replies)
Discussion started by: jbruce
7 Replies

8. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

9. Shell Programming and Scripting

How to delete first 5 lines and last five lines in all text files

Hi I want to delete first five and last five lines in text files without opening the file and also i want to keep the same file name for all the files. Thanks in advance!!! Ragav (10 Replies)
Discussion started by: ragavendran31
10 Replies

10. UNIX for Dummies Questions & Answers

re-arranging text in a file with AWK

Hi Gurus, I have a text file that I want to process with the following structure; 4528788 Blah - Something 9341423 Text - Somethinghere 98792223,5546761 Some - More - Text 5119503,5159504,1234567 Text - More - Text 13459695 Stuff - Text Again 13526583 Junk - More Text Here 13595177... (1 Reply)
Discussion started by: th3g0bl1n
1 Replies
Login or Register to Ask a Question