removing the first and last lines of a flat file in unix

Posted on 2012-09-04
Last Modified: 2012-09-04

 How do I remove the firsta nd last lines of a flat file in unix korn shell?
Question by:LuckyLucks
    LVL 84

    Accepted Solution

    mv file file.$$
    sed -n -e x -e '3,$p' file.$$ > file
    LVL 40

    Assisted Solution

    len=`wc -l filename`
    len=`expr $len - 1`
    head -n $len $filename > newfile
    len=`expr $len - 1`
    tail -n $len newfile > newfile2

    Expert Comment

    By line number (1d deletes first line, $d deletes last line):

    sed -i'' -e '1d' -e '$d' filename

    Author Comment

    Is that " (opening double quoute) after -i? I tried sed -i -e '1d' -e '$d' myfile and I got roor - illegal option -i
    LVL 84

    Expert Comment

    That's '' not "

    Featured Post

    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

    Join & Write a Comment

    I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

    728 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

    18 Experts available now in Live!

    Get 1:1 Help Now