searching searching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting searching searching
# 1  
Old 06-03-2004
Lightbulb searching searching

Guys

Im checking some of my files for errors. I want to be able to find text and make sure it's located on the right place.

Example

Chapter 1
TEXT LKJ
TEXT 12Y
more and more text
Chapter 2
TEXT 34G
TEXT HHG
more and more text
Chapter 3
TEXT FG45
TEXT 11w
more and more text

many more Chapters

To ensure that "TEXT HHG" is located on the right chapter,
I been doing grep -B 100 -A 100 "TEXT HHG" file

but it's still a pain
is there a way I can check if "TEXT HHG" is between Chapter 2 and Chapter 3 ?

Thank you
Tony
tony3101
# 2  
Old 06-04-2004
Yes indeed, use sed

Put this inside chapters.sed

Code:
#!/usr/bin/sed -nf

/Chapter 2/,/Chapter 3/ {
   /TEXT HHG/p
}

chmod the script to make it executable, and assuming that your text is in file, run
Code:
./chapters.sed file

and this should do the trick.

EDIT: You could actually make this more generic. Put this into chapters.sh
Code:
 #!/bin/sh

 chap_1=$1
 chap_2=$2
 search=$3

 sed -n "/Chapter $1/,/Chapter $2/ {
    /$3/p
 }" file

Then call the script with
Code:
chapters.sh 2 3 "TEXT HHG"

This is then reusable for other searches.

Cheers
ZB

Last edited by zazzybob; 06-04-2004 at 06:45 AM..
# 3  
Old 06-04-2004
Perhaps use grep...

grep -nE 'Chapter 2|Chapter 3|TEXT HHG' file

...gives output like....

43:Chapter 2
88:TEXT HHG
106:Chapter 3
# 4  
Old 06-04-2004
Thank you
Works Great
tony3101
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

2. UNIX for Dummies Questions & Answers

Searching

Hi guys, I have a very common issue :( im trying to work it out but I am still not used to it. my problem is searching. very often I should look for piece of string in a text file or a file itself: I want to learn some easy and professional commands to ease this routine for me. I want to be... (2 Replies)
Discussion started by: messi777
2 Replies

3. Shell Programming and Scripting

grep searching

I am making a script but having little problem. at one part I need to find one number format or other format from a file.. those formats are xxx-xx-xxxx or xxxxxxxxx i tried grep '( \{3\}-\{2\}-\{3\} |\{9\})' if i do them sepratly it work but like this it is not working Please check... (7 Replies)
Discussion started by: Learnerabc
7 Replies

4. UNIX for Advanced & Expert Users

Searching for files

Hi, I have the following command to list files beginning with a specific name and containing some text... find . -type f -name "dm_merge_domain_adm*" -exec grep -il "Error Message:" '{}' \; -print|xargs ls -ltr It works fine, but seems to list two of each file, when they only exist once...any... (1 Reply)
Discussion started by: chrislluff1976
1 Replies

5. UNIX for Dummies Questions & Answers

searching

How would i search to find all the lines consisting of of only the letter "z" followed by any four characters? (1 Reply)
Discussion started by: trob
1 Replies

6. Shell Programming and Scripting

file searching...

Hi! i'm trying to do a script (i'm not an expert, as you will see...:o) to search files in a directory (and its subdirectories). I'd like to have something like this: mysearch -a arg1 -b arg2 -c arg3 ecc... I'd like to be able to search for files in different ways: for example, with my... (1 Reply)
Discussion started by: Kaminski
1 Replies

7. UNIX for Dummies Questions & Answers

Searching in VI

Hi, I would like to do a search and replace on a file using vi. Something like this: :%s/dst_port=****//g I want to search the entire file and replace all text that does not match dst_port=**** with space or nothing. In other words delete everything except for dst_port=****. The four *... (1 Reply)
Discussion started by: andyblaylock
1 Replies

8. Shell Programming and Scripting

searching a file

Hi At the moment I am able to add text to a file but what I need to do is make sure the same value is not duplicated. Can anyone tell me how to search a file and prevent an update from taking place if the value to be entered already exists in the file? Any help would be greatly... (4 Replies)
Discussion started by: straight_edge
4 Replies

9. UNIX for Advanced & Expert Users

Searching Files

Hi, I have a file /db01/dat/march 2006/7001DW06.03B Please note, between "march 2006" there is a space/tab. While running the following script, it identifies /db01/dat/march ----> as first file 2006/7001DW06.03B ---> as second file. SRC_PATH = /db01/dat SEARCH_FILENAME =... (12 Replies)
Discussion started by: ronald_brayan
12 Replies

10. Shell Programming and Scripting

searching for {

Hello, I am having a hard time trying to do the following: I have a file that looks like this: 0 CacheMaxConn 4 64 0 RMThread 16 3423423 7 DataSource 0 /hello/sas/ses 0 {94545B4-E343-1410-81E4-08000000} 3 DDBE 3 ... (4 Replies)
Discussion started by: yotoruja
4 Replies
Login or Register to Ask a Question