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?
# 8  
Old 05-15-2012
Quote:
Originally Posted by Corona688
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.
I created a new input, to test the code:
Code:
titleA
titleB
TEST
titleC
TEST

and used the following:
Code:
# 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) ~ /TEST/) && /TEST/        { print last(1) }

# Do the same test for the last line by itself.
END {   if(last(0) ~ /TEST/) print last(0); }

and the output I get is the string "TEST"
# 9  
Old 05-15-2012
That string accepts TEST, it doesn't reject it. It even rejects titleA. It won't print things it doesn't accept. Given your input data, that's what I thought you wanted.

Rewriting.
# 10  
Old 05-15-2012
Quote:
Originally Posted by Corona688
That string accepts TEST, it doesn't reject it. It even rejects titleA. It won't print things it doesn't accept. Given your input data, that's what I thought you wanted.

Rewriting.
ok, I think i'm starting to understand it. I just assumed the code could be universal where I could simply swap the strings.
# 11  
Old 05-15-2012
This should easily let you input an exact string to take as the string to reject.

Code:
$ cat regafter2.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) != REJECT) && $0 != REJECT { print last(1) }
# Do the same test for the last line by itself.
END {   if(last(0) != REJECT) print last(0); }

$ awk -v REJECT="TEST" -f regafter2.awk data2

titleA

$

This User Gave Thanks to Corona688 For This Post:
# 12  
Old 05-16-2012
i understand that the script works for "REJECT" but how can this be modified to accept wildcards. For example, if input is:

Code:
titleA
titleB
TEST FILLER
titleC
TEST

and REJECT=TEST, I would expect the output to be just titleA. However, the current code outputs titleA, titleB, and TEST FILLER.
# 13  
Old 05-16-2012
Hi.

I had trouble all the way through this thread understanding the requirements. I'll assume that followed with means immediately followed by. Here is a solution for the last sample:
Code:
#!/usr/bin/env bash

# @(#) s2	Demonstrate search for pattern-accept followed by pattern-reject.
# See http://sourceforge.net/projects/cgrep/

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C cgrep

FILE=${1-data3}
ACCEPT="title"
REJECT="TEST"

pl " Input data file $FILE:"
cat $FILE

pl " Results, $ACCEPT to $REJECT:"
cgrep -a "^.*$ACCEPT.*\n.*$REJECT" $FILE
pl " Results, invert $ACCEPT to $REJECT:"
cgrep -v -a "^.*$ACCEPT.*\n.*$REJECT" $FILE

exit 0

producing:
Code:
% ./s2

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
cgrep ATT cgrep 8.15

-----
 Input data file data3:
titleA
titleB
TEST FILLER
titleC
TEST

-----
 Results, title to TEST:
titleB
TEST FILLER
titleC
TEST

-----
 Results, invert title to TEST:
titleA

The cgrep utility is non-standard, but very useful. See the URL for the source for anyone to compile and use.

Best wishes ... cheers, drl

( Edit 1: replaced wrong version of script. )
( Edit 2: correct minor typo )

Last edited by drl; 05-16-2012 at 01:46 PM..
# 14  
Old 05-16-2012
I am flailing around trying to solve for you a problem, which you keep changing. If you would lay down exactly what you need to do plainly the first time, I could solve it once.

Code:
$ cat regafter3.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) !~ REJECT) && $0 !~ REJECT { print last(1) }
# Do the same test for the last line by itself.
END {   if(last(0) !~ REJECT) print last(0); }

$ awk -v REJECT="TEST.*" -f regafter3.awk data3

titleA

$

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