![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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! |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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
|
|||
|
|||
|
Code:
grep "active=true" file | cut -d"." -f1 |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
||||
|
||||
|
a variation on the 'awk' theme:
Code:
awk -F'[.=]' '$(NF-1)=="active" && $NF=="true" {print $1}' myFile.txt
|
|
#6
|
||||
|
||||
|
reborg,
a slight modification to the shell alternative: Code:
while IFS='.' read one two three; do
[[ "$3" == "active=true" ]] && echo $one
done < inputfile
|
|
#7
|
||||
|
||||
|
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. |
||||
| Google The UNIX and Linux Forums |