Grep everything between two pattern if match not found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep everything between two pattern if match not found
# 1  
Old 12-17-2015
Grep everything between two pattern if match not found

I need to help to work this

Print everything between 2 patterns if grep is not found the search word
example
Code:
Detroit
orange
cat
bat 
rat
apple

Code:
sed  -n "/Detroit,/apple/p" d |grep  chicago

output would be
Code:
Detroit
orange
cat
bat 
rat
apple


Last edited by Scrutinizer; 12-17-2015 at 02:13 PM.. Reason: CODE tags
# 2  
Old 12-17-2015
Quote:
Originally Posted by jhonnyrip
I need to help to work this

Print everything between 2 patterns if grep is not found the search word
example

Detroit
orange
cat
bat
rat
apple

sed -n "/Detroit,/apple/p" d |grep chicago

output would be
Detroit
orange
cat
bat
rat
apple
Not sure if I understood this, but what do you want to display if grep **does** find something in the file?
I assume in that case you want to show the output of grep.
Is the following what you're looking for?
Code:
$ 
$ cat f34
Detroit
orange
cat
bat 
rat
apple
$ 
$ 
$ # grep doesn't find anything in the file; show the output of sed
$ 
$ grep chicago f34 || sed -n '/Detroit/,/apple/p' f34
Detroit
orange
cat
bat 
rat
apple
$ 
$ 
$ # grep does find something in the file; show the output of grep, do not show the output of sed
$ 
$ grep cat f34 || sed -n '/Detroit/,/apple/p' f34
cat
$ 
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Modify text file if found multiple pattern match for every line.

Looking for help, i have input file like below and want to modify to expected output, if can without create additional file, hope can direct modify it. have 2 thing need do. 1st is adding a word (testplan generation off) after ! ! IPG: Tue Aug 07 14:31:17 2018 2nd is adding... (16 Replies)
Discussion started by: kttan
16 Replies

2. Shell Programming and Scripting

Pattern match using grep between two files

Hello Everyone , I have two files. I want to pick line from file-1 and match with the complete data in file-2 , if there is a match print all the match lines in file 3. Below is the file cat test1.txt vikas vikasjain j ain testt douknow hello@vik@ # 33 ||@@ vcpzxcmvhvdsh... (1 Reply)
Discussion started by: mailvkjain
1 Replies

3. Shell Programming and Scripting

Displaying text till pattern match found in a line

Hi All, From the below line if we want to display all the text till found pattern dot/. I was trying with the below code but couldn't able to print text before the pattern. it display texts which is found after pattern. awk '/assed/{print;getline;print}' file_name | sed 's/^*. *//' input... (4 Replies)
Discussion started by: Optimus81
4 Replies

4. Shell Programming and Scripting

kill a process if grep match is found

Hi, I need something unusual, I guess. I need to start a process, and if that process displays a specific error message, I need to kill that process and restart it. Something like: startprocess | grep -i "This is the specific error message" && kill $pidof(startprocess) Explanation, I need... (4 Replies)
Discussion started by: burek
4 Replies

5. Solaris

Using grep to print just the pattern match

Hi all, Is it possible for grep to output just the pattern match and not the whole line when it comes across a match? I know you can adjust the number of trailing or leading lines that are printed, but am yet to find anything that outputs just the pattern match. Cheers, Tim (5 Replies)
Discussion started by: muzzaw
5 Replies

6. Shell Programming and Scripting

Grep to match unknown pattern

Hi there I would like to search a file for a certain pattern, but i don't know the exact pattern i need to search for. What i do know is that i need to search for the pattern that exists the most in a certain file. so if a file looksike this: summer, summer, winter, spring, summer... (4 Replies)
Discussion started by: mrchilly
4 Replies

7. UNIX for Advanced & Expert Users

How to prevent grep command from throwing a system trap if No match is found.

Hi How to prevent grep command from throwing a system trap(or returning error status) if No match is found in the specified file(s) ? Consider this simple shell script: #!/usr/bin/ksh trap 'STATUS=$?;set +x;echo;echo error $STATUS at line nb $LINENO executing :\ `sed -n... (2 Replies)
Discussion started by: cool.aquarian
2 Replies

8. UNIX for Dummies Questions & Answers

Grep and display n lines after the match is found.

Hello, How do I use grep to find a pattern in a list of file and then display 5 lines after the pattern is matched Eg: I want to match the string GetPresentCode in all files in a folder and then see 4 lines following this match. I am not sure if grep is what should be used to achieve. Thanks!... (3 Replies)
Discussion started by: cv_pan
3 Replies

9. Shell Programming and Scripting

Pattern match in grep

Hi, I try to grep lines with pattern like float_or_int float_or_int float_or_int where float_or_int is any float or any integer of any length. I tried some : grep "* * *" FILENAME #Work for integers but not for floats grep '\<.*\> \<.*\> \<.*\>' FILENAME #Work for float but not... (4 Replies)
Discussion started by: tipi
4 Replies

10. UNIX for Dummies Questions & Answers

how to use pattern match with grep

hi, i'm having problem like this. i wish to grep some keyword from certain files in one directory. let's say i have a lot of files in my directory with the name of file.1 file.2 file.3 ...... file.500 i only wish to grep the keyword from file.20, file.21, file.23 .... file.50 i tried... (5 Replies)
Discussion started by: rei
5 Replies
Login or Register to Ask a Question