Redirect grep output to dynamique fileName and subdirectory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Redirect grep output to dynamique fileName and subdirectory
# 1  
Old 10-24-2008
Redirect grep output to dynamique fileName and subdirectory

Hi all
i'm new in KSH,
i want to write a script to grep a logs files and redirecting the result into a relative subdirectory/file.txt that must be created near to each log file

my begin script is :

find ./logs -type f -name "*.log" -exec grep error {} \;

how i can perform that modest script
thanks in advance

Rambler
# 2  
Old 10-24-2008
Are all of the log files in the logs directory?? The reason I ask is if they are all in one directory you could do this... if this is not what you want please give more detail.

Code:
$ grep error ./logs/*.log > /tmp/error.out

# 3  
Old 10-24-2008
I guess I should have read the title better.... what about something like this?

Code:
for all in `find ./ -type f -name "*.log"`
do 
grep error $all > ./tmp/$all.error
done

# 4  
Old 10-24-2008
MySQL

Thank you sethcoop, for your response

that not work like a want,
the $all variable contant the full path of log file

the error geted is similar to :

Code:
test.ksh[3]: ./tmp/./claf/weblogic/APP/CONSOLE-APP_20081014.log.error: cannot create

when i execute :
Code:
for all in `find ./ -type f -name "*.log"`
do 
grep error $all > ./tmp/$all.error
done

that work

but my need still is to creat a sub directory in each iteration with a relative name and put into it the grep result

thank you
# 5  
Old 10-24-2008
Do you want the error files all in the same directory or should they be somewhere else releative to where the file is located?
# 6  
Old 10-24-2008
if i get the two way that will be fine

may i need some thing like :

Code:
for all in `find ./ -type f -name "*.log"`
do 
dir = `dirname $all`
file = `filename $all`
grep error $all > $dir\tmp\$file.error
done

that just an idee not a correct script

thank you sethcoop
# 7  
Old 10-24-2008
I believe it is "basename" not "filename"...

Code:
file = `basename $all`

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirect script output to file after grep

i have simple program that generate log file 1 line every sec, i need to do grep for specific record then redirect to another file. #!/bin/bash for i in `seq 1 20`; do echo $i sleep 1 done ./test.sh |egrep "5|10|15" 5 10 15 r ./test.sh... (2 Replies)
Discussion started by: before4
2 Replies

2. Shell Programming and Scripting

Find: filename in every subdirectory matching a pattern

Hi, I have multiple directories built in following manner /app/red/tmp /app/blue/upd /app/blue/tmp /app/green/tmp /app/red/upd /app/green/upd I have filenames having pattern ONE.XXX.dat TWO.ZZZ.dat and so on across the folders listed above My objective is to list all filenames of a... (4 Replies)
Discussion started by: wahi80
4 Replies

3. UNIX and Linux Applications

How to redirect grep command output to same file

Hi Everyone, Can anyone please tell me, how can I redirect the grep command output to same file. I am trying with below command but my original file contains no data after executing the command. $grep pattern file1 > file1 Kind Regards, Eswar (5 Replies)
Discussion started by: picheswa
5 Replies

4. Shell Programming and Scripting

find and replace in subdirectory (filename & content - both)

Hi, I have to rename all occurance of CUST_MST to RESELLER_MST both in filename and file content under a directory (say D0) which contains multiple (2-3 levels) sub directory. Example: D0 -> D1 -> D2 has a file CUST_MST_TEMP.txt this contains : > cat /D0/D1/D2/CUST_MST_TEMP.txt... (3 Replies)
Discussion started by: sabyasm
3 Replies

5. Shell Programming and Scripting

how to redirect the output of a grep command to a file inside a shell script

hi, i wat to get the output of a grep command in a file. but when i am trying out the same grep command in the unix prompt its working fine.. i am getting the output properly.. but when i am writing the same command inside my shell script , its just creating a new output file with no contents... (11 Replies)
Discussion started by: kripssmart
11 Replies

6. Shell Programming and Scripting

Redirect grep output into file !!!!!

Hi, I am writing the following code in command prompt it is working fine. grep ',222,' SAPPCO_20080306.CSV_old > SAPPCO_20080306.CSV_new But the command is not working in the Shell Script... ########################################## #!/bin/sh #... (2 Replies)
Discussion started by: hanu_oracle
2 Replies

7. Shell Programming and Scripting

redirect the grep output into the variable

i want to redirect the grep output into the variable but i am not able to get it i tried veri=`grep -i $1 "${logdir}"* | grep -i adding | grep -iv equation | tail -1 | cut -d ':' -f 1` vari=$(grep -i $1 "${logdir}"* | grep -i adding | grep -iv equation | tail -1 | cut -d ':' -f 1) BUT NOT... (1 Reply)
Discussion started by: mail2sant
1 Replies

8. UNIX for Dummies Questions & Answers

How to grep / zgrep to output ONLY the matching filename and line number?

Hi all, I am trying to zgrep / grep list of files so that it displays only the matching filename:line number and does not display the whole line, like: (echo "1.txt";echo "2.txt") | xargs zgrep -no STRING If I use -o option, it displays the matching STRING and if not used, displays the... (3 Replies)
Discussion started by: vvaidyan
3 Replies

9. Shell Programming and Scripting

grep string and output filename

Hello, I have over 200 files and some of them have the string like "John price $200". I would like to grep the string. Then output the filename which found the string. I have the following script, but it ONLY output the string echo Please input list file name: read listn for file in `cat... (3 Replies)
Discussion started by: happyv
3 Replies

10. Shell Programming and Scripting

Losing filename in grep output

I have the following line in a script that searches files in several directories and shows the search results on the screen. ls "$path" | xargs cat | tr -s " " | fgrep -i "$search_arg" But, because I'm also using CAT and TR, the output from the search does not display the name of the file... (6 Replies)
Discussion started by: netguy
6 Replies
Login or Register to Ask a Question