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 12-07-2006, 12:16 PM   #1
rhelpm
LQ Newbie
 
Registered: Oct 2005
Posts: 4

Rep: Reputation: 0
How to Delete nnn lines from the top of a file with out opening it?


Hi all : - Is there anyway to delete nnn number of lines from the top of a file without opening it? Can it be done using head or tail? I tried tail and piped the output to another file and renamed it to original (after renaming the original to something else). It worked, but the problem is the renamed original is still being written by the application instead of the new file with the original name. This is a 10g AS log file.

Any other ideas on how to shrink that logfile size?

Thanks.
 
Old 12-07-2006, 12:43 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Use sed with the -i option. For example, if you want to remove the top 1000 lines from your file:
Code:
sed -n -i '1001,$ p'
 
Old 12-07-2006, 12:45 PM   #3
crazyjimbo
Member
 
Registered: Mar 2006
Location: Edinburgh
Distribution: Debian Etch, Gentoo
Posts: 90

Rep: Reputation: 16
What is the problem with it being a new file? So long as it contains the correct information, etc, what difference does having it created by a script from the output of another file make?

Maybe ways around this issue can then be suggested.

James

EDIT: Beaten to it by a proper solution!
 
Old 12-07-2006, 01:01 PM   #4
AlbinoJap
Member
 
Registered: Mar 2004
Posts: 36

Rep: Reputation: 15
I agree with jimbo. If it's just a log file you can just rotate it out. It will still contain the info that you need. Check out logrotate. It should get the job done.
 
Old 12-07-2006, 03:04 PM   #5
rhelpm
LQ Newbie
 
Registered: Oct 2005
Posts: 4

Original Poster
Rep: Reputation: 0
Thank you for the response all. Here is what I found somewhere else on the internet: This is using Perl.

perl -i -ne 'print unless 1..500' <file_name> --> this deletes the top 500 lines of that file.

However I am interested in the 'sed' solution suggested by Matthew Gates. I tried but no success. Matthew if you don't mind, can you please give an example:

Regards.
 
Old 12-07-2006, 03:33 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Sure. Say you have a file named "bobdobbs", containing:
Code:
one
two
three
four
five
six
All you need to do to remove the top, say 3 lines is this:
Code:
sed -n -i '4,$ p' bobdobbs
After this command completed, the file bobdobbs contains only:
Code:
four
five
six
It's well worth doing a sed tutorial. If you're like me you'll leave it too late, and when you finally get round to it you'll think "****! If I'd known this a few years ago I would have saved myself a massive amount of work".

Ho hum.
 
Old 12-07-2006, 03:46 PM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Oh, perhaps a little explanation of the sed command might help. As a reminder, here's the command:
Code:
sed -n -i '4,$ p' bobdobbs
-n means "don't print lines to the output file unless specifically requested (with the p command)".

-i means "edit input files in place - don't write output to standard output - save any output to the input files".

The bobdobbs is the name of input file.

The rest is the command. The command is in single quotes to prevent the shell from interpretting meta characters and passing sed something else - sed needs to see this as a literal string.

OK. The command is pretty simple:
Code:
4,$ p
Sed commands can be prefixed with an address. This is to say some expression which tells sed which lines of the input files to execute the rest of the command on. In this case the address is
Code:
4,$
...which means from line 4 to the end of the file. If we said
Code:
4,12
...it would mean from line 4 to line 12 (inclusive). $ is a special address meaning "the last line of the input file".

The second part is the actual operation to be performed on lines which match the address. In this case the command is simply p - print the line. Usually sed will print the line after any operation you specify, but we told it (with the -n option, as described above) not to print output unless we explicitly request it.

So, in summary, 4,$ p means "if the line number is between 4 and the end of the file (inclusive), print the line".

Hope that helps.

Learn sed. It's time well spent.
 
  


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
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 10:19 AM
'N' lines of Top Command? DarkPenguin Linux - General 2 04-11-2006 12:05 PM
Getting multi-colour lines from top to bottom djeanveau Fedora - Installation 3 01-30-2005 09:58 PM
Getting multi-colour lines from top to bottom djeanveau Linux - Newbie 1 01-30-2005 01:02 PM
delete some lines from a file freelinuxcpp Linux - Software 4 01-17-2004 11:28 AM


All times are GMT -5. The time now is 11:04 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