2
$ find ~/AppData/Local/atom/ -name atom.sh -type f | grep -FzZ 'cli/atom.sh' | sed '/^\s*$/d' | cat -n
     1  /c/Users/devuser/AppData/Local/atom/app-1.12.1/resources/cli/atom.sh
     2  /c/Users/devuser/AppData/Local/atom/app-1.12.2/resources/cli/atom.sh
     3

I tried a number of sed/awk-based options to get rid of the blank line. (#3 in the output). But I can't quite get it right...

I need to get the last line into a variable...

The below actual command I am working with fails to give an output...

 find ~/AppData/Local/atom/ -name atom.sh -type f | grep -FzZ 'cli/atom.sh' | sed '/^$/d' | tail -n 1
Community
  • 1
  • 1
deostroll
  • 11,661
  • 21
  • 90
  • 161
  • Okay, its a dirty way, but a combination of reverse sorting and `head -n 1` temporarily solved the problem... – deostroll Nov 14 '16 at 06:09

3 Answers3

9

Below sed command would remove all the empty lines.

sed '/^$/d' file

or

Below sed command would remove all the empty lines and also the lines having only spaces.

sed '/^[[:blank:]]*$/d' file

Add -i parameter to do an in-place edit.

sed -i '/^[[:blank:]]*$/d' file
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

Not all sed's support the token \s for any whitespace character.

You can use the character class [[:blank:]] (for space or tab), or [[:space:]] (for any whitespace):

sed '/^[[:blank:]]*$/d'
sed '/^[[:space:]]*$/d'
heemayl
  • 39,294
  • 7
  • 70
  • 76
0

The problem is the -Z flag of grep. It causes grep to terminate matched lines with a null character instead of a newline. This won't work well with sed, because sed processes input line by line, and it expects each line to be terminated by a newline. In your example grep doesn't emit any newline characters, so as far as sed is concerned, it receives a block of text without a terminating newline, so it processes it as a single line, and so the pattern /^\s*$/ doesn't match anything.

Furthermore, the -z flag would only make sense if the filenames in the output of find were terminated by null characters, that is with the -print0 flag. But you're not using that, so the -z flag in grep is pointless. And the -Z flag is pointless too, because that should be used when the next command in the pipeline expects null-terminated records, which is not your case.

Do like this:

find ~/AppData/Local/atom/ -name atom.sh -type f -print0 | grep -zF 'cli/atom.sh' | tr '\0' '\n' | tail -n 1
janos
  • 120,954
  • 29
  • 226
  • 236