gawk / sed的嵌套分隔符问题

时间:2011-05-13 08:57:36

标签: sed awk gawk

我有这篇文章需要拆分:

[{names: {en: 'UK 100', es: 'UK 100'}, status: 'A', displayed: 'Y', start_time: '2011-05-12 00:00:00', start_time_xls: {en: '12th of May 2011  00:00 am', es: '12 May 2011 00:00 am'}, suspend_at: '2011-05-12 15:14:02', is_off: 'Y', score_home: '', score_away: '', bids_status: '', period_id: '', curr_period_start_time: '', score_extra_info: '', settled: 'N', ev_id: 2666872, ev_type_id: 10744, type_name: '|UK 100|'}, {names: {en: 'US 30', es: 'US 30'}, status: 'A', displayed: 'Y', start_time: '2011-05-12 00:00:00', start_time_xls: {en: '12th of May 2011  00:00 am', es: '12 May 2011 00:00 am'}, suspend_at: '2011-05-12 15:13:45', is_off: 'Y', score_home: '', score_away: '', bids_status: '', period_id: '', curr_period_start_time: '', score_extra_info: '', settled: 'N', ev_id: 2666879, ev_type_id: 10745, type_name: '|US 30|'}, {names: {en: 'Germany 30', es: 'Germany 30'}, status: 'A', displayed: 'Y', start_time: '2011-05-12 00:00:00', start_time_xls: {en: '12th of May 2011  00:00 am', es: '12 May 2011 00:00 am'}, suspend_at: '2011-05-12 15:13:52', is_off: 'Y', score_home: '', score_away: '', bids_status: '', period_id: '', curr_period_start_time: '', score_extra_info: '', settled: 'N', ev_id: 2666884, ev_type_id: 10748, type_name: '|Germany 30|'}, {names: {en: 'France 40', es: 'France 40'}, status: 'A', displayed: 'Y', start_time: '2011-05-12 00:00:00', start_time_xls: {en: '12th of May 2011  00:00 am', es: '12 May 2011 00:00 am'}, suspend_at: '2011-05-12 15:13:38', is_off: 'Y', score_home: '', score_away: '', bids_status: '', period_id: '', curr_period_start_time: '', score_extra_info: '', settled: 'N', ev_id: 2666882, ev_type_id: 10747, type_name: '|France 40|'}, {names: {en: 'US 500', es: 'US 500'}, status: 'A', displayed: 'Y', start_time: '2011-05-12 00:00:00', start_time_xls: {en: '12th of May 2011  00:00 am', es: '12 May 2011 00:00 am'}, suspend_at: '2011-05-12 15:14:30', is_off: 'Y', score_home: '', score_away: '', bids_status: '', period_id: '', curr_period_start_time: '', score_extra_info: '', settled: 'N', ev_id: 2666890, ev_type_id: 10749, type_name: '|US 500|'}, {names: {en: 'Spain 35', es: 'Spain 35'}, status: 'A', displayed: 'Y', start_time: '2011-05-12 00:00:00', start_time_xls: {en: '12th of May 2011  00:00 am', es: '12 May 2011 00:00 am'}, suspend_at: '2011-05-12 15:13:51', is_off: 'Y', score_home: '', score_away: '', bids_status: '', period_id: '', curr_period_start_time: '', score_extra_info: '', settled: 'N', ev_id: 2666886, ev_type_id: 10750, type_name: '|Spain 35|'}],

我已尝试过这些变种,但不断被“内部”分隔符抓住,我不想分开!!:

gawk -F“[” - v RS =“,”“NF {print $ 0}”text.txt

如何拆分它们(1)首先在主“{”上,忽略内部“{”(2)然后在逗号上,忽略花括号之间的逗号。然后我想输出一个或两个字段,如下所示:

suspend_at:'2011-05-12 15:14:02',ev_id:2666872,ev_type_id:10744,type_name:'| UK 100 |'

提前致谢。

1 个答案:

答案 0 :(得分:1)

如前所述,如果Perl可以接受:

% perl -MText::ParseWords -nle'
   /suspend|ev_(id|type)|type_name/ and print for parse_line("[{},]",0, $_);
  ' infile
 suspend_at: 2011-05-12 15:14:02
 ev_id: 2666872
 ev_type_id: 10744
 type_name: |UK 100|
 suspend_at: 2011-05-12 15:13:45
 ev_id: 2666879
 ev_type_id: 10745
 type_name: |US 30|
 suspend_at: 2011-05-12 15:13:52
 ev_id: 2666884
 ev_type_id: 10748
 type_name: |Germany 30|
 suspend_at: 2011-05-12 15:13:38
 ev_id: 2666882
 ev_type_id: 10747
 type_name: |France 40|
 suspend_at: 2011-05-12 15:14:30
 ev_id: 2666890
 ev_type_id: 10749
 type_name: |US 500|
 suspend_at: 2011-05-12 15:13:51
 ev_id: 2666886
 ev_type_id: 10750
 type_name: |Spain 35|