How to display first 7 char of grep results?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to display first 7 char of grep results?
# 1  
Old 03-30-2007
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  
Old 03-30-2007
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  
Old 03-30-2007
Code:
grep  "active=true"  file | cut -d"." -f1

This User Gave Thanks to ghostdog74 For This Post:
# 4  
Old 04-03-2007
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 07:29 PM.. Reason: Modify Title
# 5  
Old 04-03-2007
a variation on the 'awk' theme:
Code:
awk -F'[.=]' '$(NF-1)=="active" && $NF=="true" {print $1}' myFile.txt

# 6  
Old 04-03-2007
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  
Old 04-03-2007
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. Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to display some special results with AWK

I have a file like this: AAEQGAGNQPQH,,,,160,32,,,, AAGQDYNSPLH,,712,39,,,,,, AAGREGGNTEAF,26,,,,,,,,4 AAGSPQH,,,,8,5,,,, AAKKLGQFYNEQF,4,,6,,,7,,,2 AANSGGRYNEQF,,2747,3120,,,,,, AAQGGVGGELF,,,,5,,12,36,, AAQGLAGYYEQY,,25,13,,,,,, AAQRGNEQF,510,2,,,6,,6,,76 AAQTGENSPLH,,16,16,,,,,, The... (9 Replies)
Discussion started by: xshang
9 Replies

2. UNIX and Linux Applications

Display mysql results nicely

Hi, perhaps this is a dumb question. I'm running queries on mysql and I'm getting tabbed results like these: mysql> SELECT * from metrics_status WHERE date = '2012-03-30'; <TABLE... (1 Reply)
Discussion started by: erick_tuk
1 Replies

3. Shell Programming and Scripting

Grep no results

Hello guys, I have been looking around but can't find the answer to my problem: If the grep command displays no results, print "no results have been found" and increment x. But if the grep command find something, do nothing. if echo "no results have been found $x" x=`expr $x + 1 `... (3 Replies)
Discussion started by: Benou
3 Replies

4. Shell Programming and Scripting

Display find results, and pipe to xargs

I have an overnight script which runs across a large directory to repair permissions and ownership. I also have this command output the list of files affected so that cron can email these as a log file. Previously I had the command in the form: find /path/to/files -not -user myname -print -exec... (4 Replies)
Discussion started by: mij
4 Replies

5. Shell Programming and Scripting

Shell script compare all parameters in two files and display results

Hi , I am not familiar with shell programming. I have a requirement like i have two files .I need to compare the two files by comparing each parameter and i should produce 2 outputs. 1)i have around 35 parameters say i have one parameter name called db_name=dcap in one file and... (7 Replies)
Discussion started by: muraliinfy04
7 Replies

6. Shell Programming and Scripting

Display echo results in three column

Dear Friends, I have my command output which displays on one row and values are now scrollable (vertical) 3 pages. How do i display those output in three column so that i no need to scroll? Example: dcadd$cat components 1.Caluculator 2.Diary ... ... 50.Mobile 51.Battery .. ...... (12 Replies)
Discussion started by: baluchen
12 Replies

7. Shell Programming and Scripting

grep : search a long char contain space

Hi, i have to search for a char like that : export var1="i am not happy /not happy" with a command like : grep $var1 file but this not working with me !!! thank you in advance. (2 Replies)
Discussion started by: tizilfin
2 Replies

8. UNIX for Advanced & Expert Users

grep high bit char

Hi - I have file which contains high bit unicode chars like © etc.. How can I do grep to find out lines which contain copyright symbol © I tried using grep \x{00A9} grep \x\{00A9\} Thanks- (7 Replies)
Discussion started by: ayyo1234
7 Replies

9. Solaris

Top and Prstat display different results for memory

I have a question about the accuracy of prstat. I did a 'prstat -t' and it shows 99% of my memory is occupied by oracle. NPROC USERNAME SIZE RSS MEMORY TIME CPU 194 oracle 343G 340G 99% 86:17.24 56% However, 'top' shows I still have 7762meg of memory free. Memory: 16G real, 7762M... (4 Replies)
Discussion started by: zen03
4 Replies

10. UNIX for Dummies Questions & Answers

List grep results

Hi I need to search for matching strings in a database and I want to print out all files that matches in "detail", which means that I want the output to contain datum of last saving. I only get the grep function tp print the actual file names which is not enough since the database is to large... (14 Replies)
Discussion started by: slire
14 Replies
Login or Register to Ask a Question