LinuxQuestions.org
Help answer threads with 0 replies.
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 05-05-2015, 09:01 AM   #1
usao
Member
 
Registered: Dec 2011
Location: Chandler, AZ
Posts: 258

Rep: Reputation: Disabled
extract middle of file - how to strip header/footer


I have a log file with a header (which I can skip with awk), and a footer, which I need to find a way to remove. The goal is to extract the middle lines from a file. Specifically, there is a header (1 line) and a footer (1 line).
The only way I can figure out how to do this is if I already know how many lines are in the file to begin with. For example, if the file looks like this:
line 1 (header)
line 2 (interesting line)
line 3 (interesting line)
line 4 (footer)
I just want to extract the middle "interesting lines" without the header/footer lines.
I can't use grep to remove the header/footer, because I don't know what those lines will contain, only that they exist and are exactly 1 line each. In general, I don't know how many lines are in the file.
 
Old 05-05-2015, 09:27 AM   #2
usao
Member
 
Registered: Dec 2011
Location: Chandler, AZ
Posts: 258

Original Poster
Rep: Reputation: Disabled
Further research and I think I have answered my own question...

cat file | tail -n +2 | head -n -1
 
1 members found this post helpful.
Old 05-05-2015, 07:59 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,546

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Reading the file multiple times can become costly - especially when it's really big. Try this
Code:
sed '1d;$d' file
 
  


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
kate header footer special fields josephj Linux - General 1 05-18-2009 07:26 PM
Header, footer of a file lawrence_lee_lee Linux - Software 1 10-23-2007 07:26 AM
Apache Header & Footer blueAlien Linux - Server 1 03-07-2007 08:55 PM
Mozilla fails to print header/footer skip_g33k Linux - Software 2 03-23-2006 12:44 PM
Apache config file to add header and footer mindstormsguy Linux - Software 2 05-02-2004 12:48 PM


All times are GMT -5. The time now is 02:59 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration