Search for multiple strings in specific position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for multiple strings in specific position
# 1  
Old 03-18-2009
Search for multiple strings in specific position

Hi,

I need to search for some strings in specific positions in a file.

If the strings: "foo1", "foo2" or "foo3" is on position 266 or position 288 in a file i want the whole line printed.


Any idea how to do it?
# 2  
Old 03-18-2009
Hi

you can use grep -x for finding specific string in file.
# 3  
Old 03-18-2009
But i cannot specify the position then?

The strings may be at more places but they are only interesting if they are in this positions.
# 4  
Old 03-18-2009
With some hardwiring, but... something to start with:
Code:
nawk -v str='foo1|foo2|foo3' 'substr($0, 266, 4) ~ str || substr($0, 288, 4) ~ str' myFile

# 5  
Old 03-18-2009
Smilie

Thanks, thanks & thanks again!

It solved my problem.

Smilie
# 6  
Old 03-18-2009
a more generic solution:

nawk -v str='foo1 foo2 foo3' -v pos='266 288' -f hugo.awk myFile

hugo.awk:
Code:
BEGIN {
   split(str, strA, FS)
   split(pos, posA, FS)
}
{
   for(strI=1; strI in strA; strI++)
      for(posI=1; posI in posA; posI++)
        if ( posA[posI] == match($0, strA[strI]) ) {
           print
           next
        }
}


Last edited by vgersh99; 03-18-2009 at 01:13 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

2. Post Here to Contact Site Administrators and Moderators

Search for a pattern and replace a space at specific position with a Character in File

In file, we have millions of records each of 1000 in length. And at specific position say 800 there is a space, we need to replace it with Character X if the ID in that row starts with 123. So far i have used the below which is replacing space at that position to X but its not checking for... (3 Replies)
Discussion started by: Jagmeet Singh
3 Replies

3. Shell Programming and Scripting

Whether we can search multiple strings using or in grep -F

Hi, Whether we can search multiple strings using or in grep -F In Generally, grep -F "string1" "filename.txt" How to search for multiple string using grep -F as we using grep grep "string1\|string2" "filename.txt" Regards, Nanthagopal A (10 Replies)
Discussion started by: nanthagopal
10 Replies

4. Shell Programming and Scripting

Search & Replace: Multiple Strings / Multiple Files

I have a list of files all over a file system e.g. /home/1/foo/bar.x /www/sites/moose/foo.txtI'm looking for strings in these files and want to replace each occurrence with a replacement string, e.g. if I find: '#@!^\&@ in any of the files I want to replace it with: 655#@11, etc. There... (2 Replies)
Discussion started by: spacegoose
2 Replies

5. Shell Programming and Scripting

Find position of character in multiple strings in a file

Greetings. I have a file with information like this: AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU? AMNDHRKEEU?AMNDHREOEU? AMNDHREU?AHRKEOEU?AMNDHRKEU?AMNDKEOEU? What I need to extract is the position, in every line, of every occurrence of '?' A desired output would be something... (6 Replies)
Discussion started by: Twinklefingers
6 Replies

6. Shell Programming and Scripting

Search multiple Strings in a File

Hi I want to search multiple strings in a file . But the search should start with "From" Keyword and end with before "Where" keyword. Please suggest me. Thanks (2 Replies)
Discussion started by: sboss
2 Replies

7. Shell Programming and Scripting

Search in specific position and print the whole line

I have two files abc.dat and sant.dat (Big file 60k rows) for every line's 1,4 of abc.dat need to seach if this is present on 28,4 of sant.dat every line. if its present the output needs to go to bde.dat Example: contents abc.dat aaaa bbbb cccc dddd contents sant.dat this is... (4 Replies)
Discussion started by: ssantoshss
4 Replies

8. Shell Programming and Scripting

search a line and insert string into specific at position

Hi, guys. I have one question: How can I search for a line with certain string in it and then insert a string into this line? For example: There is a file called shadow, the contents of it are below: ************************** ... yuanz:VIRADxMsadfDF/Q:0:0:50:7:::... (9 Replies)
Discussion started by: daikeyang
9 Replies

9. UNIX for Dummies Questions & Answers

Search for multiple strings and mail accordingly

Hi, I need to find if there is any error in the last few lines of the log file and send a mail accordingly.For example, Following errors can be logged in the log file. ERR_1="DB Connection not established" ERR_2="Server Unloading" I need to find if these errors are found in the log file and... (3 Replies)
Discussion started by: AnneAnne
3 Replies

10. Shell Programming and Scripting

Print lines with search string at specific position

Hi Folks, I have a file with all fields defined by byte position, but any field can be empty so I cannot print lines based on a search of specific columns. I need to print all lines of this file where the string of two characters at byte position 100-101 contains the number 27. Any ideas? ... (4 Replies)
Discussion started by: HealthyGuy
4 Replies
Login or Register to Ask a Question