The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep high bit char ayyo1234 UNIX for Advanced & Expert Users 7 02-11-2008 02:01 PM
Top and Prstat display different results for memory zen03 SUN Solaris 4 12-29-2006 12:28 AM
List grep results slire UNIX for Dummies Questions & Answers 14 10-31-2006 08:42 AM
How to refine results of grep -p priceb Shell Programming and Scripting 2 06-28-2006 05:40 AM
Multiple Grep Results - Formatting sysera Shell Programming and Scripting 7 03-25-2004 03:04 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 03-30-2007
Registered User
 

Join Date: Mar 2007
Posts: 21
How to display first 7 char of grep results?

My file contains the following:

uat2000.aspclient.active=true
uat2001.aspclient.active=true
uat2002.aspclient.active=true
uat2003.aspclient.active=true
uat2004.aspclient.active=false
uat2005.aspclient.active=false
uat2006.aspclient.active=false
uat2007.aspclient.active=false
uat2008.aspclient.active=false
uat2009.aspclient.active=false

I am greping which ones = true with
# cat filename | grep active=true

Results:

uat2000.aspclient.active=true
uat2001.aspclient.active=true
uat2002.aspclient.active=true
uat2003.aspclient.active=true

QUESTION: I want the results to display only the first 7 characters, ie uat2000, uat2001, etc - HOW DO I DO THAT?

Thanks in advance!
Reply With Quote
Forum Sponsor
  #2  
Old 03-30-2007
reborg's Avatar
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 3,590
grep is not the correct tool to do this, here are some ways you could do it.
Also in you file you can use the . as a dilimiter. It is probably better to do so
then to depend on having 'n' characters.

Code:
awk -F\. '/active=true/{print $1}'
Code:
sed -n '/active=true/s/^\([^.][^.]*\)[.].*/\1/p'
Code:
IFS=.
while read one two three; do
    [[ "$3" == "active=true" ]] && echo $one
done < inputfile
Reply With Quote
  #3  
Old 03-30-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,580
Code:
grep  "active=true"  file | cut -d"." -f1
Reply With Quote
  #4  
Old 04-03-2007
Registered User
 

Join Date: Mar 2007
Posts: 21
How to search files and dispaly specific text within the file?

Thank you for your responses. This forum is great! I am very new to the unix environment and just trying to figure things out..

So now that you have helped me with this problem, I realize that what I am really looking for is the solution to this - with the same result as above:

1. From the root directory (or within a script), search all directories below to find any file called Application.properties

2. When an Application.properties file is found, search it for "active=true"

result:
uat2000.aspclient.active=true
uat2001.aspclient.active=true
uat2002.aspclient.active=true
uat2003.aspclient.active=true

3. Count the lines and display only first 7 characters, ie uat2000, uat2001, etc :

uat2000
uat2001
uat2002
uat2003

Thanks A Million!

Last edited by kthatch; 04-03-2007 at 03:29 PM. Reason: Modify Title
Reply With Quote
  #5  
Old 04-03-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,003
a variation on the 'awk' theme:
Code:
awk -F'[.=]' '$(NF-1)=="active" && $NF=="true" {print $1}' myFile.txt
Reply With Quote
  #6  
Old 04-03-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,003
reborg,
a slight modification to the shell alternative:
Code:
while IFS='.' read one two three; do
    [[ "$3" == "active=true" ]] && echo $one
done < inputfile
that way you don't have to save the IFS and reset it after the loop.
Reply With Quote
  #7  
Old 04-03-2007
reborg's Avatar
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 3,590
True, good point Vlad, it prevents fogetting to reset it, like I did.

..and you forgot your usual "On Solaris use nawk" for the awk alternative.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:19 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0