The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 05:01 PM
Top and Prstat display different results for memory zen03 SUN Solaris 4 12-29-2006 03:28 AM
List grep results slire UNIX for Dummies Questions & Answers 14 10-31-2006 11:42 AM
How to refine results of grep -p priceb Shell Programming and Scripting 2 06-28-2006 08:40 AM
Multiple Grep Results - Formatting sysera Shell Programming and Scripting 7 03-25-2004 06:04 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-30-2007
kthatch kthatch is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 28
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!
  #2 (permalink)  
Old 03-30-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
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
  #3 (permalink)  
Old 03-30-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,512
Code:
grep  "active=true"  file | cut -d"." -f1
  #4 (permalink)  
Old 04-03-2007
kthatch kthatch is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 28
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 06:29 PM.. Reason: Modify Title
  #5 (permalink)  
Old 04-03-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
a variation on the 'awk' theme:
Code:
awk -F'[.=]' '$(NF-1)=="active" && $NF=="true" {print $1}' myFile.txt
  #6 (permalink)  
Old 04-03-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
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.
  #7 (permalink)  
Old 04-05-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by reborg
grep is not the correct tool to do this, here are some ways you could do it.

Code:
IFS=.
while read one two three; do
    [[ "$3" == "active=true" ]] && echo $one
done < inputfile

More portably (i.e., any Bourne-type shell; the example above does not work in all POSIX shells) and more securely:
Code:
while IFS=. read one two three; do
    [ "$3" = "active=true" ] && printf "%s\n" "$one"
done < inputfile
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0