> sed and grep | Unix Linux Forums | Linux

  Unix/Linux Go Back    


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

sed and grep

Linux


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-20-2013
ravisingh ravisingh is offline
Banned User ID
 
Join Date: Feb 2013
Last Activity: 4 December 2015, 5:17 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:
sed and grep edit by bakunin: please use CODE-tags
Sponsored Links
    #2  
Old Unix and Linux 02-20-2013
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 23 July 2016, 11:57 AM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,084
Thanks: 83
Thanked 1,283 Times in 967 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 Unix and Linux 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 Unix and Linux 04-11-2013
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 July 2016, 12:37 PM EDT
Location: San Jose, CA, USA
Posts: 8,989
Thanks: 405
Thanked 2,949 Times in 2,549 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:
sed and grep 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 Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 12:57 PM.