Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grep for lines with 1 / but not 2 / 3

Status
Not open for further replies.

Einstein47

Programmer
Nov 29, 2001
737
US
Hey all,

I am wanting to grep the output of the du command to display the values just under the current directory. I can identify a pattern with the output (all the direct children have only 1 forward slash '/child_dir', and all the sub children (grandchildren?) have two '/child_dir/grandkid'.

I am looking for the RE to use in this command:
du -k | grep -v &quot;/<RE GOES HERE>/&quot;

It should be obvious, but I can't get it. Any suggestions?!? Einstein47
(Love is like PI - natural, irrational, endless, and very important.)
 
#!/usr/bin/ksh

WIDTH=10

ls -al|while read LINE
do
echo &quot;$LINE&quot;|awk -v WIDTH=${WIDTH} '{printf(&quot;%-&quot;WIDTH&quot;s&quot;,$5)}' #print size
echo &quot;$LINE&quot;|awk -v WIDTH=${WIDTH} '{printf(&quot;%-&quot;WIDTH&quot;s&quot;,$NF)}' #print file name
echo
done

I believe there is a way to reduce this down to one line
in the loop and get rid of the extra echo statement
but I can't remember off hand.

Robert Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
how about:

du -k | egrep -v '/.*/' Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Thanks MikeLacey. That was exactly what I needed.

What is the main difference between grep and egrep? The man says that the error messages are different, otherwise egrep is the same as grep -E. That still doesn't tell me why i would want to use one over the other.

Anyhow - thanks again. Einstein47
(Love is like PI - natural, irrational, endless, and very important.)
 
The only difference is that egrep lets you
grep for mutltiple things at once.

For example
egrep &quot;a|b|c&quot;
would return anything with a or b or c
in the line.

Robert Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
Rob,

errmmm no, I can't actually - irritating.... :-(

I could do it in Perl (I think), if that's any help. Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
I think the problem is that egrep regexes are greedy - they will match on the largest portion of the string they can.

So:

'/*/' will match '/usr/local/bin/thingy.pl'

which is ok if you want to reject lines with more than one / It's actually matching the two red /'s.

but looking for a specific number of /'s is fiddly.

Perl has an additional operator which makes a regex non-greedy. You could match the first two /'s with this pattern.

'/.*/?'
Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
just a question ...without grep did you try :
du -sk ./*
Andy
 
That is a much faster solution Bandybe. Thanks - this thread is just full of stars.

What would I do without Tek-Tips? Einstein47
(Love is like PI - natural, irrational, endless, and very important.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top