shell script to delete 100 lines and save as same filename in Linux

Posted on 2010-01-12
Medium Priority
Last Modified: 2013-12-06
Hi, I want to have a shell script to edit and delete 100 lines everyday and as same filename, this is something like:

vi del100.log
100 dd

P.S: This log file is active all the time and lock by the application, but no problem to delete 100 lines by manually.
Question by:silterra
  • 3
  • 2
  • 2
LVL 16

Expert Comment

by:Peter Kwan
ID: 26291429
You may perform this:

tail +101 del100.log | tee del100.log
LVL 10

Assisted Solution

Martin_J_Parker earned 300 total points
ID: 26293420
or you can use the vi commands via the stream editor, sed.
With sed you don't need the colon:
cat del100.log | sed -e "1,100d" > del100.log

Author Comment

ID: 26299817
Hi pkwan, Martin, there is some mis-understanding, in fact what I want is using vim to run, see below:

vim '+1,100dd' '+wq' del100.log

I failed to delete first 100 lines, what was wrong my script??
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

LVL 16

Expert Comment

by:Peter Kwan
ID: 26301329
I think vim does not recognize "+1,100dd" as a valid command.

You may try if the following works:

vim '+d100' '+wq'  test.txt
LVL 16

Accepted Solution

Peter Kwan earned 1200 total points
ID: 26301339
Sorry, should be:

vim '+d100' '+wq'  del100.log

Author Closing Comment

ID: 31675979
LVL 10

Expert Comment

ID: 26301608
Looks like you have the numbers and the d in the wrong place.
It should be:
vim  '+1,100d'  '+wq'  del100.log

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

Creating a Samba server for a small office. Ubuntu Linux and Samba can breathe new life into a retired PC and save an office money on new hardware/software. Our example server will have two hard disks, one exclusively for storing shared data. …
In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…

593 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