Patterns with egrep/sed/awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Patterns with egrep/sed/awk?
# 1  
Old 02-01-2012
Patterns with egrep/sed/awk?

I have an array with characters, what I want is if there are other characters in the array which I am looking for than take action that is print BAD ARRAY. So far my code just finds characters but instead I want that it should look for other characters.
Code:
echo "A B C D F" | egrep -o "D | F"
O/P
D
F

I want a one liner that can look for characters other than D and F and if the array contains other characters than print Bad Array. That is I expect the array should have only D and F. In that case no action.
Thanks in adavance

Last edited by radoulov; 02-01-2012 at 12:16 PM.. Reason: Code tags fixed.
# 2  
Old 02-01-2012
There's lots of ways to do this, many of them slow, some of them less so. What's your system? What's your shell?

---------- Post updated at 10:22 AM ---------- Previous update was at 10:18 AM ----------

For BASH and newer ksh, you can do this:

Code:
if [[ ! -z "${STR//[AD ]/}" ]]
then 
        echo "Str contains the following non-AD chars:  ${STR//[AD ]/}"
fi

# 3  
Old 02-01-2012
Thanks for your reply but my shell is tcsh.
# 4  
Old 02-01-2012
You have my deepest condolences.

csh programming considered harmful

Code:
echo "$STR" | grep -v '[^AD ]' > /dev/null || echo "string contains non-AD"

# 5  
Old 02-01-2012
That is funny but sometimes you are forced to use what is establish although my attempt is to create parallel scripts in Bash.
Code:
echo "A A D D" | grep -v '[^AD]' > /dev/null || echo "string contains non AD"
The expected output is:
nothing
but got
string contains non AD

I want: output as "string contains non AD" only when input is "A D E H"
# 6  
Old 02-01-2012
Don't forget that I'm helping you anyway. It's no joke. If you're not being forced to use csh, you should count your blessings and run. Its inventor admits to "not being very good at programming when he wrote it", and its problems aren't bugs -- they're design flaws, unfixable. It really is that bad. Sooner or later you'll find out why by yourself.

My regular expression had a space in it, yours didn't. It's not optional -- that's the list of characters for it to accept and, without space, it doesn't accept spaces...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/sed command to extract the string between 2 patterns but having some particular value

Hi - i have one file with content as below. ***** BEGIN 123 ***** BASH is awesome ***** END ***** ***** BEGIN 365 ***** KSH is awesome ***** END ***** ***** BEGIN 157 ***** KSH is awesome ***** END ***** ***** BEGIN 7123 ***** C is awesome ***** END ***** I am trying to find all... (4 Replies)
Discussion started by: reldb
4 Replies

2. Shell Programming and Scripting

Egrep patterns in a file and limit number of matches to print for each pattern match

Hi I need to egrep patterns in a file and limit number of matches to print for each matched pattern. -m10 option is not working out in my sun solaris 5.10 Please guide me the options to achieve. if i do head -10 , i wont be getting all pattern match results as output since for a... (10 Replies)
Discussion started by: ananan
10 Replies

3. Shell Programming and Scripting

Specifying patterns to egrep or awk

given this information: ^\w{3} {11} -]+ kernel:( \]+\.]+\])? ]+: media error \(bad sector\): status=0x]+ { DriveReady SeekComplete Error }$ ^\w{3} {11} -]+ kernel:( \]+\.]+\])? end_request: I/O error, dev ]+, sector ]+ i need to set up monitoring for these strings. but as you can see, they... (2 Replies)
Discussion started by: SkySmart
2 Replies

4. Shell Programming and Scripting

sed and egrep question

Its really 2 questions, but both are pretty basic. Linux Redhat 1. Need to do a search and replace on a file. I need to append '--' (comment out the line) to specific lines based on a wildcard search. So if I Have GRANT SOME_ROLE_OR_USER ... I dont care what comes after that.... (2 Replies)
Discussion started by: guessingo
2 Replies

5. Shell Programming and Scripting

Grab nth occurence in between two patterns using awk or sed

Hi , I have an issue where I want to parse through the output from a file and I want to grab the nth occurrence of text in between two patterns preferably using awk or sed ! TICKET NBR : 1 !GSI : 102 ! 3100.2.112.1 11/06/2013 15:56:29 ! 3100.2.22.3 98 ! 3100.2.134.2... (8 Replies)
Discussion started by: OTNA
8 Replies

6. Shell Programming and Scripting

sed and egrep in perl

Hi i have a data file whcih contains the data as follows : FH332OY86|AAABBB CCCC DDDA FHLMC 30 8.000|FHLMC|3|30|8.00000000|1986|26.29164289|3.29544844|0.00000000|10.05940539|107.50704264|Mar 8 2013 12:00AM|20130311|D|DA|DAA|DAAC|201302 FH332OY87|AAABBB CCCC DDDA FHLMC 30... (9 Replies)
Discussion started by: ptappeta
9 Replies

7. Shell Programming and Scripting

[Solved] Sed/awk print between patterns the first occurrence

Guys, I am trying the following: i have a log file of a webbap which logs in the following pattern: 2011-08-14 21:10:04,535 blablabla ERROR blablabla bla bla bla bla 2011-08-14 21:10:04,535 blablabla ERROR blablabla bla bla bla ... (6 Replies)
Discussion started by: ppolianidis
6 Replies

8. Shell Programming and Scripting

Sed/awk print between different patterns the first occurrence

Thanks for the help yesterday. I have a little modification today, I am trying the following: i have a log file of a webbap which logs in the following pattern: 2011-08-14 21:10:04,535 blablabla ERROR Exception1 blablabla bla bla bla bla 2011-08-14... (2 Replies)
Discussion started by: ppolianidis
2 Replies

9. Shell Programming and Scripting

deleting lines between patterns using sed or awk

hi, Here is excerpt from my xml file <!-- The custom module to do the authentication for LDAP --> </login-module> <login-module code="com.nlayers.seneca.security.LdapLogin" flag="sufficient"> <module-option... (1 Reply)
Discussion started by: sunrexstar
1 Replies

10. Shell Programming and Scripting

sed/awk help to match list of patterns and remove from org file

Hi, From the pattern mentioned below remove lines based on pattern range. Conditions 1 Look For all lines starting with ALTER TABLE and Ending with ; and contains the word MOVE.I wanto to remove these lines from the file sample below. Note : The above pattern list could be found in... (1 Reply)
Discussion started by: rajan_san
1 Replies
Login or Register to Ask a Question