在awk中使用不同的行分隔符

时间:2015-11-09 05:20:32

标签: awk

我有一个文件如下:

cat file

00:29:01|10.3.57.60|dbname1| SELECT                                   
    re.id,                                
    re.event_type_cd,                     
    re.event_ts,                          
    re.source_type,                       
    re.source_id,                         
    re.properties                         
 FROM                                     
    table1 re         
 WHERE                                    
    re.id > 621982999
    AND re.id <= 884892348
 ORDER BY                                 
    re.id                                 
^
00:01:00|10.3.56.101|dbname2|BEGIN;declare "SQL_CUR00000000009CE140" cursor for SELECT  id, cast(event_type_cd as character(4)) event_type_cd, CAST(event_ts AS DATE) event_ts, CAST(source_id AS character varying(100)) source_id, CAST(tx_id AS character varying(100)) tx_id, CAST(properties AS character varying(4000)) properties, CAST(source_type AS character(1)) source_type FROM table1 WHERE  ID > 514725989 ORDER BY ID  limit 500000;fetch 500000 in "SQL_CUR00000000009CE140"^

这些是由pipe(|)分隔的sql结果的输出。为了识别新行,我在每行的末尾使用了^

我希望输出为:

1/00:29:01|10.3.57.60|parasol_ams| SELECT                                   
    re.id,                                
    re.event_type_cd,                     
    re.event_ts,                          
    re.source_type,                       
    re.source_id,                         
    re.properties                         
 FROM                                     
    table1 re         
 WHERE                                    
    re.id > 621982999
    AND re.id <= 884892348
 ORDER BY                                 
    re.id                                 
2/00:01:00|10.3.56.101|parasol_sprint_tep|BEGIN;declare "SQL_CUR00000000009CE140" cursor for SELECT   id, cast(event_type_cd as character(4)) event_type_cd, CAST(event_ts AS DATE) event_ts, CAST(source_id AS character varying(100)) source_id, CAST(tx_id AS character varying(100)) tx_id, CAST(properties AS character varying(4000)) properties, CAST(source_type AS character(1)) source_type FROM table1 WHERE  ID > 514725989 ORDER BY ID  limit 500000;fetch 500000 in "SQL_CUR00000000009CE140"

但是当我使用时:

cat file  | awk -F '|'  -v RS="^"  '{ print FNR "/" $0 }' 

我明白了:

1/00:29:01|10.3.57.60|parasol_ams| SELECT                                   
    re.id,                                
    re.event_type_cd,                     
    re.event_ts,                          
    re.source_type,                       
    re.source_id,                         
    re.properties                         
 FROM                                     
    table1 re         
 WHERE                                    
    re.id > 621982999
    AND re.id <= 884892348
 ORDER BY                                 
    re.id                                 

2/
00:01:00|10.3.56.101|parasol_sprint_tep|BEGIN;declare "SQL_CUR00000000009CE140" cursor for SELECT   id, cast(event_type_cd as character(4)) event_type_cd, CAST(event_ts AS DATE) event_ts, CAST(source_id AS character varying(100)) source_id, CAST(tx_id AS character varying(100)) tx_id, CAST(properties AS character varying(4000)) properties, CAST(source_type AS character(1)) source_type FROM table1 WHERE  ID > 514725989 ORDER BY ID  limit 500000;fetch 500000 in "SQL_CUR00000000009CE140"
3/

1 个答案:

答案 0 :(得分:0)

 awk '/^\^/{next}/\|/{sub("^",++c"/")}1' file



awk -vRS='^' -F '|' '{sub("^\n","")}{printf "%s/TIME:%s HOST:%s DB:%s SQL:%s",FNR,$1,$2,$3,$4}' file