Solved

Sed problem

Posted on 2001-07-25
6
222 Views
Last Modified: 2010-04-21
I want to erase the last two lines from a file. I am trying to do this with a "sed -n '$d' myfile" but this does nothing. Any help will be appreciated !
0
Comment
Question by:CyberGod
6 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6319268
awk '{x[NR]=$0}END{while(n<NR-1){print x[n++];}}' file
sed -ne '1,'`wc -l file|awk '{print $1-2}'`' p' file
head -`wc -l x|awk '{print $1-2}'` file
0
 
LVL 5

Expert Comment

by:garboua
ID: 6319392
I don't think I would use sed for that.

I would write a script like:

#!/bin/sh -p

# Find the number of lines in the file
numlines=`cat $1 | wc -l`

# Subtract 2 lines
numlines=`expr $numlines - 2`

# Copy the first 'numlines' to a temp file
head -$tocopy $1 > /usr/tmp/cpfile

# Move the temp file back over the original
mv /usr/tmp/cpfile $1

But that is just me :-)
0
 
LVL 5

Accepted Solution

by:
ecw earned 50 total points
ID: 6363284
Just using sed ...
  sed '$d' myfile | sed '$d'

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:CyberGod
ID: 6363806
still not working
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6365504
what is not working?

BTW, my 3'rd suggestion has a type, should be:
  head -`wc -l file|awk '{print $1-2}'` file
0
 
LVL 5

Expert Comment

by:ecw
ID: 6367734
I'm surprised
  sed '$d' myfile | sed '$d'
doesn't work, it does for me.  Do you really want to erase the last two lines from the file, or just remove the last two lines when "catting" the file.  Sed doesn't do in-situ editting.  For that, use ed(1), eg.
  echo '$-1,$d\nw\nq' | ed -s - myfile


0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now