Solved

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

Posted on 2011-03-01
5
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

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.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

690 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