Programming :: String Comparison - Get Regular Expression To Work In If / Else Statement?

Jun 29, 2010

how do I get this regular expression to work in an if/else statement? This is just a little script for learning BASH. don't be too harsh.

This script will test if a certain number of files with 1-4 in their filename exist and print their filename. An error message will be printed if not.

#
for i in `ls file[1-9]`
do
if [[ "$i" == *1-4 ]] ; then
echo "This file, $i, ends in a number between 1-4"
else
echo "Error, this file, $i, does not end with a number between 1-4"
fi
done

I get this error. ./file_test.sh: 13: [[: not found

View 2 Replies


ADVERTISEMENT

Programming :: Regular Expression Doesnt Work In An Elsif Statement?

Feb 3, 2010

I was doing an exercise on Learning Perl, 3rd edition. (exercise chapter 10 btw) The problem asks to create a program that generates a random number and asks the user to guess. It should tell the user if the guess is lower or higher and exit if the user types either exit or quit. My code is the following:

Code:
#! /usr/bin/perl
$correct = int(1+rand 100);

[code]....

View 2 Replies View Related

Programming :: Regular Expression {x} Doesn't Work?

Sep 2, 2010

I am trying myself on regular expressions. A read on [URL]/regex.htm) that {x} means search X times preceding character. So, why is this :

Code:
wstation~> echo '123456789' | grep [0-9][0-9][0-9]
123456789 # MATCH !! I have 3 numbers in a row.

[code]...

View 2 Replies View Related

Programming :: Perl Scripting: Search For Regular Expression Not Work?

Oct 13, 2010

I'm writing a Perl script to find an old key in a file and replace it with a new codefirst the program should find the old key in the input file. here is the way I used in my script. but it doesn't work.May you please let me know what is wrong and how I can correct it?the key is stored in the file in the following format:

PHP Code:
Key=("1234567" , someOtherVrable) 
I want 1234567

[code]...

View 1 Replies View Related

Programming :: Extract A Substring Using Regular Expression With SED

May 7, 2011

I've spent most of the evening browsing the web, trying many things I've found on various forums, but nothing seems to work.

I have a test.txt file containing many lines like the following ones :

...
<insert_random_text>228.00 €<insert_more_random_text>
<insert_random_text>17.50 €<insert_more_random_text>
<insert_random_text>1238.13 €<insert_more_random_text>
...

And I want to extract :

...
228.00
17.50
1238.13
...

There is always one occurrence of € in each line. I want the numeric value that precedes this € occurrence. The random text (before and after) may contain numbers too, so the € may be important to parse, in order to correctly identify the number to return. The last character that precedes the number to extract is always a ">" (coming from an HTML tag).

View 9 Replies View Related

Programming :: Python Replace() With Regular Expression?

Mar 16, 2011

i cant use regular expression in replace function. I want to do this

a = ' Bad boy'
b = a.replace("b[aA]d", '')

Ofcourse this is a simple example for my query. Its not working so shall i assume regex dont work with replace function ?

View 1 Replies View Related

Programming :: Sed Regular Expression Match Everything Up To A Certain Character

May 27, 2009

I need to use sed to edit a file that contains just one line. This should be pretty simple, but I've googled and can't seem to figure it out. I need to match everything from a certain string up until the first comma in the line. There are multiple commas in the line and my matching pattern is matching up until the last comma, not the first.

Here is what I'm trying:

As you can see it is matching up until the last comma. Seems like the .* is matching any character including the other commas. The output from this that I am hoping to achieve:

How can I get the regular expression to match from asdf: up until the first comma?

View 3 Replies View Related

Programming :: AWK: Compare Apache Dates Without Using Regular Expression

May 15, 2010

I'm writing a loganalysis application and wanted to grab apache log records between two certain dates. Assume that a date is formated as such: 22/Dec/2009:00:19 (day/month/year:hour:minute) Currently, I'm using a regular expression to replace the month name with its numeric value, remove the separators, so the above date is converted to: 221220090019 making a date comparison trivial.. but.. Running a regex on each record for large files, say, one containing a quarter million records, is extremely costly.. is there any other method not involving regex substitution? here's the function doing the convertion/comparison

[Code]...

View 13 Replies View Related

Programming :: Use A Regular Expression In Parantheses Like ([/+[]()]) To Get The Specific Letters?

May 3, 2010

I found such simple example of awk

Code:
awk '
BEGIN { a = "1abc 2def"
b = gensub(/(.+) (.+)/, "\2 \1", "g", a)
print b }'

However I don't understand why the regular expresion in parentheses doesn't work how I woud expect. If I delete the .+ so it does the same.

Code:
awk '
BEGIN { a = "1abc 2def"
b = gensub(/() ()/, "\2 \1", "g", a)
print b }'

I wanted to use a regular expression in parantheses like ([/+[]()]) to get the specific letters.

Code:
awk '
BEGIN { a = "1abc 2def"
b = gensub(/([/+[]()])/, "//\1", "g", a)
print b }'

But why the regexp in () isn't working

View 16 Replies View Related

Programming :: Awk - Substitution With Parts Of Matched Regular Expression Used?

Apr 30, 2011

I remember reading that using sed, you can do this with parentheses: s/abc(something)def/(something)else/g I can't find an explanation of how to do something like this with Awk. Say you have this in an HTML file, where (number) stands for a one or two-digit number:

<sup>(number)</sup>

And you want to change that to this:

<a name="(number)t" href="#(number)b"><sup>(number)</sup></a>

How would this be possible? Would you have to use sed?

View 4 Replies View Related

Programming :: Perl Regular Expression To Change Character

May 26, 2009

Part of a perl script I am writing need to change the character at an index to upper case. Now i am new to perl and i am having some trouble getting it to do it. In c++ i would do something like

Code:

Now from what i understand the same thing is possible in perl using regular expressions. But i cant get it to work.

View 2 Replies View Related

Programming :: Regular Expression Double Escape For Files That End With .la ?

Mar 18, 2010

I have been battling with regular expressions and am a little lost. I want to find all my files that end with .la and have been trying this

Code:
slocate -r .la$

which finds them but also files like mozilla. The escaped full stop seems to be ignored however this works :

Code:
slocate -r \.la$

so why is the double escaping needed ?

View 3 Replies View Related

Programming :: Script For Executing A Program And Doing String Comparison

May 6, 2009

I am fairly new to linux still and I'm currently trying to write a shell script (which I have never done) that is supposed to run the xrestop(like top) program. What I want to do is execute the program and check the identifier column to see if a certain program is running. If it is running I want to strip the data from that row of the terminal say every 5 seconds or so and put it in a text file. Is this going to be a complicated thing?

Heres a list of the columns when you run xrestop program res-base Wins GCs Fnts Pxms Misc PxmMem Other Total PID Identifier.

View 9 Replies View Related

Programming :: String Comparison - [: =: Unary Operator Expected?

Nov 23, 2010

I would like to compare the nmlookup result of IP's stored in the file ips with a list of PC names stored in the file pcs.

Code:
for line in `cat "ips"`
do

[code]....

View 2 Replies View Related

Programming :: Regular Expression Compile Failed - Throwing Error

Feb 15, 2011

The following command would remove the leading astericks, spaces, slashes, tabs and is present in an awk script.

Code:
gsub(/^[/* ]*/,"")

The above code is working fine when i run in Cygwin on windows machine. But when i run in Ubuntum throws the following error.

Code:
awk: ./Format.awk: line 30: regular expression compile failed (bad class -- [], [^] or [)
^[
awk: 30: unexpected character ''
awk: ./Format.awk: line 30: runaway regular expression /,""); ...

View 2 Replies View Related

Programming :: Regular Expression To Match URL Matches Fragments - Python?

Aug 26, 2010

I have made this:

Code:

from urllib import urlopen import re

current_site = urlopen("http://www.krak.dk/").read()
search = re.findall("((http://|https://|ftp://)|(www.))+(([a-zA-Z0-9.-]+.[a-zA-Z]{2,4})|([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}))(/[a-zA-Z0-9%:/-_?.'~]*)?", current_site)

[code]....

I only want to match complete URL's. how do i avoid matching the fragments ?

View 4 Replies View Related

Programming :: Regular Expression Working Fine In JavaScript But Not In Python?

Jun 22, 2010

I have this regular expression

Code:

(@description ").*(";)

I want to match anything in between the quotes its working fine in JavaScript but its not working in my python program:

Code:

#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
import re

[code]....

View 3 Replies View Related

Programming :: Tcl Expect - Unable To Use Regular Expression In Interact Mode

Dec 8, 2010

I have something like the following in my expect script:

Code:

interact {
-nobuffer -re {^s
} {

[code]....

I have put the "^" anchor to match only those pattern that does not have anything before "s" e.g.

1.when I type "s" followed by "enter" key it should match.

2.if I type something like "chess" followed by "enter" key it shouldn't match. the second case is also being matched by the regular expression I have in my code.

View 4 Replies View Related

Programming :: Using Grep And Regular Expression To Find Class References In C++ File?

May 28, 2010

I'm trying to math all class references in a C++ file using grep with regular expression. I'm trying to know if a specific include is usuless or not, so I have to know if there is a refence in cpp. I wrote this RE that searches for a reference from class ABCZ, but unfortunately it isn't working as I espected:

grep -E '^[^(/*)(//)].*[^a-zA-Z]ABCZ[]*[*(<:;,{& ]'
^[^(/*)(//)] don't math comments in the begging of the line ( // or /* )
.* followed by any character

[code]....

Well, I can get patterns like this:

class Test: public ABCZ{
class Test: public ABCZ {
class Test : public ABCZ<T>

[code]....

View 4 Replies View Related

Programming :: Create Regular Expression - Get English Written Sentence Only Using Preg_match

Aug 7, 2010

From the following code

Code:
<table width="100%" border="0" cellspacing="3" cellpadding="3">
<tr>
<td colspan="2"><p style="width:460px; display:block;">

I wan to get this text only from the whole html code. </p></td> From the above code I want to get that english written sentence only using php preg_match or anything other which makes it possible. I've tried following so far but doesn't work

Code:
preg_match("#<td colspan="2"><p style="width:460px; display:block;">[ .]+</p>#i", $aboveCode, $match);

View 5 Replies View Related

Programming :: Create Regular Expression In Perl To Remove All Delimiters (|) From File?

May 12, 2010

I am trying to create a regular expression in perl to remove all the delimiters (|) from a file. I was also thinking might I be able to use tr?

View 11 Replies View Related

Programming :: Sed Regular Expression - Write Script To Edit Text Files?

Jun 29, 2010

I am trying to write a script to edit text files formatted like this:

Code:
(MCAL@Contig766:0.30207,CGIG@CVIR_Contig1014:0.13977,(HASI@HDIS_Contig573:0.16828,(CAPI@LCIN_5594371:0.36581,CFOR@FQH745302RIQ7Y:1.91244)0.160:0.00019)0.939:0.15648);
There are never line breaks or spaces in the actual files.

I want to delete all instances of the character "@" and everything between it and the next "," (including that comma) or the next ")" (including that close parentheses) whichever comes first. My desired output file would be like this:

Code:
(MCAL,CGIG,(HASI,(CAPI,CFOR)0.160:0.00019)0.939:0.15648);
I figured out how to do this using sed for either "," or ")" but both looking for whichever comes first.

View 6 Replies View Related

Programming :: Python Regular Expression - AttributeError: 'NoneType' Object Has No Attribute 'group'

Aug 23, 2010

I am trying to scan a website for http references (links) with this script:

Code:

from urllib import urlopen
import re
current_site = urlopen("http://en.wikipedia.org/wiki/").read()
search = re.search('href="[a-zA-Z0-9]"', current_site)

[code]....

I get the following error message:

Code: Traceback (most recent call last): File "C:UsersadminDesktopcrawler.py", line 8, in <mo print search.group(0) AttributeError: 'NoneType' object has no attribute 'group' I have googled the error

View 2 Replies View Related

Programming :: Regular Expression To Match Lines In A File That Do Not Start With # Or A Blank Space?

Dec 25, 2010

i have a file like this

# comments
#comments
#comments
bla bla

[code]....

i want to grep lines which do not start with # or a blank space. like

bla bla
bla bla

how do i do this? i tried grep --invert-match '^#' which gives lines not starting with # but gives me blank lines too i tried grep --invert-match '^#|^ ' which will give lines not starting with # OR not starting with blank ( which means any line including ones starting with #

View 3 Replies View Related

Programming :: Getting Error 'couldn't Compile Regular Expression Pattern: Invalid Character Range'

Nov 19, 2008

I'm having a bit of trouble with a regular expression I'm trying to write and I'm not sure if it's something Tcl specific or my lack of regexp understanding.

[Code]...

I get a number of strings passed to a proc in the format 3|x where x is a number, either 0 or within the range 5-12. My understanding is that that regexp will match the literal '3' followed by a '|', the escapes the special meaning of |, and then 0 or, because of |, a number within the range 5-12. However I'm getting the error 'couldn't compile regular expression pattern: invalid character range'.

View 3 Replies View Related

Programming :: Gawk Statement To Match Range Of Filed For String?

Jul 30, 2010

adf 32 324 100 100 24 234I want a gawk statement to match the above line if lets sayand field between 4 and 6 == 100.

View 2 Replies View Related

Programming :: Regular Expression Query - Express Disallowing Empty Brackets "()"

Feb 1, 2011

A string can assume the following valid values:"(a, b)" "(a)" "(b)"how I can express this disallowing empty brackets "()"? (I am using Python.)

View 6 Replies View Related

Programming :: Getting A Comparison Operator To Work?

Feb 8, 2010

I'm very new at Bash scripting and have a bone head issue that I'm trying (and failing) to resolve. I cannot get this one IF statement to work, it seems the comparison operator does not think the resulting number from the $b*$c+$b operation is an integer even though it is a number. Below is a small proof of concept script with the bit I'm having trouble with.

Code:
#! /bin/bash
a=800
b=700
c=.15
if [ "$a" -le "$(echo "($b*$c+$b)"|bc)" ]

[Code]...

View 6 Replies View Related

Programming :: Java Regex - Use Regular Expressions Find Out What The First Two Characters Of The String Are

Feb 2, 2011

What I am doing is reading the text from a text document and storing all of the text inside of a ArrayList. I then set one of the values of the Arraylist as a string. I want to use regular expressions find out what the first two characters of the String are. if first two characters = "//" then function(); I only care about the first two characters though. If you need any more information, just ask.

View 4 Replies View Related

Programming :: Special Expression `-l STRING' For "test" Command?

Apr 21, 2010

I have searched and searched in regards to this documented 'special expression '-l STRING' for the 'test' command, and to no avail, have I found out why it does not work on my system.

The example always given is:
Code:
test -l abc -gt 1 && echo yes
And the returned result is:
Code:
bash: test: -l: unary operator expected
The documention is usually as follows:
Numeric tests

Numeric relationals. The arguments must be entirely numeric (possibly negative), or the special expression `-l STRING', which evaluates to the length of STRING. Then examples are given, including the one I provided above. Does anyone else have this issue of getting an error when trying this special expression of 'l STRING' ???

View 4 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved