- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- grep first and result to ls command
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-11-2009 02:04 PM
тАО03-11-2009 02:04 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-11-2009 02:28 PM
тАО03-11-2009 02:28 PM
Re: grep first and result to ls command
ls -lrt $(grep -il "Error" frm*)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-11-2009 07:08 PM
тАО03-11-2009 07:08 PM
Solution> [...]
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.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-16-2009 10:26 AM
тАО03-16-2009 10:26 AM
Re: grep first and result to ls command
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-16-2009 12:54 PM
тАО03-16-2009 12:54 PM
Re: grep first and result to ls command
Why do you care? What's the real problem?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-19-2009 12:04 AM
тАО03-19-2009 12:04 AM
Re: grep first and result to ls command
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-19-2009 04:42 AM
тАО03-19-2009 04:42 AM
Re: grep first and result to ls command
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-19-2009 04:59 AM
тАО03-19-2009 04:59 AM
Re: grep first and result to ls command
> 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 "(".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-20-2009 09:58 AM
тАО03-20-2009 09:58 AM
Re: grep first and result to ls command
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-20-2009 10:59 AM
тАО03-20-2009 10:59 AM
Re: grep first and result to ls command
> 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.