Solved

Sed problem

Posted on 2001-07-25
6
223 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 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…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

929 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

14 Experts available now in Live!

Get 1:1 Help Now