HOW TO - Bash REGEX - Print Error when More then One Occurence is Found Consecutively?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting HOW TO - Bash REGEX - Print Error when More then One Occurence is Found Consecutively?
# 1  
Old 10-16-2012
HOW TO - Bash REGEX - Print Error when More then One Occurence is Found Consecutively?

Hello All,

Bash Version: 4.1.10(1)


I'm trying to "verify" some user input. The User input will contain the "Absolute Path" a "Command" and any "Options"
of that Command.

For Example, say the user's input is:
Code:
user_input="/usr//local/myExample/check_process -p 'java' -w 10 -c 20"

I can't get my REGEX Pattern below to print an error when, say for example the user accidentally inputs 2 "/" consecutively by accident...


My REGEX Pattern/Code is:
*Notice the double "//" after "/usr"...
Code:
user_input="/usr//local/myExample/check_process -p 'java' -w 10 -c 20"

MY_PATTERN="((/{1,1}[[:alnum:]_-]*)+).*$"

if [[ "$user_input" =~ $MY_PATTERN ]]
 then
        echo "TRUE"
else
        echo "FALSE"
fi

So my question is how can I get this to print FALSE when the user enters a double "//"?
I thought the part of my REGEX that has "/{1,1}" would mean match one and ONLY one consecutive "/"...?

Any thoughts would be great..!

Thanks in Advance,
Matt
# 2  
Old 10-16-2012
Code:
user_input="/usr//local/myExample/check_process -p 'java' -w 10 -c 20"

fixed_input=`echo ${user_input} | sed 's/\/*\//\//g'`

if [ "${user_input}" = "${fixed_input}" ]
then
        print "TRUE"
else
        print "FALSE"
fi

This User Gave Thanks to Yoda For This Post:
# 3  
Old 10-16-2012
Dont think bash has a "likes" operator "=~" which exists in perl so the basis of the if test is wrong...
# 4  
Old 10-16-2012
Hey bipinajith, thanks for the reply...

Ah ha, that's a good idea!

Just so I'm clear what that does...
Will that remove any consecutive "/" and replace it with only one "/" and save the input into a NEW variable, then compare
the NEW var with the original...?

Sounds like a plan to me... Cool, Thanks!


Thanks Again,
Matt

---------- Post updated at 03:49 PM ---------- Previous update was at 03:44 PM ----------

Hey Shamrock, thanks for the reply...


Bash does have a "=~" Operator... Isn't that the REGEX Operator?

From the Bash Manpage:
Quote:
An additional binary operator, =~, is available, with the same precedence as == and !=. When it is used, the string to the right of the operator is considered an extended regular expression and matched accordingly (as in regex(3)). The return value is 0 if the string matches the pattern, and 1 otherwise. If the regular expression is syntactically incorrect, the conditional expression's return value is 2. If the shell option nocasematch is enabled, the match is performed without regard to the case of alphabetic characters. Any part of the pattern may be quoted to force it to be matched as a string. Substrings matched by parenthesized subexpressions within the regular expression are saved in the array variable BASH_REMATCH. The element of BASH_REMATCH with index 0 is the portion of the string matching the entire regular expression. The element of BASH_REMATCH with index n is the portion of the string matching the nth parenthesized subexpression.
Thanks,
Matt

---------- Post updated at 04:32 PM ---------- Previous update was at 03:49 PM ----------

Hey bipinajith,

Will I get the same result with your sed command and this one below..?
Code:
### the "\/{2,}" will 2 or more consecutive forward slashes "/"
tmp_input=$(echo "$user_input" | sed 's/\/\{2,\}/\//g' )

Is there any real difference between yours and mine..?


Thanks,
Matt
# 5  
Old 10-16-2012
Yes, you will get the same result. Your command looks good.
This User Gave Thanks to Yoda For This Post:
# 6  
Old 10-16-2012
Ok my bad...so bash has that operator in any case if you want to look for the presence of two consecutive slashes then your regexp must be formed accordingly since that is what you want to find. Your regexp is looking for a single slash and $user_input contains it so it wont report false. I modified the code a bit to suit what you need to find...
Code:
user_input="/usr/local//myExample/check_process -p 'java' -w 10 -c 20"

MY_PATTERN="[/][/]+"

if [[ ! "$user_input" =~ "$MY_PATTERN" ]]
then
        echo "TRUE:  contains >= 2 consecutive slashes"
else
        echo "FALSE:  doesn't contain >= 2 consecutive slashes"
fi

This User Gave Thanks to shamrock For This Post:
# 7  
Old 10-17-2012
Ok Cool, thanks bipinajith!



Hey Shamrock, thanks for the reply!
Cool thanks, I'll give that a shot...

Thanks Again,
Matt
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print occurence number

Hi folks, I have a file with lots of lines in a text file,i need to print the occurence number after sorting based on the first column as shown below, thanks in advance. sam,dallas,20174 sam,houston,20175 sam,atlanta,20176 jack,raleigh,457865 jack,dc,7845 john,sacramento,4567 ... (4 Replies)
Discussion started by: tech_frk
4 Replies

2. Shell Programming and Scripting

Bash - trap error file not found

Hello. In bash, is there a way to trap error "file not found" when a script call another script which is not found; then abort. Example ( part of script running with -x option set) : + return 0 + RETURN_CODE=0 + ] + /root/bin/200_yast_install/00_reset_yast_install bash:... (5 Replies)
Discussion started by: jcdole
5 Replies

3. Shell Programming and Scripting

Help with using awk to print pattern/occurence

Hi, Do anybody know how to use awk to count the pattern at specific column? Input file M2A928K 419 ath-miR159a,gma-miR159a-3p,ptc-miR159a 60 miR235a . . Output file M2A928K 419 ath-miR159a,gma-miR159a-3p,ptc-miR159a 60 miR235a 3 . . I plan to count how many "miR" in column 3... (2 Replies)
Discussion started by: cpp_beginner
2 Replies

4. Shell Programming and Scripting

Regex to match only first occurence with grep

Hello to all, How would be the correct regex to match only the first occurence of the pattern 3.*6. I'm trying with 3.*6 trying to match only 34rrte56, but with my current regex is matching 4rrte567890123456789123powiluur56. And if I try with ? doesn't print anything echo... (6 Replies)
Discussion started by: Ophiuchus
6 Replies

5. Shell Programming and Scripting

Print between patterns - first occurence, second occurence etc

I have a file # cat asasas AAAAAA 11 22 33 44 BBBBB NILNILNIL AAAAAA 22 33 44 55 66 77 88 BBBBB NILNILNIL (2 Replies)
Discussion started by: anil510
2 Replies

6. Shell Programming and Scripting

sed print between 2 patterns only last occurence

Hi, I have a file, which contains the following log data. I am trying to print fromt he file the following data: I have tried using sed, but I am getting from the first pattern Thanks for your help. (5 Replies)
Discussion started by: sol_nov
5 Replies

7. UNIX for Dummies Questions & Answers

How to print first occurence

Hi there, how can i print the first pattern occurrence in a .log file? I want to print the filename of the first 17262? I tried but all I can do is print all the lines with the number 17262? I tried using awk and sed but nothing!:wall: I just want filename! Here´s an example: 17259... (3 Replies)
Discussion started by: BMatter
3 Replies

8. Shell Programming and Scripting

print first few lines, then apply regex on a specific column to print results.

abc.dat tty cpu tin tout us sy wt id 0 0 7 3 19 71 extended device statistics r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device 0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0 0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx I want to skip first 5 line... (4 Replies)
Discussion started by: kchinnam
4 Replies

9. Shell Programming and Scripting

bash syntax error: command not found

I am trying to create a shell that asks the user to enter their name, and compare it to my own by saying we have the same name or saying my name and that they have a nice name too. Here is my script... #!/bin/bash-x echo "Enter your name". read name if then echo "My name is Adam too"... (1 Reply)
Discussion started by: amaxey45
1 Replies

10. Shell Programming and Scripting

Print row if value in column 1 is the first occurence

Hi All, I would like to have a script which is able to perform the below. Print the whole row if column1 which is "0001" for the below example is the first occurrence. Subsequent "0001" occurrence will not be printed out and so on. Can any expert help ? Input: 0001 k= 40 0001 k= 2... (7 Replies)
Discussion started by: Raynon
7 Replies
Login or Register to Ask a Question