Welcome, Guest
Username Password: Remember me

Remove blank line using Grep and sed
(1 viewing) (1) Guest
Shell Script
  • Page:
  • 1

TOPIC: Remove blank line using Grep and sed

Remove blank line using Grep and sed 1 year, 4 months ago #529

  • scmuser
  • OFFLINE
  • Gold Boarder
  • scm master
  • Posts: 237
  • Points: 3429
  • Karma: 0
  • Honor Medal 2009
Hi,

How do you remove blank line using grep and sed in shell scripting?

Re: Remove blank line using Grep and sed 1 year, 4 months ago #530

  • rajeshkumar
  • OFFLINE
  • Moderator
  • I love software configuration management
  • Posts: 370
  • Points: 44590
  • Karma: 4
  • Honor Medal 2009
grep -v "^$" filename > newfilename
 

The ^$ within the quotes is a regular expression: ^=beginning of line, $=end of line, with no characters between.

To store output to another file use redirection operator:

$ sed '/^$/d' /tmp/data.txt > /tmp/output.txt
Regards,
Rajesh Kumar
Build and Release Engineer
My Blog: community.scmgalaxy.com/pg/profile/rajeshkumar

Re: Remove blank line using Grep and sed 1 year, 4 months ago #531

  • rajeshkumar
  • OFFLINE
  • Moderator
  • I love software configuration management
  • Posts: 370
  • Points: 44590
  • Karma: 4
  • Honor Medal 2009
The above script will work only if you have no character in the blank line but most of the time it can happen that Tab can be possle in blank line....

so for this situation u should try following..

sed '/^[PRESS TAB]*$/d' filename
Regards,
Rajesh Kumar
Build and Release Engineer
My Blog: community.scmgalaxy.com/pg/profile/rajeshkumar
  • Page:
  • 1
Time to create page: 0.88 seconds

     
    
Home Forum