Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Sed problem

Posted on 2001-07-25
6
Medium Priority
?
232 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 150 total points
ID: 6363284
Just using sed ...
  sed '$d' myfile | sed '$d'

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

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…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

649 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