Python: Suppress lines not substituted


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python: Suppress lines not substituted
# 1  
Old 09-01-2016
Python: Suppress lines not substituted

Hi all,

I started playing around with Python (2.6.6) trying to parse a file. No matter what I tried so far I am not able to print only the lines/group/backreference that have been affected by the substitution. Instead I get also the other lines too

File to parse:
Code:
        [serverClass:One] some more text
                config = alpha.cfg

        [serverClass:Two] some more text
                config = bravo.cfg

        [serverClass:Three] some more text
                config = charly.cfg

Current code:
Code:
import re

f = open('infile', 'r')

lines = f.readlines()

for line in lines:
        x = re.sub(r"^\s*\[serverClass:([^]]*)\] .*$", r"\1", line.rstrip())
        print(x)

I assume there is something needed like the behavior of sed's -n and /p to basically print nothing but the lines that have been matched.

Output:
Code:
$ python ./mach.py
One
                config = alpha.cfg

Two
                config = bravo.cfg

Three
                config = charly.cfg

Expected output:
Code:
One
Two
Three

I saw that there are more functions/methods in the module re like re.group etc. but I would like to know why it doesn't work with my example.

Thanks in forward for a hint with this rather basic problem.

Cheers
zaxxon
# 2  
Old 09-01-2016
we can skip the line using match ?

Code:
import re

f = open('infile', 'r')

lines = f.readlines()

for line in lines:
        if re.match('^.*:(.*)].*$',line.rstrip()):
                x = re.sub(r'^.*:(.*)].*$', r'\1', line.rstrip())
                print x

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 09-01-2016
Thank you, itkamaraj. I had it tried so far with match() and search() and it didn't work.
Now I just noticed why - I used:

Code:
re.match('<partial-pattern>', ...

...where I assumed I can use just a part of a pattern like when you use Linux/Unix grep; this seems not working so I have to use at least something like:

Code:
re.match('.*<partial-pattern>.*', ...


cheers
zaxxon

Last edited by zaxxon; 09-01-2016 at 09:24 AM.. Reason: clarification, typos
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies

2. Shell Programming and Scripting

2 lines of python

Hi, Please can you tell me what the last two lines in the python code below are doing? data = open('input.txt', 'r').read() # should be simple plain text file chars = list(set(data)) data_size, vocab_size = len(data), len(chars) print 'data has %d characters, %d unique.' % (data_size,... (2 Replies)
Discussion started by: 1Brajesh
2 Replies

3. UNIX for Dummies Questions & Answers

Tool to suppress lines where value=0

Hello everyone, Here's the problem, I have a list generated by this command; watch -n 5 'iptables -nvL | grep "DROP"' It generates output that looks like this (severely truncated for clarity) Every 5.0s: iptables -nvL | grep "DROP" ... (2 Replies)
Discussion started by: putter1900
2 Replies

4. Programming

Way to print a code with substituted macro..?

Is there any way to produce a code with all use dmacro to be substituted up to 'ready for compilation' condition? Some macro are build up and it is hard to replace all them up to final code by hand. I need to see the final line after all macro been applied by preprocessor. How that could be... (5 Replies)
Discussion started by: alex_5161
5 Replies

5. Shell Programming and Scripting

substituted variable assignment

I try to run this script, however, it gives an exception in line 3. How do I do an assignment to a substituted variable? #!/bin/bash name=fruit ext_$(eval echo ${name})=apple tmp=ext_$(eval echo ${name}) if ]; then echo "apple" elif ]; then echo "orange" fi echo ${!tmp} Error... (2 Replies)
Discussion started by: angelokh
2 Replies

6. Shell Programming and Scripting

how to suppress dd output?

I have to stop the output of dd from writing to terminal. Here is the command: sudo dd if=boot1h of="/dev/r$temp1" Here is the output: 2+0 records in 2+0 records out 1024 bytes transferred in 0.000804 secs (1273715 bytes/sec) I have tried >> log.txt but it doesn't work. Is there... (4 Replies)
Discussion started by: msf5042
4 Replies

7. UNIX for Dummies Questions & Answers

Only one particular occurence needs to be substituted !!!

In a file, field separetor in line is irregular number of spaces, so I canot use field in sub function to get my charecter replaced with empty space. I would like to substitute only one perticular charecter with space at perticular posiotn, so I canot use perticular character as that may occur... (6 Replies)
Discussion started by: vaka
6 Replies

8. Programming

Suppress last N lines printing

Hi, I want to know different ways of suppressing printing of last N lines. Can anyone help? Thanks, Sree (1 Reply)
Discussion started by: chakri400
1 Replies
Login or Register to Ask a Question