Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-03-01
5
Medium Priority
?
362 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 1000 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…

604 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