2

I tried to use 'sed' command to remove the empty lines.

sed -i '/^$/d' file.txt

My sample txt file looks likes this. The second line has space characters. sed command only removes the empty lines but not the lines with white space.

Sample text

Sample text

So is there away to accomplish this via bash.

My intended out put is

Sample text
Sample text
Mad Physicist
  • 95,415
  • 23
  • 151
  • 231
Nirojan Selvanathan
  • 8,618
  • 5
  • 54
  • 73
  • What's your intended output, **exactly**? Do you want `testline`? Do you want any line with any whitespace in it at all to be deleted? – Charles Duffy Jan 09 '17 at 04:18
  • 3
    try `sed '/^\s*$/d'` – Michael Coker Jan 09 '17 at 04:18
  • 1
    ...should the file just contain `testlinetestline`, no whitespace *including* newlines? (That's actually *really* easy, being a job `tr` can do). – Charles Duffy Jan 09 '17 at 04:18
  • My intended output is (test line \n test line) I want the second empty line with spaces to be removed in the output file. – Nirojan Selvanathan Jan 09 '17 at 04:19
  • 2
    @MichaelCoker, `\s` isn't defined by BRE or ERE; `[[:space:]]` is the portable way to write it. – Charles Duffy Jan 09 '17 at 04:19
  • 2
    @NirojanSelvanathan, ...re: making intended output clear, edit the question with that. You might also want to include a line that *should* be kept in the sample content, just so someone trying to be funny can't tell you to write `> yourfile` as an answer that works with the test data given. – Charles Duffy Jan 09 '17 at 04:20

2 Answers2

7

Use character class [:blank:] to indicate space or tab:

With sed:

sed -i '/^[[:blank:]]*$/ d' file.txt

With perl:

perl -ne 'print if !/^[[:blank:]]*$/' file.txt 

With awk:

awk '!/^[[:blank:]]*$/' file.txt 

With grep:

grep -v '^[[:blank:]]*$' file.txt

If the tool does not support editing in-place, leverage a temporary file e.g. for grep:

grep -v '^[[:blank:]]*$' file.txt >file.txt.tmp && mv file.txt{.tmp,}
heemayl
  • 35,775
  • 6
  • 62
  • 69
2
sed -i '/^ *$/d' file.txt

or to also match other white space characters such as tabs, etc:

sed -i '/^[[:space:]]*$/d' file.txt

the * character matches 0 or more instances of preceding character

user122992
  • 21
  • 3
  • `\s` for any type of whitespace, not just spaces. – Mad Physicist Jan 09 '17 at 04:25
  • Or `[[:space:]]` for a preference. – Mad Physicist Jan 09 '17 at 04:26
  • 1
    `\s` isn't guaranteed to work in POSIX `sed`. This is a place where unless there's a specific implementation specified in the question that's known to support PCRE extensions, I'd argue that specifying anything other than the POSIX BRE syntax is *wrong*. – Charles Duffy Jan 09 '17 at 04:29
  • 1
    I just tested `'/^\s*$/d'` on Mac OS X 10.11.6, GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin15) and it didn't match some lines with just spaces, but the following did work: `'/^[[:space:]]*$/d'` – user122992 Jan 09 '17 at 04:30
  • 1
    @user122992, ...to be clear, btw, it's the version of `sed`, as opposed to the version of bash, that matters. – Charles Duffy Jan 09 '17 at 04:31