Affichage des articles dont le libellé est regex. Afficher tous les articles
Affichage des articles dont le libellé est regex. Afficher tous les articles

mercredi 26 mars 2014

regex line by line over file topic




I can't get this to work.
It runs but there is no output when I try it on a file.


#!/usr/bin/python

import os
import sys
import re
from datetime import datetime

#logDir = '/nfs/projects/equinox/platformTools/RTLG/RTLG_logs';
#os.chdir( logDir );

programName = sys.argv[0]
fileName = sys.argv[1]

#pattern = re.compile('\s*\"SHELF-.*,SC,.*,:\"Log Collection In Progress\"')
re.M
p = re.compile('^\s*\"SHELF-.*,SC,.*,:\\"Log Collection In Progress\\"')
l = ' "SHELF-17:LOG_COLN_IP,SC,03-25,01-18-58,NEND,NA,,,:\"Log Collection In Progress\",NONE:1700000035-6364-1048,:YEAR=2014,MODE=NONE"'

# this works :-)
m = p.match( l )
if m:
print( l )

# this doesn't match anything (or the if doesn't work) :-(
with open(fileName) as f:
for line in f:
# debug code (print the line without adding a linefeed)
# sys.stdout.write( line )
if p.match(line):
print(line)


The test file just has one line:
"SHELF-17:LOG_COLN_IP,SC,03-25,01-18-58,NEND,NA,,,:\"Log Collection In Progress\",NONE:1700000035-6364-1048,:YEAR=2014,MODE=NONE"





mardi 18 mars 2014

Re: Find and replace multiple RegEx search expressions topic




Jignesh Sutar wrote:

> Hi,
>
> I'm trying to delete contents of a .txt log file, matching on multiple
> re.sub criteria but not sure how to achieve this.
>
> Below is an illustration of what I am trying to achieve (of course in this
> example I can combine the 3 re.sub into a single re expression but my
> actual code will have a dozen plus expression I need to match on so easier
> to keep them separate). Only the last re.sub will take effect in the
> example below I need all 3 to take effect.
>
>
> import re
> o = open(r"c:\temp\outputfile.txt","w")
> data = open(r"C:\Temp\infile.txt").read()
> o.write( re.sub(".*<X> ","",data) )
> o.write( re.sub(".*<Y> ","",data) )
> o.write( re.sub(".*<Z> ","",data) )
> o.close()


Apply all substitutions to data before you write the result to the file:

with open(infile) as f:
data = f.read()

for expr in list_of_regexes:
data = re.sub(expr, "", data)

with open(outfile, "w") as f:
f.write(data)







Find and replace multiple RegEx search expressions topic




Hi,

I'm trying to delete contents of a .txt log file, matching on multiple
re.sub criteria but not sure how to achieve this.

Below is an illustration of what I am trying to achieve (of course in this
example I can combine the 3 re.sub into a single re expression but my
actual code will have a dozen plus expression I need to match on so easier
to keep them separate). Only the last re.sub will take effect in the
example below I need all 3 to take effect.


import re
o = open(r"c:\temp\outputfile.txt","w")
data = open(r"C:\Temp\infile.txt").read()
o.write( re.sub(".*<X> ","",data) )
o.write( re.sub(".*<Y> ","",data) )
o.write( re.sub(".*<Z> ","",data) )
o.close()


Thanks in advance.
Jignesh






jeudi 23 janvier 2014

Re: Regex replacement via external command topic




Rainer Weikusat <(E-Mail Removed)> writes:

[...]


> Contrived example which can actually be executed:
>
> -----------------
> undef $/;
> my $input = <STDIN>;
>
> for ($input) {
> /\G(x+)/gc and do {
> my $fh;
>
> open($fh, '| tr x y');


This is sort-of a silly example pipe because the output would be
identical when everything was just piped through tr. A slightly less
silly example would be

open($fh, '| sed "s/./y/g"');

This uses sed to replace every input byte with an y. As can easily be
verified, it only runs for x in the input, not for any other character.