Possible to grep string based on surrounding strings?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Possible to grep string based on surrounding strings?
# 1  
Old 05-15-2012
Possible to grep string based on surrounding strings?

I was wondering if it was possible to grep a pattern based on the surround text. For example, if i have an input file like this:

Code:
titleA
titleB
titlex
titleC
titleD
titlex
titleE

And I want to grep "title" and save the results only if it is not followed with a "titlex". My output would look like this:
Code:
titleA
titleC
titleE

Is this possible? I feel like awk should come into play, but I'm not great at awk at the moment.
# 2  
Old 05-15-2012
your expected output will have following two also rt?
Code:
titleB
titleD

try this
Code:
grep -v titlex infile

# 3  
Old 05-15-2012
Quote:
Originally Posted by 47shailesh
your expected output will have following two also rt?
Code:
titleB
titleD

try this
Code:
grep -v titlex infile

I don't want to have titleB and titleD in my output. With "grep -v" I would get titleA, titleB, titleC, titleD, and titleE.

I found the following, which would produced an output of just titleB and titleD, I just need to make it do the opposite!

Code:
awk '/titlex/ { print prv_line; next } { prv_line = $0 }' input.txt

# 4  
Old 05-15-2012
grep is not a programming language, it can't understand 'if x then do y'. It can't even remember lines for later. awk is a programming language, though, and can do both.

Perhaps something like
Code:
$ cat regafter.awk

# Recall N lines ago up to 9 lines
function last(N)
{
        if(N>L) return("");
        return(LINE[(L-N)%10]);
}

{ LINE[(++L)%10]=$0 } # Remember line for later

# If this line and the last line don't match titlex, print last line.
(last(1) ~ /title[^xX]/) && /title[^xX]/        { print last(1) }
# Do the same test for the last line by itself.
END {   if(last(0) ~ /title[^xX]/) print last(0); }

$ awk -f regafter.awk data

titleA
titleC
titleE

$

If awk doesn't work, try nawk or gawk.
# 5  
Old 05-15-2012
NEVERMIND. THIS DOES NOT WORK CORRECTLY when titlex is the first line in the file or when there are consecutive instances of titlex. I leave it here only for your amusment.


Corona's awk solution is more efficient, since it only reads the data once, but here's a simple ed solution.

Code:
ed -s data <<EOED
g/titlex/-,.d
g/title/
Q
EOED

Or, if you prefer a less readable oneliner
Code:
printf %s\\n g/titlex/-,.d g/title/ Q | ed -s data

Regards,
Alister

Last edited by alister; 05-15-2012 at 12:48 PM.. Reason: Solution is incorrect.
# 6  
Old 05-15-2012
Quote:
Originally Posted by Corona688
grep is not a programming language, it can't understand 'if x then do y'. It can't even remember lines for later. awk is a programming language, though, and can do both.

Perhaps something like
Code:
$ cat regafter.awk

# Recall N lines ago up to 9 lines
function last(N)
{
        if(N>L) return("");
        return(LINE[(L-N)%10]);
}

{ LINE[(++L)%10]=$0 } # Remember line for later

# If this line and the last line don't match titlex, print last line.
(last(1) ~ /title[^xX]/) && /title[^xX]/        { print last(1) }
# Do the same test for the last line by itself.
END {   if(last(0) ~ /title[^xX]/) print last(0); }

$ awk -f regafter.awk data

titleA
titleC
titleE

$

If awk doesn't work, try nawk or gawk.
Thanks corona. If I want to substitute "titlex" with another string, how can I do that? In the code, I replaced "title[^xX]" with another string to test, but it outputs the new searched string.
# 7  
Old 05-15-2012
The regex I put in accepts titleG but not titleX or titlex. If you give it a regex that doesn't reject titleX, it of course won't reject titleX...

Please show what you did.

Better yet, show the actual input you have and actual output you want, since your sample data doesn't seem to be it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Single grep to multiple strings with separate output per string

I need to grep multiple strings from a particular file. I found the use of egrep "String1|String2|String3" file.txt | wc-l Now what I'm really after is that I need to separate word count per each string found. I am trying to keep it to use the grep only 1 time. Can you guys help ? ... (9 Replies)
Discussion started by: nms
9 Replies

2. Shell Programming and Scripting

Grep pattern only and surrounding lines

Hello, I am trying to grep search a pattern and a line before it. cat input >record1 hello1hello2hellonhello3 >record2 helloohello1hello2hello3 When I use, grep with -o option and either of -A/B/C options, I still can't see lines before or after the pattern. But the exact pattern is... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

3. Shell Programming and Scripting

Need to print the next word from the same line based on grep string condtion match.

I need to fetch particular string from log file based on grep condition match. Actual requirement is need to print the next word from the same line based on grep string condtion match. File :Java.lanag.xyz......File copied completed : abc.txt Ouput :abc.txt I have used below... (5 Replies)
Discussion started by: siva83
5 Replies

4. Shell Programming and Scripting

Grep a part of file based on string identifiers

consider below file contents cat myOutputFIle.txt 8 CCM-HQE-ResourceHealthCheck: Resource List : No RED/UNKNOWN resource Health entries found ---------------------------------------------------------- 9 CCM-TraderLogin-Status: Number of logins: 0... (4 Replies)
Discussion started by: vivek d r
4 Replies

5. UNIX for Dummies Questions & Answers

Delete strings in file1 based on the list of strings in file2

Hello guys, should be a very easy questn for you: I need to delete strings in file1 based on the list of strings in file2. like file2: word1_word2_ word3_word5_ word3_word4_ word6_word7_ file1: word1_word2_otherwords..,word3_word5_others... (7 Replies)
Discussion started by: roussine
7 Replies

6. Shell Programming and Scripting

grep a string and print strings on that line

i have a file like below ABS 12234 A C 12G CFY 23865 A C 34D i want to grep "A C" then print ABS 12G 12234 CFY 34D 23865 Thanks! (4 Replies)
Discussion started by: kingpeejay
4 Replies

7. Shell Programming and Scripting

how to get surrounding lines of my grep search?

hi, if I grep a file, sometimes I want to see as an eg 2 lines above and below my grep results. how can this be done thanks (3 Replies)
Discussion started by: JamesByars
3 Replies

8. Shell Programming and Scripting

grep a string in the lines between 2 strings of a file

Hi , Please help me with the following problem: I have an xml file with the following lines <cisco:name> <cisco:mdNm>Cisco Device 7500 A Series</cisco:mdNm> <cisco:meNm>10.1.100.19</cisco:meNm> <cisco:ehNm>/shelf=1</cisco:ehNm> <cisco:subname> <cisco:sptp>Cisco PortA... (8 Replies)
Discussion started by: bhagirathi
8 Replies

9. Shell Programming and Scripting

how to get surrounding lines of grep result

hi, if i have a file and i want to search for the word error using grep, i usually want to see the surrounding lines too as they contain info about the error. what would be a nice way to achieve this? thanks (6 Replies)
Discussion started by: JamesByars
6 Replies

10. UNIX for Dummies Questions & Answers

How can you show lines surrounding a search string?

I would like to be able to grep (or some such thing) a search argument and then display the line plus the preceding 3 lines of the file and the following 3 lines of the file. Any ideas? Thanks in advance! :D (3 Replies)
Discussion started by: robster
3 Replies
Login or Register to Ask a Question