Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8146699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:05:42+00:00 2026-06-06T14:05:42+00:00

How can I delete the first (!) line of a text file if it’s

  • 0

How can I delete the first (!) line of a text file if it’s empty, using e.g. sed or other standard UNIX tools. I tried this command:

sed '/^$/d' < somefile

But this will delete the first empty line, not the first line of the file, if it’s empty. Can I give sed some condition, concerning the line number?

With Levon’s answer I built this small script based on awk:

#!/bin/bash

for FILE in $(find some_directory -name "*.csv")
do
    echo Processing ${FILE}

    awk '{if (NR==1 && NF==0) next};1' < ${FILE} > ${FILE}.killfirstline
    mv ${FILE}.killfirstline ${FILE}

done
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:05:44+00:00Added an answer on June 6, 2026 at 2:05 pm

    The simplest thing in sed is:

    sed '1{/^$/d}'
    

    Note that this does not delete a line that contains all blanks, but only a line that contains nothing but a single newline. To get rid of blanks:

    sed '1{/^ *$/d}'
    

    and to eliminate all whitespace:

    sed '1{/^[[:space:]]*$/d}'
    

    Note that some versions of sed require a terminator inside the block, so you might need to add a semi-colon. eg sed '1{/^$/d;}'

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm opening a text file and removing the first line to prepare it for
How can I delete the hidden cropped elements from a pdf from command line?
How can I delete all the listed files using PHP? As shown in the
I want to parse the text line from the Wavefront OBJ file . Currently
Given a text file, how would I go about reading an arbitrary line and
In vim when my cursor is on the first line I can press: 100dd
At first my table view is empty, and then you can add your own
I wonder if there is an efficient way to delete the first line in
I am reading data files in text format using readLines . The first 'column'
How can I delete a line without putting it into my default buffer? Example:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.