Solved

Sed problem

Posted on 2001-07-25
6
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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