Re: Question about regular expressions
On 7/21/11 4:33 AM, dave jones wrote: > Hi, > > I have a config file below: > > $user= 'root'; // This is the username > > if $user is found, I want to display root. > Anyone knows how to programming in C or some other language? thank you. > > Regards, > Dave. > ___ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org" Let us assume you want to read your file, then display each entry for "$user1" , "$user2" and so on: grep "$user" my_file | awk '{ print $3}' | sed -e "s/\'//" | sed -e "s/;//" 1/ open my_file and only display lines containing "$user" 2/ display the 3rd item on the line 3/ remove the single quotes and the ; I'm sure it can be optimized a bit but basically, that'll do what I assume you want. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: Question about regular expressions
On Wed, July 20, 2011 10:33 pm, dave jones wrote: > Hi, > > I have a config file below: > > $user= 'root'; // This is the username > > if $user is found, I want to display root. > Anyone knows how to programming in C or some other language? thank you. I'm not quite sure what you are asking here. Found where? Display where? Are we just reading through the config file? Are we processing some other file with it's config? It should be simple in Perl or some similar scripting language, if I knew what you meant. (Except for the comment, that could be a Perl file. If so, one way to 'process' the config file would be to execute it in your main program, and then just use the variables assigned in it as regular variables.) Daniel T. Staal --- This email copyright the author. Unless otherwise noted, you are expressly allowed to retransmit, quote, or otherwise use the contents for non-commercial purposes. This copyright will expire 5 years after the author's death, or in 30 years, whichever is longer, unless such a period is in excess of local copyright law. --- ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Question about regular expressions
Hi, I have a config file below: $user= 'root'; // This is the username if $user is found, I want to display root. Anyone knows how to programming in C or some other language? thank you. Regards, Dave. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: Regular expressions
Written by Reid Linnemann on 08/20/07 11:58>> Written by Christer Hermansson on 08/18/07 18:08>> Derek Ragona wrote: At 12:04 PM 8/18/2007, Christer Hermansson wrote: I also found some basic example at http://www.grymoire.com/Unix/Sh.html#uh-88 : 8<8<8<8<8< #!/bin/sh echo "Type in a number" read ans number=`expr "$ans" : "([0-9]*)"` if [ "$number" != "$ans" ]; then echo "Not a number" elif [ "$number" -eq 0 ]; then echo "Nothing was typed" else echo "$number is a fine number" fi 8<8<8<8<8< The above example doesn't work on my freebsd box. Maybe I need to update my system, sitting with 6.0R which never been updated. You have a syntax error using expr. Do a man on expr for more details but if you change that line from: number=`expr "$ans" : "([0-9]*)"` to: number=`expr "$ans" : "\([0-9]*\)"` You will get the desired results. Also when debugging scripts remember to add: set -x to your script on the second line, and see what the script lines are actually doing. -Derek Thanks Derek ! Now both the example and my own code works for me. I changed my code from "^[A-Za-z0-9_-]+$" to "\([A-Za-z0-9_-]*\)" It seems that FreeBSD's expr want some different syntax than the webbased test tool at http://regexlib.com/RETester.aspx No, your expression is double quoted, which means the shell will expand it before passing it to expr. Parens are expanded by shells, they manipulate the order of operations (i.e. 'echo 1 || echo 2 && echo 3' vs. '(echo 1 || echo 2) && echo 3'). As a result, you must escape the parens or the shell will gobble them up. Disregard that, I am a moron. :/ ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: Regular expressions
Written by Christer Hermansson on 08/18/07 18:08>> Derek Ragona wrote: At 12:04 PM 8/18/2007, Christer Hermansson wrote: I also found some basic example at http://www.grymoire.com/Unix/Sh.html#uh-88 : 8<8<8<8<8< #!/bin/sh echo "Type in a number" read ans number=`expr "$ans" : "([0-9]*)"` if [ "$number" != "$ans" ]; then echo "Not a number" elif [ "$number" -eq 0 ]; then echo "Nothing was typed" else echo "$number is a fine number" fi 8<8<8<8<8< The above example doesn't work on my freebsd box. Maybe I need to update my system, sitting with 6.0R which never been updated. You have a syntax error using expr. Do a man on expr for more details but if you change that line from: number=`expr "$ans" : "([0-9]*)"` to: number=`expr "$ans" : "\([0-9]*\)"` You will get the desired results. Also when debugging scripts remember to add: set -x to your script on the second line, and see what the script lines are actually doing. -Derek Thanks Derek ! Now both the example and my own code works for me. I changed my code from "^[A-Za-z0-9_-]+$" to "\([A-Za-z0-9_-]*\)" It seems that FreeBSD's expr want some different syntax than the webbased test tool at http://regexlib.com/RETester.aspx No, your expression is double quoted, which means the shell will expand it before passing it to expr. Parens are expanded by shells, they manipulate the order of operations (i.e. 'echo 1 || echo 2 && echo 3' vs. '(echo 1 || echo 2) && echo 3'). As a result, you must escape the parens or the shell will gobble them up. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: Regular expressions
Derek Ragona wrote: At 12:04 PM 8/18/2007, Christer Hermansson wrote: I also found some basic example at http://www.grymoire.com/Unix/Sh.html#uh-88 : 8<8<8<8<8< #!/bin/sh echo "Type in a number" read ans number=`expr "$ans" : "([0-9]*)"` if [ "$number" != "$ans" ]; then echo "Not a number" elif [ "$number" -eq 0 ]; then echo "Nothing was typed" else echo "$number is a fine number" fi 8<8<8<8<8< The above example doesn't work on my freebsd box. Maybe I need to update my system, sitting with 6.0R which never been updated. You have a syntax error using expr. Do a man on expr for more details but if you change that line from: number=`expr "$ans" : "([0-9]*)"` to: number=`expr "$ans" : "\([0-9]*\)"` You will get the desired results. Also when debugging scripts remember to add: set -x to your script on the second line, and see what the script lines are actually doing. -Derek Thanks Derek ! Now both the example and my own code works for me. I changed my code from "^[A-Za-z0-9_-]+$" to "\([A-Za-z0-9_-]*\)" It seems that FreeBSD's expr want some different syntax than the webbased test tool at http://regexlib.com/RETester.aspx -- Christer Hermansson ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: Regular expressions
At 12:04 PM 8/18/2007, Christer Hermansson wrote: Hi. I'm trying to use regular expressions inside a shell script (/bin/sh) on my freebsd box and can't get it to work so I searched the web and found http://regexlib.com/RETester.aspx On this webpage I could test my pattern "^[A-Za-z0-9_-]+$" and everything was fine, did exactly what I wanted to do, check that a string only contains some combination of the characters A-Z, a-z, 0-9, hyphen - and underscore _. I also found some basic example at http://www.grymoire.com/Unix/Sh.html#uh-88 : 8<8<8<8<8< #!/bin/sh echo "Type in a number" read ans number=`expr "$ans" : "([0-9]*)"` if [ "$number" != "$ans" ]; then echo "Not a number" elif [ "$number" -eq 0 ]; then echo "Nothing was typed" else echo "$number is a fine number" fi 8<8<8<8<8< The above example doesn't work on my freebsd box. Maybe I need to update my system, sitting with 6.0R which never been updated. Is there anyone who has some advice about how to get regular expressions to work in FreeBSD shell script ? -- Christer Hermansson You have a syntax error using expr. Do a man on expr for more details but if you change that line from: number=`expr "$ans" : "([0-9]*)"` to: number=`expr "$ans" : "\([0-9]*\)"` You will get the desired results. Also when debugging scripts remember to add: set -x to your script on the second line, and see what the script lines are actually doing. -Derek -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. MailScanner thanks transtec Computers for their support. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Regular expressions
Hi. I'm trying to use regular expressions inside a shell script (/bin/sh) on my freebsd box and can't get it to work so I searched the web and found http://regexlib.com/RETester.aspx On this webpage I could test my pattern "^[A-Za-z0-9_-]+$" and everything was fine, did exactly what I wanted to do, check that a string only contains some combination of the characters A-Z, a-z, 0-9, hyphen - and underscore _. I also found some basic example at http://www.grymoire.com/Unix/Sh.html#uh-88 : 8<8<8<8<8< #!/bin/sh echo "Type in a number" read ans number=`expr "$ans" : "([0-9]*)"` if [ "$number" != "$ans" ]; then echo "Not a number" elif [ "$number" -eq 0 ]; then echo "Nothing was typed" else echo "$number is a fine number" fi 8<8<8<8<8< The above example doesn't work on my freebsd box. Maybe I need to update my system, sitting with 6.0R which never been updated. Is there anyone who has some advice about how to get regular expressions to work in FreeBSD shell script ? -- Christer Hermansson ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"