Solved

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

Posted on 2011-03-01
5
350 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 Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

867 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now