Solved

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

Posted on 2011-03-01
5
349 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 11

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

18 Experts available now in Live!

Get 1:1 Help Now