Parsing Text file

2013-07-02 Thread sas429s
I have a text file like this:

Sometext
Somemore
Somemore
maskit

Sometext
Somemore
Somemore
Somemore
maskit

Sometext
Somemore
maskit

I want to search for the string maskit in this file and also need to print 
Sometext above it..SOmetext location can vary as you can see above.

In the first instance it is 3 lines above mask it, in the second instance it is 
4 lines above it and so on..

Please help how to do it?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing Text file

2013-07-02 Thread sas429s
Somemore can be anything for instance:

Sometext
mail
maskit

Sometext
rupee
dollar
maskit

and so on..

Is there a way I can achieve this?

On Tuesday, July 2, 2013 2:24:26 PM UTC-5, Neil Cerutti wrote:
> On 2013-07-02, sas4...@gmail.com  wrote:
> 
> > I have a text file like this:
> 
> >
> 
> > Sometext
> 
> > Somemore
> 
> > Somemore
> 
> > maskit
> 
> >
> 
> > Sometext
> 
> > Somemore
> 
> > Somemore
> 
> > Somemore
> 
> > maskit
> 
> >
> 
> > Sometext
> 
> > Somemore
> 
> > maskit
> 
> >
> 
> > I want to search for the string maskit in this file and also
> 
> > need to print Sometext above it..SOmetext location can vary as
> 
> > you can see above.
> 
> >
> 
> > In the first instance it is 3 lines above mask it, in the
> 
> > second instance it is 4 lines above it and so on..
> 
> >
> 
> > Please help how to do it?
> 
> 
> 
> How can you tell the difference between Sometext and Somemore?
> 
> 
> 
> -- 
> 
> Neil Cerutti

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing Text file

2013-07-02 Thread sas429s
Ok here is a snippet of the text file I have:

config/meal/governor_mode_config.h
  #define GOVERNOR_MODE_TASK_RATE SSS_TID_0015MSEC
  #define GOVERNOR_MODE_WORK_MODE_MASK(CEAL_MODE_WORK_MASK_GEAR| \
   CEAL_MODE_WORK_MASK_PARK_BRAKE | \
   CEAL_MODE_WORK_MASK_VEHICLE_SPEED)
  #define GOVERNOR_MODE_IDLE_CHECKFALSE
  #define GOVERNOR_MODE_SPD_THRES 50
  #define GOVERNOR_MODE_SPDDES_THRES  10

config/meal/components/source/kso_aic_core_config.h
  #define CEAL_KSO_AIC_CORE_TASK_RATE  SSS_TID_0120MSEC
  #define CEAL_KSO_AIC_LOAD_FAC_AVG_TIME   300
  #define CEAL_KSO_AIC_LOAD_FAC_HYST_TIME  30
  #define CEAL_KSO_AIC_TEMP_DPF_INSTALLED  TRUE
  #define CEAL_KSO_AIC_TEMP_DPF_ENABLE 450
  #define CEAL_KSO_AIC_TEMP_DPF_HYST   25
  #define CEAL_KSO_AIC_DPF_ROC_TIME10
  #define CEAL_KSO_AIC_TEMP_EXHAUST_INSTALLED  FALSE
  #define CEAL_KSO_AIC_TEMP_EXHAUST_ENABLE 275
  #define CEAL_KSO_AIC_TEMP_EXHAUST_HYST   25
  #define CEAL_KSO_AIC_EXHAUST_ROC_TIME10
  #define CEAL_KSO_AIC_WORK_MODE_MASK   (CEAL_MODE_WORK_MASK_GEAR   | \
   CEAL_MODE_WORK_MASK_PARK_BRAKE | \
   CEAL_MODE_WORK_MASK_VEHICLE_SPEED)
  #define CEAL_KSO_AIC_OV_TIME 15

Here I am looking for the line that contains: "WORK_MODE_MASK", I want to print 
that line as well as the file name above it: config/meal/governor_mode_config.h
or config/meal/components/source/ceal_PackD_kso_aic_core_config.h.

SO the output should be something like this:
config/meal/governor_mode_config.h

#define GOVERNOR_MODE_WORK_MODE_MASK(CEAL_MODE_WORK_MASK_GEAR| \
   CEAL_MODE_WORK_MASK_PARK_BRAKE | \
   CEAL_MODE_WORK_MASK_VEHICLE_SPEED)

config/meal/components/source/kso_aic_core_config.h
#define CEAL_KSO_AIC_WORK_MODE_MASK   (CEAL_MODE_WORK_MASK_GEAR   | \
   CEAL_MODE_WORK_MASK_PARK_BRAKE | \
   CEAL_MODE_WORK_MASK_VEHICLE_SPEED)

I hope this helps..

Thanks for your help


On Tuesday, July 2, 2013 3:12:55 PM UTC-5, Neil Cerutti wrote:
> On 2013-07-02, Tobiah  wrote:
> 
> > On 07/02/2013 12:30 PM, sas4...@gmail.com wrote:
> 
> >> Somemore can be anything for instance:
> 
> >>
> 
> >> Sometext
> 
> >> mail
> 
> >> maskit
> 
> >>
> 
> >> Sometext
> 
> >> rupee
> 
> >> dollar
> 
> >> maskit
> 
> >>
> 
> >> and so on..
> 
> >>
> 
> >> Is there a way I can achieve this?
> 
> >
> 
> > How do we know whether we have Sometext?
> 
> > If it's really just a literal 'Sometext', then
> 
> > just print that when you hit maskit.
> 
> >
> 
> > Otherwise:
> 
> >
> 
> >
> 
> > for line in open('file.txt').readlines():
> 
> > 
> 
> > if is_sometext(line):
> 
> > memory = line
> 
> >
> 
> > if line == 'maskit':
> 
> > print memory
> 
> 
> 
> Tobiah's solution fits what little we can make of your problem.
> 
> 
> 
> My feeling is that you've simplified your question a little too
> 
> much in hopes that it would help us provide a better solution.
> 
> Can you provide more context? 
> 
> 
> 
> -- 
> 
> Neil Cerutti
-- 
http://mail.python.org/mailman/listinfo/python-list