Unix administration - Newbie question

This is Interesting: Free IT Magazines  
Home > Archive > Unix administration > October 2005 > Newbie question





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Newbie question
WebGod@aol.com

2005-10-24, 3:49 pm

I have a newbie question. What command I use to see if there are any
extra spaces with in the line by using grep? I assumed I would look
for all line that didn't have a 0-9 a-z A-Z? Is this how I would do
that and how would the command read? Thanks for your help.

Barry Margolin

2005-10-24, 3:49 pm

In article <1129573153.619286.265680@g43g2000cwa.googlegroups.com>,
WebGod@aol.com wrote:

> I have a newbie question. What command I use to see if there are any
> extra spaces with in the line by using grep? I assumed I would look


What are "extra" spaces? How many spaces can you have before the rest
are considered extra?

> for all line that didn't have a 0-9 a-z A-Z? Is this how I would do
> that and how would the command read? Thanks for your help.


grep ' ' filename

will show all the lines in the file that have any spaces in them.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Chris F.A. Johnson

2005-10-24, 3:49 pm

On 2005-10-17, WebGod@aol.com wrote:
> I have a newbie question. What command I use to see if there are any
> extra spaces with in the line by using grep?


What do you mean by "extra spaces"?

> I assumed I would look for all line that didn't have a 0-9 a-z A-Z?


Is that what you want to do?

> Is this how I would do that and how would the command read?


If you want to find lines that contain nothing but whitespace
(spaces and tabs):

grep '^[ ][ ]*$' FILENAME

Each pair of brackets contains a space and a tab.


If you want do do it for a single line, you don't need grep:

case $line in
*[! ]*) echo "Not just whitespace" ;;
*) echo "Just whitespace" ;;
esac

--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========================================
==========================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
WebGod@aol.com

2005-10-24, 3:49 pm

Excellent! Thank you for your help. Also, I apologize for the
multiple postings. Thanks again!

WebGod@aol.com

2005-10-24, 3:49 pm

Sorry I ended that post early...To continue with my question. What I
meant by extra spaces is say I am typing a sentence and I put more
than one space in between like in this sentence within a file. How do
I find that line in the file using grep? Thanks again!

Doug Freyburger

2005-10-24, 3:49 pm

Chris F.A. Johnson wrote:
> WebGod@aol.com wrote:
>

School is back in session and folks are trying to be
creative in getting others to do their homework, right?
Deal is there's an expectation of tit-for-tat. Folks
with a long history of contribution get a lot better
response than newbies. Folks with interesting questions
get a lot better response than folks with questions that
sound like homework.
[vbcol=seagreen]
>
> What do you mean by "extra spaces"?


I figured a single space separates words, a double
space separates sentences, trailing spaces on a line are
always extra, and something sensible should be done
about indentation at the start of paragraphs.

So you'll want to grep for multiple spaces and pipe
that into a grep -v of punctuation followed by two
spaces. That second one is nice practice for regular
expressions. Then you'll want to learn that the dollar
sign is used in regular expressions to stand for end
of line so you can detect trailing spaces. Then you'll
want to learn that caret is used in regular expressions
to stand for start of line so you can detect
indentation.
[vbcol=seagreen]

I'm not convinced that empty lines (see caret and dollar
above) are bad. I am convinced that space-only lines are
bad. Detecting a line with only multiple spaces is
another nice use of regular expressions, brackets and
stars this time.

Done well, the question actually uses some fancy REs to
get it to work. Gotta know which punctuation to escape,
how to handle n-tuple repeats of spaces. Even better is
handling anything but a tab as the start of a paragraph
because that also includes the line before it must be
empty (can't do two lines in a row with only grep in
normal circumstances).

Barry Margolin

2005-10-24, 3:49 pm

In article <1129582435.369845.313400@g49g2000cwa.googlegroups.com>,
WebGod@aol.com wrote:

> Sorry I ended that post early...To continue with my question. What I
> meant by extra spaces is say I am typing a sentence and I put more
> than one space in between like in this sentence within a file. How do
> I find that line in the file using grep? Thanks again!


So you want to find two spaces in a row, or three spaces at the end of a
sentence? Something like this will do it:

egrep '[.!?] |[^.!?] ' filename

This doesn't handle the case where the end of a sentence is inside
quotes, like:

He said, "Damn!" And then he shut up.

It will complain about this because it thinks those spaces are in the
middle of a sentence, not at the end. Correcting it is left as an
exercise for the reader.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
WebGod@aol.com

2005-10-24, 3:49 pm

What I think is needed is simpler than I assumed it would be. If I
have a list of things like this:
Sam and Dave
Jack and Jill
Harold and Maude
I should be able to find the extra spaces between and and Jill above
By using grep '[ ][ ]' filename
This should search for any space, followed by any other space with
nothing in between, correct? Thanks for all your help.

Alan D Johnson

2005-10-24, 3:49 pm

WebGod@aol.com wrote:

> What I think is needed is simpler than I assumed it would be. If I
> have a list of things like this:
> Sam and Dave
> Jack and Jill
> Harold and Maude
> I should be able to find the extra spaces between and and Jill above
> By using grep '[ ][ ]' filename
> This should search for any space, followed by any other space with
> nothing in between, correct? Thanks for all your help.
>

I guess he hasn't gotten to the awk part of grep/sed/awk...
Chris F.A. Johnson

2005-10-24, 3:49 pm

On 2005-10-18, Alan D Johnson wrote:
> WebGod@aol.com wrote:
>
> I guess he hasn't gotten to the awk part of grep/sed/awk...


He hasn't even got to the part of explaining exactly what he
wants.

Once he describes exactly what that is, the solution will be
obvious.

--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========================================
==========================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
Ian Wilson

2005-10-24, 3:49 pm

WebGod@aol.com wrote:
> What I think is needed is simpler than I assumed it would be. If I
> have a list of things like this:
> Sam and Dave
> Jack and Jill
> Harold and Maude
> I should be able to find the extra spaces between and and Jill above
> By using grep '[ ][ ]' filename
> This should search for any space, followed by any other space with
> nothing in between, correct?


grep ' ' filename
will find all lines that contain two consecutive spaces.

You only need the [abc] notation to match *one* of several specific
different characters; 'a' or 'b' or 'c' in this example.

man grep. (and possibly man regexp depending on O/S)
Have you considered buying an introductory book on Unix?


Niklas Norrthon

2005-10-24, 3:49 pm

WebGod@aol.com writes:

> I should be able to find the extra spaces between and and Jill above
> By using grep '[ ][ ]' filename


Off topic perhaps, but someone said:
"The sentence:
'There should be hyphens between Fish and and and and and Chips
in my Fish and Chips sign' would be clearer if there quotes
before Fish and between Fish and and and and and and and and
and and and and and and and and and and and and and Chips and
after Chips."

/Niklas Norrthon
Dave Hinz

2005-10-24, 3:49 pm

On Tue, 18 Oct 2005 01:10:04 GMT, Alan D Johnson <adjtech@hvc.rr.com> wrote:
> WebGod@aol.com wrote:
>
[vbcol=seagreen]
> I guess he hasn't gotten to the awk part of grep/sed/awk...


Well, to be fair, I found it rather awkward when I started as well.

Dave "Get it? I sed 'awk'ward. It's like humor...nevermind..." Hinz

jpd

2005-10-24, 3:49 pm

Begin <3rlm91Fkckq6U4@individual.net>
On 2005-10-19, Dave Hinz <DaveHinz@spamcop.net> wrote:
> On Tue, 18 Oct 2005 01:10:04 GMT, Alan D Johnson <adjtech@hvc.rr.com> wrote:

[snip: aoler on usenet? I thought aol decided to drop it?]
>
> Well, to be fair, I found it rather awkward when I started as well.
>
> Dave "Get it? I sed 'awk'ward. It's like humor...nevermind..." Hinz


It's also sed that if you can grep this you're awkfully awkvanced.


--
j p d (at) d s b (dot) t u d e l f t (dot) n l .
Ok, I'll stop now.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com