search a word and copy the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search a word and copy the file
# 1  
Old 02-01-2012
search a word and copy the file

Hi need help with a script or command

My requirement is

- I need to do a "ls -ltr tcserver*.syslog" files in /tmp, direct the output to a file
HTML Code:
ls -ltr tcserv*.syslog | grep "Jan 31" | awk '{printf "\n" $9}' > jandat.logs
-Then open each file in above list and search for string/word, if the word is there then copy the file name to another out put file
HTML Code:
cat jandat.logs| while read LOG;do
    grep -i "EXCEPTION: O/S ERROR: signal  6" $LOG  && echo ${LOG} >> jansig.log
some how above script is not working, here is the full script
HTML Code:
#!/bin/ksh -x
DATE=$1
ls -ltr tcserv*.syslog | grep "Jan 31" | awk '{printf "\n" $9}' > jandat.logs
cat jandat.logs| while read LOG;do
    grep -i "EXCEPTION: O/S ERROR: signal  6" $LOG  && echo ${LOG} >> jansig.log
done
# 2  
Old 02-01-2012
Please, can we have sample of jandat.logs, jansig.log. Also a script is suppose to work, even if that contains error or gives undesired output. So by not working, I assume, you are not getting desired output Smilie

Also, please try this:
Code:
#!/bin/ksh -x
DATE=$1
ls -ltr tcserv*.syslog | awk '\Jan 31\ {printf "\n" $9}' > jandat.logs
while read LOG;do
    STR=$(grep -i "EXCEPTION: O/S ERROR: signal  6" $LOG)  
    [[ -z ${STR} ]] || echo ${LOG} >> jansig.log
    unset STR
done < jandat.logs

# 3  
Old 02-01-2012
Here is smaple of ls -lrt tcserver*.syslog:

Code:
-rw-r--r--   1 infodba  tcgrp       6935 Feb  1 15:00 tcserver16557.syslog
-rw-r--r--   1 infodba  tcgrp       6935 Feb  1 15:03 tcserver16675.syslog

so my first line of script should copy names of tcserver*.syslog created on Feb 1 to a file1, then i need to read all the files to see if they have "EXCEPTION: O/S ERROR: signal 6" and then copy those name to File2

---------- Post updated at 05:46 AM ---------- Previous update was at 05:25 AM ----------

here is what i get wehn execute the script, (it create the list of file in jandat.logs) but while reading it failes

Code:
+ [[ -z  ]]
+ unset STR
+ read LOG
+ + grep -i EXCEPTION: O/S ERROR: signal  6 -rw-r--r-- 1 infodba tcgrp 263630 Dec 21 17:38 tcserver20953.syslog
grep: can't open -rw-r--r--
grep: can't open 1
grep: can't open tcgrp
grep: can't open 263630
grep: can't open Dec
grep: can't open 21
grep: can't open 17:38
STR=
+ [[ -z  ]]
+ unset STR
+ read LOG
+ + grep -i EXCEPTION: O/S ERROR: signal  6 -rw-r--r-- 1 infodba tcgrp 263630 Dec 21 17:44 tcserver21009.syslog
grep: can't open -rw-r--r--
grep: can't open 1
grep: can't open tcgrp
grep: can't open 263630
grep: can't open Dec
grep: can't open 21
grep: can't open 17:44
STR=
+ [[ -z  ]]
+ unset STR
+ read LOG
+ + grep -i EXCEPTION: O/S ERROR: signal  6 -rw-r--r-- 1 infodba tcgrp 263630 Dec 21 18:02 tcserver21188.syslog
grep: can't open -rw-r--r--
grep: can't open 1
grep: can't open tcgrp
grep: can't open 263630
grep: can't open Dec
grep: can't open 21
grep: can't open 18:02
STR=
+ [[ -z  ]]
+ unset STR
+ read LOG


Moderator's Comments:
Mod Comment Please use code tags!

Last edited by radoulov; 02-01-2012 at 07:44 AM..
# 4  
Old 02-01-2012
Quote:
Originally Posted by karghum
Here is smaple of ls -lrt tcserver*.syslog
-rw-r--r-- 1 infodba tcgrp 6935 Feb 1 15:00 tcserver16557.syslog
-rw-r--r-- 1 infodba tcgrp 6935 Feb 1 15:03 tcserver16675.syslog

so my first line of script should copy names of tcserver*.syslog created on Feb 1 to a file1, then i need to read all the files to see if they have "EXCEPTION: O/S ERROR: signal 6" and then copy those name to File2

---------- Post updated at 05:46 AM ---------- Previous update was at 05:25 AM ----------

here is what i get wehn execute the script, (it create the list of file in jandat.logs) but while reading it failes

+ [[ -z ]]
+ unset STR
+ read LOG
+ + grep -i EXCEPTION: O/S ERROR: signal 6 -rw-r--r-- 1 infodba tcgrp 263630 Dec 21 17:38 tcserver20953.syslog
grep: can't open -rw-r--r--
grep: can't open 1
grep: can't open tcgrp
grep: can't open 263630
grep: can't open Dec
grep: can't open 21
grep: can't open 17:38
STR=
STR=
+ [[ -z ]]
+ unset STR
+ read LOG

Yiikkkees... Smilie My Bad...

Here you go:

Code:
 
#!/bin/ksh -x
DATE=$1
ls -ltr tcserv*.syslog | awk '/Jan 31/ {print $9}' > jandat.logs
while read LOG;do
    STR=$(grep -i "EXCEPTION: O/S ERROR: signal  6" $LOG 2>/dev/null)
    [[ -z ${STR} ]] || echo ${LOG} >> jansig.log
    unset STR
done < jandat.logs


Last edited by knight_eon; 02-01-2012 at 06:54 AM.. Reason: Lil more editing required :P
# 5  
Old 02-01-2012
Code:
 
ls -lrt tcserver*.syslog | awk '/Feb 1/ {print $NF}' | while read file; do grep -l "EXCEPTION: O\/S ERROR: signal 6" $file && echo "$file has the pattern" >>output.txt ; done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

2. Shell Programming and Scripting

How to search a word in a file?

How to search a particular word in a file. in my file contains lacks of rows. (2 Replies)
Discussion started by: pmreddy
2 Replies

3. UNIX for Dummies Questions & Answers

How to copy a file opened in UNIX to word or txt?

Hi I would like to know how to copy a file opened in unix editor like Nedit or vi editor to a word document Since I am newbie I want to make sure I am not really copying onto real subdirectories I tried to right click and copy the entire .sql file or any other file opened.. Nedit doesn't... (3 Replies)
Discussion started by: swathi123
3 Replies

4. Shell Programming and Scripting

sed help - search/copy from one file and search/paste to another

I am a newbie and would like some help with the following - Trying to search fileA for a string similar to - AS11000022010 30.4 31.7 43.7 53.8 60.5 71.1 75.2 74.7 66.9 56.6 42.7 32.5 53.3 I then want to replace that string with a string from fileB - ... (5 Replies)
Discussion started by: ncwxpanther
5 Replies

5. Shell Programming and Scripting

copy contents of unix file to Word document

Hello, I have a unix file about 3000lines which i want to copy from and paste it into a Word document. If i cat the file and try to scroll through it then not everything is captured so i am getting and incomplete paste. Any help is really appreciated. jak (2 Replies)
Discussion started by: jakSun8
2 Replies

6. Shell Programming and Scripting

copy line to new file if word matches

I'm drawing blank on this. The log file I have is filled with garbage, but the important lines are ##/##/### Installation xxxxxxx So, I want to sed the line to a new file IF the word installation is in it. I tried removing none matching lines sed 's/Installation/,//!d' infile > outfile but that... (3 Replies)
Discussion started by: dba_frog
3 Replies

7. Shell Programming and Scripting

How to search for a word in a particular column of a file

How to search for a word like "computer" in a column (eg: 4th field) of a '***' delimited file and add a column at the end of the record which denotes 'Y' if present and 'N' if not. After this, we need to again check for words like 'Dell' but not 'DellXPS' in 5th field and again add another column... (5 Replies)
Discussion started by: Jassz
5 Replies

8. Shell Programming and Scripting

Search for last instance of word in a file

Hi I'm trying to search for the last instance of the word 'cache' in a HTML file that I have downloaded from YouTube. I'm using the following syntax, but an error is thrown when I try it. grep -f "cache" Also I wish to append the above grep command to the below so that the search for cache... (3 Replies)
Discussion started by: colmbell
3 Replies

9. Solaris

Copy and paste text from a word document into a txt file in vi

Hello, Can anybody please tell me how we can copy and paste text from a word document into a text file that we are editing in vi? Is it possible to do that while we are editing the text file in vi in insert mode? Thanks, (3 Replies)
Discussion started by: Pouchie1
3 Replies

10. Shell Programming and Scripting

search a word from file

Hello, I have a file which contains some SQL statements. I need to take out the table name from the file. Table name will always end with "_t". can anyone help me in getting that? for e.g. --- SQL_STMT do_sql_insert: cmd="insert into account_t ( poid_DB, poid_ID0, poid_TYPE, poid_REV,... (9 Replies)
Discussion started by: vishy
9 Replies
Login or Register to Ask a Question