Search for string on a range of lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for string on a range of lines
# 1  
Old 09-07-2015
Search for string on a range of lines

Hi,

I am trying to parse a file searching for specific set of string and then within those set of strings finding a keyword. The script works the way that I intended it to be but I thought it could be a lot simpler may be.

Any advice will be much appreciated.

The script at the moment is as below.

Code:
#!/bin/ksh

file_to_search="x.txt"
search1="CREATE INDEX"
#search1="DROP INDEX"
grep -in "^${search1}" ${file_to_search} > x.ksh.tmp.00
num_lines=`wc -l x.ksh.tmp.00 | awk '{ print $1 }'`
#echo "num_lines = $num_lines"

count=0
while read line
do
   let count=$count+1
   create_index_txt=`sed -n ${count}p x.ksh.tmp.00 | awk -F: '{ print $2 }'`
   start=`sed -n ${count}p x.ksh.tmp.00 | awk -F: '{ print $1 }'`

   if [[ $count -eq $num_lines ]] ; then
      end=`wc -l ${file_to_search} | awk '{ print $1 }'`
   else
      let x=$count+1
      end=`sed -n ${x}p x.ksh.tmp.00 | awk -F: '{ print $1 }'`
   fi

   echo "- Searching timing for - [ $create_index_txt ] within --> start = $start // end = $end"
   elapsed=`sed -n ${start},${end}p ${file_to_search} | grep -i "^elapsed:" | head -1`
   echo "     Elapsed = ${elapsed}"
done < x.ksh.tmp.00

It initially grep for all strings starting with CREATE INDEX from a text file, x.txt and re-direct the result to a file name x.ksh.tmp.00.

Sample output of the x.ksh.tmp.00 is as below:

Code:
15:CREATE INDEX ABC.TESTCCB_XYZ_SOLD_B7
33:CREATE INDEX ABC.TESTCCB_XYZ_CRAP_CON_B3
51:CREATE INDEX ABC.TESTCCB_XYZ_VAT_B1
56:CREATE INDEX ABC.TESTCCB_XYZ_VAT_B1
66:CREATE INDEX ABC.TESTCCB_XYZ_CRAP_CON_B3
81:CREATE INDEX ABC.TESTCCB_XYZ_VAT_B3
96:CREATE INDEX ABC.TESTCCB_XYZ_SOLD_B3
111:CREATE INDEX ABC.TESTCCB_XYZ_TRIP_B3
126:CREATE INDEX ABC.TESTCCB_XYZ_XXX_B3
141:CREATE INDEX ABC.TEST_XYZ_VAT_B3
156:CREATE INDEX ABC.TEST_XYZ_SOLD_B3
171:CREATE INDEX ABC.TEST_XYZ_REASON_B3
186:CREATE INDEX ABC.TEST_XYZ_TRIP_B3
201:CREATE INDEX ABC.TEST_XYZ_XXX_B3
216:CREATE INDEX ABC.TEST_XYZ_CRAP_CON_B3
231:CREATE INDEX ABC.TEST_XYZ_ERROR_B3
236:CREATE INDEX ABC.TEST_XYZ_ERROR_B3
246:CREATE INDEX ABC.TEST_XYZ_COM_END_B3
261:CREATE INDEX ABC.TEST_XYZ_COM_B3
276:CREATE INDEX ABC.TEST_XYZ_SUB_INFO_B3

Then on a while loop, it read each line of the file x.ksh.tmp.00 taking from it the start and end line of which I then uses sed to search for the first line that start with the string elapsed:
Code:
sed -n ${start},${end}p ${file_to_search} | grep -i "^elapsed:" | head -1

I've uploaded the file x.txt. I've purposedly changed some of the strings but the does not affect the result of running the script. I just modify the string for security purposes. Excerpt of the file is as below:

Code:
04-SEP-15 15:45:24

Elapsed: 00:00:00.01
DOC>* Script to create indexes
DOC>*/
DROP INDEX ABC.TESTCCB_XYZ_SOLD_B7
DROP INDEX ABC.TESTCCB_XYZ_SOLD_B7
               *
ERROR at line 1:
ORA-01418: specified index does not exist


Elapsed: 00:00:00.00
CREATE INDEX ABC.TESTCCB_XYZ_SOLD_B7

Index created.

Elapsed: 00:16:15.54
ALTER INDEX ABC.TESTCCB_XYZ_SOLD_B7

Index altered.

Elapsed: 00:00:00.03
DROP INDEX ABC.TESTCCB_XYZ_CRAP_CON_B3
DROP INDEX ABC.TESTCCB_XYZ_CRAP_CON_B3
               *
ERROR at line 1:
ORA-01418: specified index does not exist


Elapsed: 00:00:00.00
CREATE INDEX ABC.TESTCCB_XYZ_CRAP_CON_B3

Index created.

Elapsed: 00:31:17.98
ALTER INDEX ABC.TESTCCB_XYZ_CRAP_CON_B3

Index altered.

Elapsed: 00:00:00.04
DROP INDEX ABC.TESTCCB_XYZ_VAT_B1
DROP INDEX ABC.TESTCCB_XYZ_VAT_B1
               *
ERROR at line 1:
ORA-01418: specified index does not exist


Elapsed: 00:00:00.01
CREATE INDEX ABC.TESTCCB_XYZ_VAT_B1

Index created.

Elapsed: 00:18:15.82
CREATE INDEX ABC.TESTCCB_XYZ_VAT_B1

Index altered.

# 2  
Old 09-07-2015
From what i understand, i take the first scripts works as expected, but on the 2nd step it is not clear to me what exactly you want to achieve.

However, i assume its:
Code:
sed -n ${start},${end}p ${file_to_search}x.ksh.tmp.00 | grep -i "^elapsed:" | head -1

Or else i'm even more confused why even creating the x.ksh.tmp.00 file

Because of wich i would like to suggest to use something like:
Code:
file_tmp=x.ksh.tmp.00

Anyway, could you rephrase your requirement for your 2nd parsing?

ty & hth
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Search specific string logfile specific date range

Hi, I have logfile like this.. === 2014-02-09 15:46:59,936 INFO RequestContext - URL: '/eyisp/sc/skins/EY/images/pickers/comboBoxPicker_Over.png', User-Agent: 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko': Unsupported with Accept-Encoding header === 2015-02-09... (8 Replies)
Discussion started by: kishk
8 Replies

2. Shell Programming and Scripting

Search for 2 string in 2 lines with sed ?

Hi ! I want to search a string in all lines with sed. If that string is there, i want to look for an other string in the next line.If that string is there i want to put an other line under it. eg: aaa bbb ccc ddd cat bla.txt | sed -e '/aaa/a\' -e ' \!!!' in the upper case, i would... (6 Replies)
Discussion started by: fugitivus
6 Replies

3. UNIX for Dummies Questions & Answers

Search for a specific String in a log file for a specific date range

Hi, I have log file which rolls out every second which is as this. HttpGenRequest - -<!--OXi dbPublish--> <created="2014-03-24 23:45:37" lastMsgId="" requestTime="0.0333"> <response request="getOutcomeDetails" code="114" message="Request found no matching data" debug="" provider="undefined"/>... (3 Replies)
Discussion started by: karthikprakash
3 Replies

4. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

5. Shell Programming and Scripting

How to Search string(which is in 2 lines) in a file?

Hello, I want to search 2 lines from the file. I wanted to display all such matches. Example file: ================== Testfile is test TEST1 TEST2 testing the file string to do testing TEST1 TEST2 sample strings ================= I wanted to search the file with 2 lines " TEST1... (3 Replies)
Discussion started by: balareddy
3 Replies

6. Shell Programming and Scripting

Search a string and replace the same string above two lines?.

I need to search this "<CardinalMPI>" string and replace the same string above two lines of the string. Nov 16, 2012 12:58:34 PM INFO: Centinel LookUp ResponseXML : <CardinalMPI> <ReasonCode></ReasonCode> <ErrorDesc></ErrorDesc> <MerchantReferenceNumber></MerchantReferenceNumber>... (4 Replies)
Discussion started by: laknar
4 Replies

7. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

8. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

9. Shell Programming and Scripting

Printing 10 lines above and below the search string: help needed

Hi, The below code will search a particular string(say false in this case) and return me 10 lines above and below the search string in a file. " awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r;print("***********************************");print;c=a;}b{r=$ 0}' b=10 a=10 s="false" " ... (5 Replies)
Discussion started by: vimalm22
5 Replies

10. UNIX for Dummies Questions & Answers

Search for string and return lines above and below

Hi, I have a file with the following contents: COMPANY ABCD TRUCKER JOE SMITH TRUCK 456 PLATE A12345678 TRUCKER BILL JONES TRUCK 789 PLATE B7894561 I need to create a script or search command that will search for this string '789' in the file. This string is unique and only... (7 Replies)
Discussion started by: doza22
7 Replies
Login or Register to Ask a Question