Find string multiple times, same line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find string multiple times, same line
# 1  
Old 07-11-2011
Find string multiple times, same line

Hi everybody,

Fairly simple question here:

I need an awk, sed, or grep command that will find the same string multiple times on one line

needs to return all lines which contain .02 twice.

I do know the exact number of characters in between the two occurrences of .02 if that helps, all such lines are --------.02------.02------ with exactly six characters in between, but an indefinite amount of characters before and after.

Thank you
# 2  
Old 07-11-2011
Code:
grep -E '\.02.{6}\.02' infile

This User Gave Thanks to verdepollo For This Post:
# 3  
Old 07-11-2011
Code:
awk ' (p=index($0, ".02") ) 
       {
            p+=3; 
            a=substr($0,p) 
            if(index(a, ".02")==1)
            {
                print $0}
            }  
       }
       {next} ' inputfile > outputfile

Start with that.
# 4  
Old 07-11-2011
does that work with directories as well? I tried using this:
Code:
u=$(ls /XXXX/XXXX/XXXX/I3 | grep -E '\.02.{6}\.02')

to find all the lines which contained .02 .02 in that directory, to no avail.

Last edited by Franklin52; 07-11-2011 at 02:27 PM.. Reason: Please use code tags for data and code samples
# 5  
Old 07-11-2011
Are you looking for files or strings within lines of a file.
This User Gave Thanks to shamrock For This Post:
# 6  
Old 07-11-2011
Should have been more clear about the terms of search.

Hi everybody,

Fairly simple question here:

I need an awk, sed, or grep command that will find the same string multiple times on one line

needs to return all lines which contain .02 twice.

I do know the exact number of characters in between the two occurrences of .02 if that helps, all such lines are --------.02------.02------ with exactly six characters in between, but an indefinite amount of characters before and after.

Thank you

I'm searching a directory for the occurence of this string (.02) twice in the filenames within this directory.
# 7  
Old 07-11-2011
Code:
grep -E "\.02.{6}\.02" file

This User Gave Thanks to fpmurphy For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replace string and create new file multiple times

First of all, apologies if this has already been answered elsewhere. I haven't quite been able to find what I'm looking for yet, so hopefully this won't come across as repetition. I have a file consisting of ~100 nearly identical lines, each of which contains multiple instances of the string I... (11 Replies)
Discussion started by: pseudo.seppuku
11 Replies

2. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

3. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

4. Shell Programming and Scripting

awk to find lines containing word that occur multiple times

i have a script that scans a log file every 10 minutes. this script remembers the last line of the log and then uses it to continue monitoring the log when it runs again 10 minutes later. the script searches the log for a string called MaxClients. now, how can i make it so that when the... (7 Replies)
Discussion started by: SkySmart
7 Replies

5. Shell Programming and Scripting

Sed replace using same pattern repeating multiple times in a line

Sed replace using same pattern repeating multiple times in a line I have text like below in a file: I am trying to replace the above line to following How can I acheive this? I am able to do it if the occurrence is for 1 time: But If I try like below I am getting like this: I have to... (4 Replies)
Discussion started by: sol_nov
4 Replies

6. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

7. Emergency UNIX and Linux Support

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (3 Replies)
Discussion started by: nithins007
3 Replies

8. Shell Programming and Scripting

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (6 Replies)
Discussion started by: nithins007
6 Replies

9. Shell Programming and Scripting

Find multiple string in one file using find command

Hi, I want find multiple string in one file using find coomand. And keeping it in one variable.grep is not working. (5 Replies)
Discussion started by: vivek1489
5 Replies

10. Shell Programming and Scripting

matching multiple times in same line

Hi, I am stuck with pattern matching. I need to match a particular pattern several times in a same line and replace them. for ex., I need to convert (abc XY) (bvf ZY) bla bla to XY ZY bla bla I tried.. s/\(+ (.+)\)/$1/gi and it works (2 Replies)
Discussion started by: oldtrash
2 Replies
Login or Register to Ask a Question