Solved

Switch first 10 rows in a file with the last 10 rows using shell script

Posted on 2011-03-01
5
351 Views
Last Modified: 2012-06-27
If I have a file called file1.txt and it looks like this

/path/file1.txt
7.363E+07
7.363E+07
7.363E+07
7.363E+07
7.363E+07
7.363E+07
7.363E+07
7.363E+07
1.473E+08
6.627E+09
4.607E+07
4.607E+07
4.607E+07
4.607E+07
4.607E+07
4.607E+07
4.607E+07
4.607E+07
9.215E+07
4.147E+09

I want it to look like this

/path/file1.txt
4.607E+07
4.607E+07
4.607E+07
4.607E+07
4.607E+07
4.607E+07
4.607E+07
4.607E+07
9.215E+07
4.147E+09
7.363E+07
7.363E+07
7.363E+07
7.363E+07
7.363E+07
7.363E+07
7.363E+07
7.363E+07
1.473E+08
6.627E+09
0
Comment
Question by:libertyforall2
5 Comments
 
LVL 8

Expert Comment

by:pwust
ID: 35012470
Do I get you right that the input file also can hold more than 20 lines, and you still want to swap first ten with last ten lines?
0
 
LVL 58

Accepted Solution

by:
amit_g earned 250 total points
ID: 35012613

tail -10 /path/file1.txt > /tmp/$$.$$ && head -10 /path/file1.txt >> /tmp/$$.$$ && mv /tmp/$$.$$ /path/file1.txt

Open in new window

0
 
LVL 8

Assisted Solution

by:pwust
pwust earned 250 total points
ID: 35012678
or, without tempfile, and taking into account files of more than 20 lines:
tail -n10 /path/file1;head -n$((`wc -l /path/file1|cut -d" " -f1`-10)) /path/file1 |tail -n$((`wc -l /path/file1|cut -d" " -f1`-20));head -n10 /path/file1

Open in new window

0
 

Author Closing Comment

by:libertyforall2
ID: 35013031
Works great!
0
 
LVL 12

Expert Comment

by:tel2
ID: 35020995
Am I too late for the scripting party?

Here's a slightly more concise version:
(tail -10 /path/file1.txt;head -10 /path/file1.txt) > /tmp/$$.$$ && mv /tmp/$$.$$ /path/file1.txt

Open in new window

0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article will help you understand what HashTables are and how to use them in PowerShell.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question