PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > extract lines from file
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

extract lines from file

Réponse
 
LinkBack Outils de la discussion
Vieux 22/05/2007, 19h25   #1
sonal10july@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut extract lines from file

Hi Friends,

I want all those lines lines from file starting with keyword 'INSERT'
and end with ')'
Note: In following example there are two ')' (one after 'INSERT' and
another after 'VALUES') . I want all those lines between INSERT and
second occurence of ')'
############
Input File
################################################## #########################
INSERT common..dividend
( dividend_id,tid,declared_date,ex_date,record_date, pay_date,amount,interim_final_flag,

fx_rate,stock_option_ratio,stock_price,treaty_tax_ rate,non_treaty_tax_rate,currency_code,

create_date,update_date,create_user_id,update_user _id,cam_id,orig_cam_id
)
VALUES (306181, 51758, '2007-05-11', '2007-05-21',
'2007-05-21', '2007-05-24', 0.19,
'I', 0.7426385949277784, null, 4.818, null,
null, 'EUR', '2007-05-11 03:04', '2007-05-11 03:04',
105,
105, 19874739, null
)

May 21, 2007 6:17:11 PM com.esdi.dividend.DividendAccrual
accrueDividends
INFO: START of accrual process for tid: 51745
May 21, 2007 6:17:11 PM com.esdi.dividend.DividendAccrual
accrueDividends
INFO: Instrument tid is 51745

INSERT common..dividend (

dividend_id,tid,declared_date,ex_date,record_date, pay_date,amount,interim_final_flag,

fx_rate,stock_option_ratio,stock_price,treaty_tax_ rate,non_treaty_tax_rate,currency_code,

create_date,update_date,create_user_id,update_user _id,cam_id,orig_cam_id
)
VALUES (306177, 51745, '2007-05-11', '2007-05-21',
'2007-05-21', '2007-05-24', 0.015,
'I', 0.7426385949277784, null, 1.381, null,
null, 'EUR', '2007-05-11 03:04', '2007-05-11 03:04',
105,
105, 19874793, null
)

################################################## #########################

Output should be like following
======================
INSERT common..dividend
( dividend_id,tid,declared_date,ex_date,record_date, pay_date,amount,interim_final_flag,

fx_rate,stock_option_ratio,stock_price,treaty_tax_ rate,non_treaty_tax_rate,currency_code,

create_date,update_date,create_user_id,update_user _id,cam_id,orig_cam_id
)
VALUES (306181, 51758, '2007-05-11', '2007-05-21',
'2007-05-21', '2007-05-24', 0.19,
'I', 0.7426385949277784, null, 4.818, null,
null, 'EUR', '2007-05-11 03:04', '2007-05-11 03:04',
105,
105, 19874739, null
)
INSERT common..dividend
( dividend_id,tid,declared_date,ex_date,record_date, pay_date,amount,interim_final_flag,

fx_rate,stock_option_ratio,stock_price,treaty_tax_ rate,non_treaty_tax_rate,currency_code,

create_date,update_date,create_user_id,update_user _id,cam_id,orig_cam_id
)
VALUES (306181, 51758, '2007-05-11', '2007-05-21',
'2007-05-21', '2007-05-24', 0.19,
'I', 0.7426385949277784, null, 4.818, null,
null, 'EUR', '2007-05-11 03:04', '2007-05-11 03:04',
105,
105, 19874739, null
)


Thank you all in advance for your .

Regards
SK

  Réponse avec citation
Vieux 22/05/2007, 19h55   #2
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: extract lines from file

sonal10july@gmail.com wrote:
> Hi Friends,
>
> I want all those lines lines from file starting with keyword 'INSERT'
> and end with ')'
> Note: In following example there are two ')' (one after 'INSERT' and
> another after 'VALUES') . I want all those lines between INSERT and
> second occurence of ')'


It's really hard to decipher your data [ - I hope that someday people
would take more care about how they present their data structures - ].

But you may try whether the following code suffices for your request...

awk '/INSERT/,/)/;/VALUES/,/)/' < inputfile > output_file


Janis


> ############
> Input File
> ################################################## #########################
> INSERT common..dividend
> ( dividend_id,tid,declared_date,ex_date,record_date, pay_date,amount,interim_final_flag,
>
> fx_rate,stock_option_ratio,stock_price,treaty_tax_ rate,non_treaty_tax_rate,currency_code,
>
> create_date,update_date,create_user_id,update_user _id,cam_id,orig_cam_id
> )
> VALUES (306181, 51758, '2007-05-11', '2007-05-21',
> '2007-05-21', '2007-05-24', 0.19,
> 'I', 0.7426385949277784, null, 4.818, null,
> null, 'EUR', '2007-05-11 03:04', '2007-05-11 03:04',
> 105,
> 105, 19874739, null
> )
>
> May 21, 2007 6:17:11 PM com.esdi.dividend.DividendAccrual
> accrueDividends
> INFO: START of accrual process for tid: 51745
> May 21, 2007 6:17:11 PM com.esdi.dividend.DividendAccrual
> accrueDividends
> INFO: Instrument tid is 51745
>
> INSERT common..dividend (
>
> dividend_id,tid,declared_date,ex_date,record_date, pay_date,amount,interim_final_flag,
>
> fx_rate,stock_option_ratio,stock_price,treaty_tax_ rate,non_treaty_tax_rate,currency_code,
>
> create_date,update_date,create_user_id,update_user _id,cam_id,orig_cam_id
> )
> VALUES (306177, 51745, '2007-05-11', '2007-05-21',
> '2007-05-21', '2007-05-24', 0.015,
> 'I', 0.7426385949277784, null, 1.381, null,
> null, 'EUR', '2007-05-11 03:04', '2007-05-11 03:04',
> 105,
> 105, 19874793, null
> )
>
> ################################################## #########################
>
> Output should be like following
> ======================
> INSERT common..dividend
> ( dividend_id,tid,declared_date,ex_date,record_date, pay_date,amount,interim_final_flag,
>
> fx_rate,stock_option_ratio,stock_price,treaty_tax_ rate,non_treaty_tax_rate,currency_code,
>
> create_date,update_date,create_user_id,update_user _id,cam_id,orig_cam_id
> )
> VALUES (306181, 51758, '2007-05-11', '2007-05-21',
> '2007-05-21', '2007-05-24', 0.19,
> 'I', 0.7426385949277784, null, 4.818, null,
> null, 'EUR', '2007-05-11 03:04', '2007-05-11 03:04',
> 105,
> 105, 19874739, null
> )
> INSERT common..dividend
> ( dividend_id,tid,declared_date,ex_date,record_date, pay_date,amount,interim_final_flag,
>
> fx_rate,stock_option_ratio,stock_price,treaty_tax_ rate,non_treaty_tax_rate,currency_code,
>
> create_date,update_date,create_user_id,update_user _id,cam_id,orig_cam_id
> )
> VALUES (306181, 51758, '2007-05-11', '2007-05-21',
> '2007-05-21', '2007-05-24', 0.19,
> 'I', 0.7426385949277784, null, 4.818, null,
> null, 'EUR', '2007-05-11 03:04', '2007-05-11 03:04',
> 105,
> 105, 19874739, null
> )
>
>
> Thank you all in advance for your .
>
> Regards
> SK
>

  Réponse avec citation
Vieux 22/05/2007, 19h57   #3
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: extract lines from file

sonal10july@gmail.com wrote:
> Hi Friends,
>
> I want all those lines lines from file starting with keyword 'INSERT'
> and end with ')'
> Note: In following example there are two ')' (one after 'INSERT' and
> another after 'VALUES') . I want all those lines between INSERT and
> second occurence of ')'
> ############
> Input File
> ################################################## #########################
> INSERT common..dividend
> ( dividend_id,tid,declared_date,ex_date,record_date, pay_date,amount,interim_final_flag,
>
> fx_rate,stock_option_ratio,stock_price,treaty_tax_ rate,non_treaty_tax_rate,currency_code,
>
> create_date,update_date,create_user_id,update_user _id,cam_id,orig_cam_id
> )
> VALUES (306181, 51758, '2007-05-11', '2007-05-21',
> '2007-05-21', '2007-05-24', 0.19,
> 'I', 0.7426385949277784, null, 4.818, null,
> null, 'EUR', '2007-05-11 03:04', '2007-05-11 03:04',
> 105,
> 105, 19874739, null
> )
>
> May 21, 2007 6:17:11 PM com.esdi.dividend.DividendAccrual
> accrueDividends
> INFO: START of accrual process for tid: 51745
> May 21, 2007 6:17:11 PM com.esdi.dividend.DividendAccrual
> accrueDividends
> INFO: Instrument tid is 51745
>
> INSERT common..dividend (
>
> dividend_id,tid,declared_date,ex_date,record_date, pay_date,amount,interim_final_flag,
>
> fx_rate,stock_option_ratio,stock_price,treaty_tax_ rate,non_treaty_tax_rate,currency_code,
>
> create_date,update_date,create_user_id,update_user _id,cam_id,orig_cam_id
> )
> VALUES (306177, 51745, '2007-05-11', '2007-05-21',
> '2007-05-21', '2007-05-24', 0.015,
> 'I', 0.7426385949277784, null, 1.381, null,
> null, 'EUR', '2007-05-11 03:04', '2007-05-11 03:04',
> 105,
> 105, 19874793, null
> )
>
> ################################################## #########################
>
> Output should be like following
> ======================
> INSERT common..dividend
> ( dividend_id,tid,declared_date,ex_date,record_date, pay_date,amount,interim_final_flag,
>
> fx_rate,stock_option_ratio,stock_price,treaty_tax_ rate,non_treaty_tax_rate,currency_code,
>
> create_date,update_date,create_user_id,update_user _id,cam_id,orig_cam_id
> )
> VALUES (306181, 51758, '2007-05-11', '2007-05-21',
> '2007-05-21', '2007-05-24', 0.19,
> 'I', 0.7426385949277784, null, 4.818, null,
> null, 'EUR', '2007-05-11 03:04', '2007-05-11 03:04',
> 105,
> 105, 19874739, null
> )
> INSERT common..dividend
> ( dividend_id,tid,declared_date,ex_date,record_date, pay_date,amount,interim_final_flag,
>
> fx_rate,stock_option_ratio,stock_price,treaty_tax_ rate,non_treaty_tax_rate,currency_code,
>
> create_date,update_date,create_user_id,update_user _id,cam_id,orig_cam_id
> )
> VALUES (306181, 51758, '2007-05-11', '2007-05-21',
> '2007-05-21', '2007-05-24', 0.19,
> 'I', 0.7426385949277784, null, 4.818, null,
> null, 'EUR', '2007-05-11 03:04', '2007-05-11 03:04',
> 105,
> 105, 19874739, null
> )
>
>
> Thank you all in advance for your .
>
> Regards
> SK
>



sed -n "/[^']*INSERT /,/[^']*)/p;/[^']*VALUES /,/[^']*)/p;" file



--
Michael Tosch @ hp : com
  Réponse avec citation
Vieux 22/05/2007, 20h05   #4
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: extract lines from file

Janis Papanagnou wrote:
> sonal10july@gmail.com wrote:
>> Hi Friends,
>>
>> I want all those lines lines from file starting with keyword 'INSERT'
>> and end with ')'
>> Note: In following example there are two ')' (one after 'INSERT' and
>> another after 'VALUES') . I want all those lines between INSERT and
>> second occurence of ')'

>
> It's really hard to decipher your data [ - I hope that someday people
> would take more care about how they present their data structures - ].
>
> But you may try whether the following code suffices for your request...
>
> awk '/INSERT/,/)/;/VALUES/,/)/' < inputfile > output_file
>
>


It is safer to ensure that INSERT and VALUE do not appear inside a 'string':

and an ERE needs \) to match a )

awk "/[^']*INSERT/,/[^']*\)/;/[^']*VALUES/,/[^']*\)/"


--
Michael Tosch @ hp : com
  Réponse avec citation
Vieux 22/05/2007, 20h07   #5
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: extract lines from file

2007-05-22, 21:05(+02), Michael Tosch:
[...]
>> awk '/INSERT/,/)/;/VALUES/,/)/' < inputfile > output_file
>>
>>

>
> It is safer to ensure that INSERT and VALUE do not appear inside a 'string':
>
> and an ERE needs \) to match a )
>
> awk "/[^']*INSERT/,/[^']*\)/;/[^']*VALUES/,/[^']*\)/"

[...]

/[^']*INSERT/ is equivalent to /INSERT/
given that [^']* matches the empty string.

Maybe you meant /^[^']*INSERT/
which may miss a "INSERT" that is not inside '...' as in
'...' INSERT

--
Stéphane
  Réponse avec citation
Vieux 22/05/2007, 20h16   #6
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: extract lines from file

Michael Tosch wrote:
> Janis Papanagnou wrote:
>
>> sonal10july@gmail.com wrote:
>>
>>> Hi Friends,
>>>
>>> I want all those lines lines from file starting with keyword 'INSERT'
>>> and end with ')'
>>> Note: In following example there are two ')' (one after 'INSERT' and
>>> another after 'VALUES') . I want all those lines between INSERT and
>>> second occurence of ')'

>>
>>
>> It's really hard to decipher your data [ - I hope that someday people
>> would take more care about how they present their data structures - ].
>>
>> But you may try whether the following code suffices for your request...
>>
>> awk '/INSERT/,/)/;/VALUES/,/)/' < inputfile > output_file
>>
>>

>
> It is safer to ensure that INSERT and VALUE do not appear inside a
> 'string':
>
> and an ERE needs \) to match a )


Not in awk. (You haven't tried?)

Janis

>
> awk "/[^']*INSERT/,/[^']*\)/;/[^']*VALUES/,/[^']*\)/"
>
>

  Réponse avec citation
Vieux 22/05/2007, 20h28   #7
G-Man
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: extract lines from file

sonal10july@gmail.com wrote:
> Hi Friends,
>
> I want all those lines lines from file starting with keyword 'INSERT'
> and end with ')'
> Note: In following example there are two ')' (one after 'INSERT' and
> another after 'VALUES') . I want all those lines between INSERT and
> second occurence of ')'
> ############
> Input File
> ################################################## #########################
> INSERT common..dividend
> ( dividend_id,tid,declared_date,ex_date,record_date, pay_date,amount,interim_final_flag,
>
> fx_rate,stock_option_ratio,stock_price,treaty_tax_ rate,non_treaty_tax_rate,currency_code,
>
> create_date,update_date,create_user_id,update_user _id,cam_id,orig_cam_id
> )
> VALUES (306181, 51758, '2007-05-11', '2007-05-21',
> '2007-05-21', '2007-05-24', 0.19,
> 'I', 0.7426385949277784, null, 4.818, null,
> null, 'EUR', '2007-05-11 03:04', '2007-05-11 03:04',
> 105,
> 105, 19874739, null
> )
>
> May 21, 2007 6:17:11 PM com.esdi.dividend.DividendAccrual
> accrueDividends
> INFO: START of accrual process for tid: 51745
> May 21, 2007 6:17:11 PM com.esdi.dividend.DividendAccrual
> accrueDividends
> INFO: Instrument tid is 51745
>
> INSERT common..dividend (
>
> dividend_id,tid,declared_date,ex_date,record_date, pay_date,amount,interim_final_flag,
>
> fx_rate,stock_option_ratio,stock_price,treaty_tax_ rate,non_treaty_tax_rate,currency_code,
>
> create_date,update_date,create_user_id,update_user _id,cam_id,orig_cam_id
> )
> VALUES (306177, 51745, '2007-05-11', '2007-05-21',
> '2007-05-21', '2007-05-24', 0.015,
> 'I', 0.7426385949277784, null, 1.381, null,
> null, 'EUR', '2007-05-11 03:04', '2007-05-11 03:04',
> 105,
> 105, 19874793, null
> )
>
> ################################################## #########################
>
> Output should be like following
> ======================
> INSERT common..dividend
> ( dividend_id,tid,declared_date,ex_date,record_date, pay_date,amount,interim_final_flag,
>
> fx_rate,stock_option_ratio,stock_price,treaty_tax_ rate,non_treaty_tax_rate,currency_code,
>
> create_date,update_date,create_user_id,update_user _id,cam_id,orig_cam_id
> )
> VALUES (306181, 51758, '2007-05-11', '2007-05-21',
> '2007-05-21', '2007-05-24', 0.19,
> 'I', 0.7426385949277784, null, 4.818, null,
> null, 'EUR', '2007-05-11 03:04', '2007-05-11 03:04',
> 105,
> 105, 19874739, null
> )
> INSERT common..dividend
> ( dividend_id,tid,declared_date,ex_date,record_date, pay_date,amount,interim_final_flag,
>
> fx_rate,stock_option_ratio,stock_price,treaty_tax_ rate,non_treaty_tax_rate,currency_code,
>
> create_date,update_date,create_user_id,update_user _id,cam_id,orig_cam_id
> )
> VALUES (306181, 51758, '2007-05-11', '2007-05-21',
> '2007-05-21', '2007-05-24', 0.19,
> 'I', 0.7426385949277784, null, 4.818, null,
> null, 'EUR', '2007-05-11 03:04', '2007-05-11 03:04',
> 105,
> 105, 19874739, null
> )
>
>
> Thank you all in advance for your .
>
> Regards
> SK
>


Presumably something along the lines of:

gawk '{if($0~"INSERT"){t=0rint $0
while(t<2){getlinerint $0;if($0~"\)"){t+=1}}}
}' file


Regards,
Geoff
  Réponse avec citation
Vieux 22/05/2007, 20h44   #8
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: extract lines from file

2007-05-22, 21:16(+02), Janis Papanagnou:
[...]
>> and an ERE needs \) to match a )

>
> Not in awk. (You haven't tried?)

[...]

$ awk '/)/'
awk: line 1: regular expression compile failed (missing '(')

Don't assume what is true in gawk is true in every awk
implementation.

POSIX requires the ")" do be escaped here, though it doesn't
require an awk implementation to treat un unmatched ")" as an
error.

--
Stéphane
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h10.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,22446 seconds with 16 queries