Solved

Sed problem

Posted on 2001-07-25
6
224 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
partition customisation for kickstart file 2 94
Debian 3.16.7 3 149
Understanding Futures with busy waiting 2 88
awk sed 8 42
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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