Sponsored Content
Top Forums Shell Programming and Scripting How to read from middle of a file for a particular text Post 302371180 by migurus on Friday 13th of November 2009 02:23:05 PM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
rcsfreeze(1)															      rcsfreeze(1)

NAME
rcsfreeze - freeze a configuration of sources checked in under RCS SYNOPSIS
rcsfreeze [name] DESCRIPTION
rcsfreeze assigns a symbolic revision number to a set of RCS files that form a valid configuration. The idea is to run rcsfreeze each time a new version is checked in. A unique symbolic name (C_number, where number is increased each time rcsfreeze is run) is then assigned to the most recent revision of each RCS file of the main trunk. An optional name argument to rcsfreeze gives a symbolic name to the configuration. The unique identifier is still generated and is listed in the log file but it will not appear as part of the symbolic revision name in the actual RCS files. A log message is requested from the user for future reference. The shell script works only on all RCS files at one time. All changed files must be checked in already. Run rcsclean(1) first and see whether any sources remain in the current directory. RESTRICTIONS
rcsfreeze does not check whether any sources are checked out and modified. Although both source file names and RCS file names are accepted, they are not paired as usual with RCS commands. Error checking is rudimentary. rcsfreeze is just an optional example shell script, and should not be taken too seriously. See CVS for a more complete solution. FILES
version number log messages, most recent first IDENTIFICATION
Author: Stephan v. Bechtolsheim SEE ALSO
co(1), rcs(1), rcsclean(1), rlog(1) rcsfreeze(1)
All times are GMT -4. The time now is 10:01 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy