BASH - Regular Expressions :Looking for one word on multiple lines.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH - Regular Expressions :Looking for one word on multiple lines.
# 1  
Old 01-11-2016
BASH - Regular Expressions :Looking for one word on multiple lines.

Im looking for a bash solution that will use Regular Expressions (not perl, sed or awk) to check the example data below and then give me a status.
which would be just simply Match or Mismatch.




Code:
SYS PS1 is present.
        Fan status: Normal
        Input Voltage status: Normal
        DC Output Voltage status: Normal
        Type: AC
        Thermal status: Normal

Im not asking about how to create a script to do this for me. I am asking for a "better" "nicer" regex expression than the one given below. Its just ugly ... know ?

Code:
.*Fan.*Normal.*\n.*Input.*Normal.*\n.*DC\sOutput.*Normal.*\n.*Type.*\n.*Thermal.*Normal

Thanks in advance
Pop

Last edited by Scrutinizer; 01-11-2016 at 05:09 PM.. Reason: Change quote tags to code tags
# 2  
Old 01-11-2016
What is the file/source to search data for?
What is the expected output?

Why not just:
Code:
grep Normal input_file

hth
# 3  
Old 01-11-2016
qiven the quoted source in my original post

The output could be
Quote:
Matched
It is matched because 4 instances of "Normal" were found.

or it could be
Quote:
not matched
because only 3 instance of "Normal" were found.
# 4  
Old 01-11-2016
Your source is a multiline textfile.
Your expected output is "match" or "not matched".

Still dont konw wether that is per item/line or for the whole file.
If it is for the whole file, it still isnt clear wether 'match' shall be printed if any (at least) or multiple are to be found.

Please enlight.

(Too late already again to read properly)
EDIT ----------------

Code:
num=$(grep Normal input_file|wc -l)
[ $num -eq 4 ] && echo Match || echo "No match"

Enojy
# 5  
Old 01-11-2016
It is quite possible to do this just using shell built-ins, but only if we understand what you're trying to do.

Where is your data stored? Is it in a file or is it stored in a shell variable?

Is the goal to find four occurrences of Normal, or is the goal to determine if every line that contains status: also contains Normal? (Or, is there some other rule that determines the desired output?)

Instead of just showing us an RE, show us how you are using that RE to get the results you want. It is hard to simplify an RE if we don't know the context in which it is being utilized. Show us the code (in CODE tags) you are using (if you have something working that you want to simplify) or show us the code you have tried (if you don't have a working solution yet).
# 6  
Old 01-12-2016
Almost nothing to do with bash. Try
Code:
[ 4 -eq $(grep -Ec "(Fan|Input|DC|Thermal).*Normal" file) ] && echo match || echo no match
match

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing Multiple spaces with a single space but excluding few regular expressions

Hi All. Attached are two files. I ran a query and have the output as in the file with name "FILEWITHFOURRECORDS.txt " I didn't want all the spaces between the columns so I squeezed the spaces with the "tr" command and also added a carriage return at the end of every line. But in two... (3 Replies)
Discussion started by: sparks
3 Replies

2. Shell Programming and Scripting

Regular expression to match multiple lines?

Using a regular expression, I would like multiple lines to be matched. By default, a period (.) matches any character except newline. However, (?s) and /s modifiers are supposed to force . to accept a newline and to match any character including a newline. However, the following two perl... (4 Replies)
Discussion started by: LessNux
4 Replies

3. Shell Programming and Scripting

regular expression grouping across multiple lines

cat book.txt book1 price 23 sku 1234 auth Bill book2 sku 1233 price 22 auth John book3 auth Frank price 24 book4 price 25 sku 129 auth Tod import re f = open('book.txt', 'r') text = f.read() f.close() m =... (2 Replies)
Discussion started by: chirish
2 Replies

4. Shell Programming and Scripting

Perl - Regular Expressions - Match complete word only

Hi Team, I have two strings like: xxx|yyy|Arizona Cardinals| Tell Cardinals | Cardinals bbb|Bell Earn, Jr | Bell Earn | Jayhawks | hawks I have a lookup file which has a set of strings. These need to be removed from above two strings Lookup file Contents: Bell Earn, Jr hawks... (2 Replies)
Discussion started by: forums123456
2 Replies

5. Shell Programming and Scripting

Regular Expressions

Hi Ilove unix and alwyas trying to to learn unix,but i am weak in using regular expressions.can you please give me a littel brief discription that how can i understand them and how to use .your response could lead a great hand in my unix love. (1 Reply)
Discussion started by: manoj attri
1 Replies

6. Shell Programming and Scripting

Regular Expressions

I am new to shell scripts.Can u please help me on this req. test_user = "Arun" if echo "test_user is a word" else echo "test_user is not a word" (1 Reply)
Discussion started by: chandrababu
1 Replies

7. UNIX for Dummies Questions & Answers

Regular expressions

In regular expressions with grep(or egrep), ^ works if we want something in starting of line..but what if we write ^^^ or ^ for pattern matching??..Hope u all r familiar with regular expressions for pattern matching.. (1 Reply)
Discussion started by: aadi_uni
1 Replies

8. UNIX for Advanced & Expert Users

Regular Expressions

Hi, below is a piece of code written by my predecessor at work. I'm kind of a newbie and am trying to figure out all the regular expressions in this piece of code. It is really a tough time for me to figure out all the regular expressions. Please shed some light on the regular expressions... (3 Replies)
Discussion started by: ramky79
3 Replies

9. UNIX for Dummies Questions & Answers

regular expressions

Hi Gurus, I need help with regular expressions. I want to create a regular expression which will take only alpha-numeric characters for 7 characters long and will throw out an error if longer than that. i tried various combinations but couldn't get it, please help me how to get it guys. ... (2 Replies)
Discussion started by: ragha81
2 Replies

10. Shell Programming and Scripting

Regular Expressions

I'm trying to parse RichText to XML. I want to be able to capture everything between the '/par' tag in the RTF but not include the tag itself. So far all I have is this, '.*?\\par' but it leaves '\par' at the end of it. Any suggestions? (1 Reply)
Discussion started by: AresMedia
1 Replies
Login or Register to Ask a Question