LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-28-2011, 06:16 AM   #1
appilu
Member
 
Registered: Jan 2011
Distribution: RedHat,Debian-Ubuntu,Fedora
Posts: 73

Rep: Reputation: 8
Smile Shell SCript


HI,

is ther any method to remove first2 and last2 lines of a file or first & last lines of a file using shell script.

thnks in advance.
 
Old 04-28-2011, 06:27 AM   #2
omgs
Member
 
Registered: Dec 2010
Posts: 64

Rep: Reputation: 6
tail -n +<lines_at_top> <file> |head -n -<lines_at_bottom>
 
Old 04-28-2011, 06:31 AM   #3
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Rep: Reputation: 12
Quote:
Originally Posted by omgs View Post
tail -n +<lines_at_top> <file> |head -n -<lines_at_bottom>
if i do that

Code:
# tail -n +2 test.txt | head -n -2
two
three
four
test.txt
Code:
one
two
three
four
five
six
So what you're sayinh is not right , or i'm doing something wrong

Last edited by brownie_cookie; 04-28-2011 at 06:33 AM.
 
Old 04-28-2011, 06:34 AM   #4
omgs
Member
 
Registered: Dec 2010
Posts: 64

Rep: Reputation: 6
If I understood correctly, it does *REMOVE* "one", "two" (the first two lines) and "five", "six", the last two. What is wrong?
 
Old 04-28-2011, 06:36 AM   #5
appilu
Member
 
Registered: Jan 2011
Distribution: RedHat,Debian-Ubuntu,Fedora
Posts: 73

Original Poster
Rep: Reputation: 8
Smile

Quote:
Originally Posted by omgs View Post
tail -n +<lines_at_top> <file> |head -n -<lines_at_bottom>
thnks omgs,it worked.
can i redirect it to another file like. tail -n +<lines_at_top> <file> |head -n -<lines_at_bottom> >sample.txt
 
Old 04-28-2011, 06:41 AM   #6
omgs
Member
 
Registered: Dec 2010
Posts: 64

Rep: Reputation: 6
Of course you can do whatever you can do with any standard output
 
Old 04-28-2011, 06:53 AM   #7
appilu
Member
 
Registered: Jan 2011
Distribution: RedHat,Debian-Ubuntu,Fedora
Posts: 73

Original Poster
Rep: Reputation: 8
Quote:
Originally Posted by omgs View Post
Of course you can do whatever you can do with any standard output
but doing so provides a blank file.
 
Old 04-28-2011, 07:04 AM   #8
omgs
Member
 
Registered: Dec 2010
Posts: 64

Rep: Reputation: 6
Then you're doing something wrong, like maybe naming the destination file just like the source. I've tried and it works.
 
Old 04-28-2011, 10:25 AM   #9
speedy64
LQ Newbie
 
Registered: Apr 2011
Location: Kentucky
Distribution: Ubuntu 10.10
Posts: 7

Rep: Reputation: 0
How about trying:

Code:
sed '1d;$d' file_name > temp_file
mv temp_file file_name
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
Shell script calling shell script - List of all nikunjbadjatya Programming 7 04-13-2011 06:27 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 01:45 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM


All times are GMT -5. The time now is 08:23 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration