Go Back   The UNIX and Linux Forums > Operating Systems > Linux


Linux RedHat, Ubuntu, SUSE, Fedora, Debian, Mandriva, Slackware, Gentoo linux, PCLinuxOS. All Linux questions here!

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-20-2013
Banned
 
Join Date: Feb 2013
Location: Mumbai
Posts: 106
Thanks: 7
Thanked 2 Times in 2 Posts
sed and grep

I am stranded with a problem. Please solve.

How will you remove blank lines from a file using sed and grep? ( blank line contains nothing or only white spaces).

I run the below commands of sed and grep but grep isn't giving output as desired. Why?


Code:
sed '/^[ \o11]*$/d' blank

grep -v "^[ \o11]*$" blank

(there is a space before tab in character class)

The above sed command gave desired result but not grep. Why?

Also, I am fix at one more place. As, tab is 011 (octal) but in my system the zero of 011 is accepting as o (alphabetic) instead of numeral zero. If I give numeral zero, it won't work. Is it O.K.? A few days back by mistake a system file got deleted becz of which link count is not showing properly. Can this be a reason for the above problem.

Waiting eagerly for your valuable replies.

Moderator's Comments:
edit by bakunin: please use CODE-tags
Sponsored Links
    #2  
Old 02-20-2013
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 3,301
Thanks: 27
Thanked 457 Times in 356 Posts
Quote:
Originally Posted by ravisingh View Post
I run the below commands of sed and grep but grep isn't giving output as desired. Why?


Code:
sed '/^[ \o11]*$/d' blank

grep -v "^[ \o11]*$" blank

I suppose it is because of the way you quote. You should probably single-quote the grep-expression as well as you did with the sed-expression.


Quote:
Originally Posted by ravisingh View Post
Also, I am fix at one more place. As, tab is 011 (octal) but in my system the zero of 011 is accepting as o (alphabetic) instead of numeral zero.
You don't need either representation of tabs, as you can enter them directly when you use single-quotes to protect these characters (replace "<spc>" and "<tab>" with literal spaces/tabs when enering the following):


Code:
sed '/^[<spc><tab>]*$/d' blank

I hope this helps.

bakunin
Sponsored Links
    #3  
Old 04-11-2013
Registered User
 
Join Date: Apr 2013
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
blank lines and whitespaces lines could be removed using this regex:

sed '/^ *$/d'
    #4  
Old 04-11-2013
Registered User
 
Join Date: Jul 2012
Location: San Jose, CA
Posts: 1,485
Thanks: 62
Thanked 535 Times in 468 Posts
Quote:
Originally Posted by ravisingh View Post
I am stranded with a problem. Please solve.

How will you remove blank lines from a file using sed and grep? ( blank line contains nothing or only white spaces).

I run the below commands of sed and grep but grep isn't giving output as desired. Why?


Code:
sed '/^[ \o11]*$/d' blank

grep -v "^[ \o11]*$" blank

(there is a space before tab in character class)

The above sed command gave desired result but not grep. Why?

Also, I am fix at one more place. As, tab is 011 (octal) but in my system the zero of 011 is accepting as o (alphabetic) instead of numeral zero. If I give numeral zero, it won't work. Is it O.K.? A few days back by mistake a system file got deleted becz of which link count is not showing properly. Can this be a reason for the above problem.

Waiting eagerly for your valuable replies.

Moderator's Comments:
edit by bakunin: please use CODE-tags
I find it hard to believe that either of these commands did what you want. The \o11 needs to be \011 (i.e., digit zero; not lowercase letter oh).

To make it clearer to readers, you could also replace your bracket expression with [[:space:]].
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed/tr/grep help flightskoo Shell Programming and Scripting 3 04-17-2012 05:09 AM
use sed or grep? dixits Shell Programming and Scripting 3 02-21-2012 01:31 AM
help with SED + GREP 2001.arun Shell Programming and Scripting 6 06-14-2010 07:43 AM
using sed to grep wxornot Shell Programming and Scripting 1 01-24-2006 11:43 PM
SED and it used with | and grep Lem2003 UNIX for Dummies Questions & Answers 4 02-03-2004 01:26 PM



All times are GMT -4. The time now is 07:09 PM.