How to read from middle of a file for a particular text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read from middle of a file for a particular text
# 1  
Old 11-12-2009
How to read from middle of a file for a particular text

Hi,

Below is my issue which I desperately need and I want a shell script which can do this job.

1. There are 10 log files in a particular location.

2. open each log file. Goto to the end of the file. From the end go up to find a particular text. From this particular text till the end of the log search for another particular text.

3. if the particular text exists in the file return the file name(s) as the output.

Please help me with the script. Thanks a lot to all.

Thanks and Regards
Shriram
# 2  
Old 11-12-2009
If it is not homework, please tell us why you need this type of code.
# 3  
Old 11-13-2009
I m trying to prepare a system's health check during which certain text will be searched in the many logs under one location. Can you please let me know how to work it out.
# 4  
Old 11-13-2009
I will refer to item 2 'particular text' as first keyword, and the ' another particular text' as second keyword.
Here is my script, works in ksh, should be good for bash or any other plain POSIX shell

Code:
 
KEYWRD1='your_first_keyword';
KEYWRD2='your_second_keyword'
 
# go through all log files in a particular directory: 
LOGDIR='your_log_directory_path'
# I don't know naming pattern of your logs, 
# for example they are ending with .log
 
for fn in $LOGDIR/*.log; do
#
# determine line number of last occurence of key word 1
#
  lineno=$( grep -n $KEYWRD1 $fn | tail -1 | cut -d':' -f1 )
#
# build ed command to print from that line to the end
#
  cmnd=$( printf "%d,$p" $lineno )
#
# run ed and grep throu the last n lines searching for KEYWRD2
#
  echo $cmnd | ed - $fn | grep -q $KEYWRD2
#
# if found (result 0) then spit out the file name
#
  if(( $? == 0 )); then
    echo $fn
  fi
done

I would think this could be reduced to a one-liner awk, but this is pure shell
I typed it into the browser window, hopefully no typos.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

2. Shell Programming and Scripting

Read n lines from a text files getting n from within the text file

I dont even have a sample script cause I dont know where to start from. My data lookes like this > sat#16 #data: 15 site:UNZA baseline: 205.9151 0.008 -165.2465 35.8109 40.6685 21.9148 121.1446 26.4629 -18.4976 33.8722 0.017 -165.2243 48.2201 40.6908 ... (8 Replies)
Discussion started by: malandisa
8 Replies

3. Shell Programming and Scripting

How to insert text in the middle of a file?

Hi, So far i've made a script that takes two argument, 1st is the contents and the 2nd is the named file. At the moment i've managed to insert new contents as a new line at the top, but i want to ask how can you insert contents in the middle of the file? Source Code #!/bin/bash #Write... (3 Replies)
Discussion started by: zen10
3 Replies

4. Shell Programming and Scripting

Adding Text To Middle Of File

Hi I am trying to write a bash script to add a line of text to the middle of a file. The way I worked it out was to calculate the number of lines and divide by 2. The file I am using is called newfile and it just contains lines of data. The new file I have created I have called midfile and... (7 Replies)
Discussion started by: BundBash
7 Replies

5. Shell Programming and Scripting

Script to add a single line to middle of text file.

I've got a configuration file that is filled with xml text statements for example: <...../> <...../> <...../> <data id="java-options" value="-server -Djava.security.policy..../> <...../> <...../> <...../> I want to write a korn shell script that will go to this specific line and add a... (2 Replies)
Discussion started by: progkcp
2 Replies

6. UNIX for Dummies Questions & Answers

How to insert text in the middle of a file

Hey guys, how do we take a line of text as an argument from a user and then insert it in the middle of a file irrespective of the number of lines in the file. I am trying to do this without SED or AWK. Inserting it in the beginning and at the end is easy, but i am trying to accomplish inserting... (6 Replies)
Discussion started by: kartikkumar84@g
6 Replies

7. Shell Programming and Scripting

add text in the middle of file

Can anyone help me pls? I want to add a text into the middle of file. I've writtenthe following script text to add="$1" file="$2" lines=$(wc -l $2) half_lines=$(expr $lines / 2) head -$half_lines $2 > temp echo "text to add" >> temp ((half_lines=$half_lines + 1)) tail -$half_lines $2... (6 Replies)
Discussion started by: relle
6 Replies

8. Shell Programming and Scripting

insert text in the middle of a file

I want to insert a text into the middle of a file (3 Replies)
Discussion started by: relle
3 Replies

9. Shell Programming and Scripting

How to insert text into first line of the file and middle of the file?

Script 1 Pre-requisites Create a file with x amount of lines in it, the content of your choice. Write a script that takes two arguments. The first being a line of text, the second being your newly created file. The script should take the first argument and insert it into the very top (the... (3 Replies)
Discussion started by: ali hussain
3 Replies

10. Shell Programming and Scripting

insert text into the middle of a original file

how do u insert text into a specific place in a file, say the middle for example, without changing the name for that file (1 Reply)
Discussion started by: mopimp
1 Replies
Login or Register to Ask a Question