LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-30-2012, 05:00 AM   #1
Alexos
LQ Newbie
 
Registered: Jan 2012
Posts: 4

Rep: Reputation: Disabled
[bash] transfer last line to first place


I want to transfer last line in the file to the first place, without processing all file. Is it possible?
For example if I have
Code:
Line 1...
Line 2...
....
Line n-1...
Line n...
I want to receive
Code:
Line n...
Line 1...
Line 2...
....
Line n-1...
Or if it is impossible, how can I get
Code:
Line n...
Line 2...
...
Line n-1...
 
Old 01-30-2012, 05:45 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Maybe using something like
Code:
cat <(tail -n1 file) <(head -n-1 file)
this requires redirecting the output to a temporary file and rename it later. How many lines are in the file?
 
Old 01-30-2012, 06:30 AM   #3
Alexos
LQ Newbie
 
Registered: Jan 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
How many lines are in the file?
unknown
 
Old 01-30-2012, 07:10 AM   #4
Alexos
LQ Newbie
 
Registered: Jan 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
OK, solved)
Code:
size=`wc -l file.in | awk '{print $1}'`
let size--
cat <(tail -1 file.in) <(head -$size file.in) > file.out
 
Old 01-30-2012, 08:45 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Nice! Actually the size calculation is not necessary since option -n of the head command accepts negative numbers to print all but the last N lines of each file. Specifying
Code:
head -n-1
as in my example, will exclude the last line of the file independently from the total length. My concern about the number of lines was related only to performance.
 
1 members found this post helpful.
Old 01-31-2012, 04:46 AM   #6
Alexos
LQ Newbie
 
Registered: Jan 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Great! thanks. but I found easier method again) without creating new files
Code:
total=`tail -n 1 "file"`
sed -i -e '$d' -e "1c\\$total\n" "file"

Last edited by Alexos; 01-31-2012 at 05:24 AM.
 
Old 01-31-2012, 06:31 AM   #7
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Here is even simpler solution:
Code:
$ cat infile2.txt 
Line 1...
Line 2...
....
Line n-1...
Line n...
$ echo -e '$m0\nw' | ed infile2.txt 
47
47
$ cat infile2.txt 
Line n...
Line 1...
Line 2...
....
Line n-1...
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Phone Line Modem -> Phone Line Server Transfer Files renthemighty Linux - Newbie 1 07-29-2011 09:07 PM
How does - line no to address conversion- take place in Kernel-debugger? topworld Linux - Software 2 03-20-2006 06:40 AM
How does - line no to address conversion- take place in Kernel-debugger? topworld Programming 0 03-20-2006 01:21 AM
Where's the best place to learn Bash? evangelinux Programming 5 06-13-2004 11:45 PM
Application to place single space with % between each line MasterC Linux - General 2 05-18-2003 01:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration