sed and grep | Unix Linux Forums | Linux

  Go Back    


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

sed and grep

Linux


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-20-2013
ravisingh ravisingh is offline
Banned
 
Join Date: Feb 2013
Last Activity: 25 November 2013, 2:56 AM EST
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
Last Activity: 24 July 2014, 11:21 PM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 4,118
Thanks: 43
Thanked 769 Times in 605 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
versd versd is offline
Registered User
 
Join Date: Apr 2013
Last Activity: 27 May 2013, 11:25 AM EDT
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
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 July 2014, 9:12 PM EDT
Location: San Jose, CA, USA
Posts: 4,135
Thanks: 162
Thanked 1,412 Times in 1,197 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
Closed Thread

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 01:15 AM.