Operating System - Linux
1753347 Members
5158 Online
108792 Solutions
New Discussion юеВ

Re: grep first and result to ls command

 
SOLVED
Go to solution
Leo The Cat
Regular Advisor

grep first and result to ls command

Hi

I'd like to combine grep and after that ls

something like that

grep -i "Error" frm*.txt | ls -lrt .......

My result must be all grep files with Error but I want the same display provided by ls -lrt command to have by example date and time of these files !

How to do this small trick !?


Any good idea ?
Bests Regards
Den
9 REPLIES 9
Leo The Cat
Regular Advisor

Re: grep first and result to ls command

Sorry of course the solution is:

ls -lrt $(grep -il "Error" frm*)

Steven Schweda
Honored Contributor
Solution

Re: grep first and result to ls command

> Sorry of course the solution is:
> [...]

Except that "the solution" is, of course,
almost never more than "_a_ solution", and
this proposed solution may fail for very long
file lists. Something like the following
might avoid some problems:

find . -name 'frm*.txt' \
-exec grep -iq 'error' {} \; \
-exec ls -l {} \;


(If "ls -l" output were not so lame, you
might even be able to sort the stuff by
date-time in a reasonable way.)

Some "find" programs have a "-ls" operator,
which would simplify things a bit, too. (The
one supplied with HP-UX seems not to be among
these.)
Leo The Cat
Regular Advisor

Re: grep first and result to ls command

Hi Guys

ok. I'm in business now with the command below

1. cd /logs/forms/debug/

2. find . -name 'frm*' -exec grep -iq 'Error' {} \; -exec ls -lrt {} \; >> $OUTPUT_FILE


BUT !!

I'd like to do this without the cd command. The problem is now

find . -name '/logs/forms/debug/frm*' -exec grep -iq 'Error' {} \; -exec ls -lrt {} \; >> $OUTPUT_FILE

doesn't work if I'm not running this from /logs/forms/debug/


The situation is better !!! ....

Bests Regards
Den
Steven Schweda
Honored Contributor

Re: grep first and result to ls command

> I'd like to do this without the cd command.

Why do you care? What's the real problem?
Fredrik.eriksson
Valued Contributor

Re: grep first and result to ls command

You're going about your find line the wrong way
the "." after find is path to search. -name just tells find what it's looking for.

find /logs/forms/debug/ -name frm* -exec grep -iq 'Error' {} \; -exec ls -lrt {} \; >> $OUTPUT_FILE

This is alot more likely to produce the result you're looking for. But I'm not entirely sure that find will return the full path (eg. /logs/forms/debug/frm1.file). Without the full path this will not work since neither of grep or ls will find the target.

You can fix that by adding the path /logs/forms/debug/ before {}. Only do this is find doesn't give you a full path.

Best regards
Fredrik Eriksson
Leo The Cat
Regular Advisor

Re: grep first and result to ls command

Why I don,t want use the cd commad ? I'd prefer to have no modification on the current active path.

About the idea: use /logs/forms/debug/before {}

Example
find /logs/forms/debug/ -name "frm*" -exec grep -iq 'Error' {} \; -exec ls -lrt /logs/forms/debug/{} \;

doesn't work of course. Probably I don't see want do want do.

Bests Regards
Den
Steven Schweda
Honored Contributor

Re: grep first and result to ls command

> Why I don,t want use the cd commad ? I'd
> prefer to have no modification on the
> current active path.

As the old saying goes, there's more than one
way to skin a cat.

bash$ pwd
/ALP$DKA0/sms
bash$ ( cd /tmp ; pwd )
/tmp
bash$ pwd
/ALP$DKA0/sms

"man your_shell", look for "(".
Leo The Cat
Regular Advisor

Re: grep first and result to ls command

ok break !

Why before I've used loop. It's because I need to add ### before each output line !

like this:
cd /logs/forms/debug/
ls /logs/forms/debug/frmweb*
if [[ $? -eq 0 ]]; then
for file in $(ls $(grep -il "Error" /logs/forms/debug/frmweb*))
do
echo "### " $(ls -l $file) >> $OUTPUT_FILE
done
fi

And Unfortunately I didn't find the solution to do this with find and -exec (something like this)

cd /logs/forms/debug/
find . -name 'frm*' -exec grep -iq 'Error' {} \; -exec ls -lrt {} \; >> $OUTPUT_FILE

Where to manage the ### special string ?

This is my problem, forget the "cd" command problem... it's not a problem here.

Thank you very much
Den
Steven Schweda
Honored Contributor

Re: grep first and result to ls command

> It's because I need to add ### before each
> output line !

It certainly was foolish of me not to have
known that.

> Where to manage the ### special string ?

You could write a shell script, and have a
"-exec your_script" clause in your "find"
command.

You could pipe the "find" output into a
sub-shell:

bash$ ls -l
total 2
-rwxr-x--- 1 SMS 40 5 Mar 20 13:48 a.b
-rw-r----- 1 SMS 40 5 Mar 20 13:49 a.c
-rw-r----- 1 SMS 40 5 Mar 20 13:49 a.d
-rwxr-x--- 1 SMS 40 5 Mar 20 2009 b.c
bash$ cat ../fs.sh
#!/bin/sh

find . -name 'a.*' -type f | \
(
while read file ; do
lsl=$( ls -l "$file" )
echo "### ${lsl}"
done
)

bash$ ../fs.sh
### -rwxr-x--- 1 SMS 40 5 Mar 20 13:48 ./a.b
### -rw-r----- 1 SMS 40 5 Mar 20 13:49 ./a.c
### -rw-r----- 1 SMS 40 5 Mar 20 13:49 ./a.d

Many things are possible.