Grep pattern only and surrounding lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep pattern only and surrounding lines
# 1  
Old 07-25-2016
Grep pattern only and surrounding lines

Hello,

I am trying to grep search a pattern and a line before it.

Code:
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 being printed fine though. Any pointers are appreciated.

Expected output is

Code:
cat input | grep -o "hello[a-z]hello"
>record1
hellonhello
>record2
helloohello

But current output is

Code:
cat input | grep -o -B 1 "hello[a-z]hello"
hellonhello
helloohello


Thanks in advance.
# 2  
Old 07-25-2016
-o and -B are mutually exclusive, -o tells it to show nothing but the matching text. Remove -o.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 07-25-2016
Thank you @Corona688. I had that doubt in mind.

But I would like to see only the pattern and the line before it. Can you please share any comments?
# 4  
Old 07-25-2016
Code:
sed 's/.*\(hello[a-z]hello\).*/\1/' infile


Last edited by rdrtx1; 07-25-2016 at 02:45 PM..
This User Gave Thanks to rdrtx1 For This Post:
# 5  
Old 07-25-2016
grep doesn't do that, you can use awk:
Code:
awk 'match($0,/hello[a-z]hello/) { print L ; print substr($0,RSTART,RLENGTH); } ; { L=$0 }' input

This User Gave Thanks to Corona688 For This Post:
# 6  
Old 07-25-2016
Amazing! Thank you!
This User Gave Thanks to jacobs.smith For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep all lines with the pattern .sh

Linux version : Oracle Linux 6.5 Shell : bash In the the below text file (someString.text), I want to grep all lines with .sh in it. ie. Only the lines mysript.sh and anotherscript.sh should be returned. My below attempts failed. I gather that in regular expression world, dot (.) is the... (3 Replies)
Discussion started by: John K
3 Replies

2. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

3. Shell Programming and Scripting

Grep lines before a pattern having some other pattern

Hi All, I am trying to fetch lines before a pattern, I got to know about -B flag in grep but we have to pass the number to get those lines before some pattern say (X), now what if I want to get line/s with some other pattern say (Y) before X pattern? How to get about it? please help. Input:... (5 Replies)
Discussion started by: dips_ag
5 Replies

4. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

5. Shell Programming and Scripting

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: 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... (14 Replies)
Discussion started by: jl487
14 Replies

6. Shell Programming and Scripting

Print the above and below lines for the grep pattern.

Hi, i would like to get the above and below lines of the grep pattern . For ex : file as below: chk1- aaaa 1-Nov chk2 -aaaa ########## chk1-bbbbbb 1-Nov chk2-bbbbbb ######### my search pattern is date : 1-Nov i need the o/p as below chk1- aaaa 1-Nov (6 Replies)
Discussion started by: expert
6 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. UNIX for Dummies Questions & Answers

Grep with 8 lines before and after pattern.

OK. I have a file I'd like to be able to grep, but on top of returning the line where the pattern matches, I'd like to be able to get the previous 8 lines and the following 8 lines. Is there a way to do this? (2 Replies)
Discussion started by: mrwatkin
2 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