19

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
6
  • What do you mean by in-place? If you mean "generate no temp file", I actually don't care. I guess I can rename the resulting file afterwards.
    – Arne
    Commented Jun 27, 2012 at 13:13
  • Yes, I was wondering if you wanted to modify the file itself in-place.
    – Levon
    Commented Jun 27, 2012 at 13:17
  • Btw, I also tried sed '1 /^$/d', because the sed manual says, commands can be restricted by prefixing them with a line number. However, my GNU sed does not like this. Any hints?
    – Arne
    Commented Jun 27, 2012 at 13:20
  • Sorry, I'm more comfortable with awk, not so much sed which I only use use occassionally.
    – Levon
    Commented Jun 27, 2012 at 13:23
  • I think the awk solution does what you want though, no?
    – Levon
    Commented Jun 27, 2012 at 13:24

5 Answers 5

31

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;}'

5
  • That's what I was looking for. I did not realize, I had to use the curly braces. sed's documentation is a bit lacking...
    – Arne
    Commented Jun 27, 2012 at 20:00
  • 1
    @Arne Once you decided to do it in-place and to use sed, you accepted the better answer. (And I learned a lot about sed today :) Glad it all worked out.
    – Levon
    Commented Jun 27, 2012 at 20:19
  • 2
    BSD/macOS sed expects newlines in the command; this works: `sed $'1{\n/^$/d\n}' Commented Aug 1, 2018 at 15:32
  • On macOS, It seems only the second \n is required: sed $'1{/^$/d\n}'.
    – Míng
    Commented Dec 1, 2022 at 2:28
  • You don't really need a newline in bsd sed, just a terminator. This should work: sed '1{/^$/d;}' Answer edited. Commented Dec 1, 2022 at 16:36
4

Using sed, try this:

sed -e '2,$b' -e '/^$/d' < somefile

or to make the change in place:

sed -i~ -e '2,$b' -e '/^$/d' somefile
2
  • 1
    Note that this answer doesn't require GNU sed like the others do.
    – Rob Davis
    Commented Jun 27, 2012 at 16:24
  • You get a point for that. When I'm on a Solaris machine, I'll use your answer. On my Mac, I have gsed installed, but it's nice to have a solution that works with BSD sed as well.
    – Arne
    Commented Jun 27, 2012 at 20:03
3

If you don't have to do this in-place, you can use awk and redirect the output into a different file.

awk '{if (NR==1 && NF==0) next};1' somefile

This will print the contents of the file except if it's the first line (NR == 1) and it doesn't contain any data (NF == 0).

NR the current line number,NF the number of fields on a given line separated by blanks/tabs

E.g.,

$ cat -n data.txt
     1  
     2  this is some text
     3  and here
     4  too
     5  
     6  blank above
     7  the end

$ awk '{if (NR==1 && NF==0) next};1' data.txt | cat -n
     1  this is some text
     2  and here
     3  too
     4  
     5  blank above
     6  the end

and

cat -n data2.txt
     1  this is some text
     2  and here
     3  too
     4  
     5  blank above
     6  the end

$ awk '{if (NR==1 && NF==0) next};1' data2.txt | cat -n
     1  this is some text
     2  and here
     3  too
     4  
     5  blank above
     6  the end

Update:

This sed solution should also work for in-place replacement:

sed -i.bak '1{/^$/d}'  somefile

The original file will be saved with a .bak extension

2
  • Your sed version does not work -- it kills the first empty line it encounters. But the awk version worked fine. I'll update my question with the small wrapper script I wrote around the awk call, to make it seem in-place.
    – Arne
    Commented Jun 27, 2012 at 13:42
  • @Arne .. sorry, I pasted the wrong string - my answer has been updated with the correct sed line :-/ ... just bugged me not to have a sed solution earlier.
    – Levon
    Commented Jun 27, 2012 at 16:20
1

Delete the first line of all files under the actual directory if the first line is empty :
find -type f | xargs sed -i -e '2,$b' -e '/^$/d'

0

This might work for you:

sed '1!b;/^$/d' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.