如何用awk

时间:2017-10-13 17:07:50

标签: bash awk replace str-replace gawk

我的文件awk-test中的字段用括号括起来,并用分号分隔:

"col1";"col2";"col3";"col4";"col5";
"eiusmod";"tempor";"incididunt";"ut";"labore";
"et";"dolore";"magna";"aliqua";"Ut";
"enim";"ad";"minim";"veniam";"quis";
"ut";"aliquip";"ex";"ea";"commodo";
"nostrud";"exercitation";"ullamco";"laboris";"nisi";

真实数据(标题行加三条记录):

"col1";"col2";"col3";"col4";"col5";
"/absence/lang/#LANG_ID#/.descr.php";"BP2_DESCR";"Dodaj";"Add";"Adicionar";
"/cal/lang/#LANG_ID#/cal_feed.php";"LF_COMM_MSG";"je komentiral ""#EVENT_TITLE#""";"commented on an event ""#EVENT_TITLE#""";"comentado sobre o evento ""#EVENT_TITLE#""";
"/mod/lang/#LANG_ID#/set_events.php";"IM_NOTIFY";"Pozdravljeni #USER_NAME#!

#FROM_USER# vam je poslal(a) sporocilo.

------------------------------------------

#FROM_USER#: #MESSAGE#

------------------------------------------;"Hello #USER_NAME#!

You have a new notification from #FROM_USER#

------------------------------------------

#MESSAGE#

------------------------------------------;"Olá #USER_NAME#!

Você tem uma nova notificação de #FROM_USER# 

------------------------------------------

 #MESSAGE# 

------------------------------------------;

如果第3列的字符为“m”,我知道如何打印第3列和第4列的前30行:

gawk 'BEGIN {FS = ";" } ; $3 ~/m/ {print $3 ";" $4} NR==30{exit}' OFS=';' awk-test 

结果是:

"magna";"aliqua"
"minim";"veniam"
"ullamco";"laboris"

但我不知道 (a)如何在测试30线样品上用“x”代替“m” (b)如何在真正的250.000行文件中将“m”替换为“x”。

test-awk上的所需输出:

"xagna";"aliqua"
"xinim";"veniam"
"ullaxco";"laboris"

实际上我只需修复第3列中字符的错误。因此,我想知道如何编写更改的行并将未更改的行保存到包含固定列3的新文件中?

提前谢谢!

1 个答案:

答案 0 :(得分:0)

awk解决方案:

$ cat tst.awk
BEGIN{FS=OFS=";"}
NR>1 && sub(/m/,"x",$3){print $3, $4}

这适用于您的真实 250.000行文件:

$ awk -f tst.awk file
"xagna";"aliqua"
"xinim";"veniam"
"ullaxco";"laboris

或者,使用单行:

awk 'BEGIN{FS=OFS=";"} NR>1 && sub(/m/,"x",$3){print $3, $4}' file