A FIND and GREP Combination

Using find and grep together:

To find all files with target pattern in any given directory use the command:

% find [target_dir] -exec grep -l 'target' {} \;

This uses the find fuction with the grep function to specify absolute path to all files with the target pattern in them.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

I want to searh a record in the table. But I don't want to display out the entire row or column record. What I want to display is only a single row or column record.

I have try grep ann file-name what should I add so I can display the row 4 record that I have.

The following command display the column 4 of the forst line of the input file containing 'ann' :

awk '/ann/ {print $4;exit}' input_file

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Is there any method that u know using grep instead of using awk to the example above.

I'm using "grep ann inputfile" and it show all the entire row and not the record on column 4.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

The short answer is - no. The grep utility is a line orientated utility. You can use grep to find/print the line but you then
need to use some other utility (cut/awk, etc.) to process the line and only output the part of the line that you want.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

There is a way of doing it.
Why don't you post your homework assignment so I can verify my procedure is in conformance with your assignment.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

This is my example:

name age number (not list in the file)
acb 51 1234
hela 42 4567

My question is that now I know how to cut the column already but when I cut it I want to use grep to display out the age that less than 50, how should use grep to search age that less than 50.

I have try using grep [:digit:] < 50 file but I fail to do this.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

cut -d\ -f2 example |grep -E "^[0-9]$|^[0-4][0-9]$"

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Of course, that takes advantage of 50 being a multiple of 10. <54 would be a bit hairy'er.
This will handle any number:
NUM=50 # for example
cat example| while read a b c;do [ $b -lt $NUM ] && echo $b;done

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Sorry for posting againt but still not very understand, how about I using grep to compare column 3 record (exp: < 25.50) and display record at column 1 and 3 that match the requirement using grep.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

"compare column 3 record" or column 2 ??
" using grep" why grep ??

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

To display all rows in test.dat where the value in the second column is less than 50:
awk '{ if ($2 < 50) {print} }' test.dat

As others have pointed out, grep is a tool for pattern filtering, not output manipulation.

Related:

Have a Unix Problem
Unix Forum - Do you have a UNIX Question?

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.