awk输出格式

时间:2017-10-15 12:21:21

标签: awk gawk

我有2个.po文件,其中有一些词有两个不同的含义 并希望使用awk将其变成某种翻译器

例如

在.po文件1中

msgstr“示例”

msgstr“某事”

.po文件2中的

msgstr“示例”

msgstr“somethingelse”

我想出了这个

awk -F'"' 'match($2, /^example$/) {printf "%s", $2": ";getline; printf "%s", $2}' file1.po file2.po

输出

example:something example:somethinelse

如何将其制作成这种格式

example : something, somethingelse.

2 个答案:

答案 0 :(得分:1)

$ awk -F'"' 'NR%2{k=$2; next} NR==FNR{a[k]=$2; next} {print k" : "a[k]", "$2}' file1 file2
example : something, somethingelse

答案 1 :(得分:0)

重新格式化

example:something example:somethinelse

进入

example : something, somethingelse

可以使用单行

完成
awk -F":| " -v OFS="," '{printf "%s:", $1; for (i=1;i<=NF;i++) if (i % 2 == 0)printf("%s%s%s", ((i==2)?"":OFS), $i, ((i==NF)?"\n":""))}'

<强>测试

$ echo "example:something example:somethinelse example:something3 example:something4" | \
awk -F":| " -v OFS="," '{ \
printf "%s:", $1; \
for (i=1;i<=NF;i++) \
    if (i % 2 == 0) \
       printf("%s%s%s", ((i==2)?"":OFS), $i, ((i==NF)?"\n":""))}'
example:something,somethinelse,something3,something4

<强>解释

$ cat tst.awk
BEGIN{FS=":| ";OFS=","}      # define field sep and output field sep
{ printf "%s:", $1           # print header line "example:"
for (i=1;i<=NF;i++)          # loop over all fields
    if (i % 2 == 0)          # we're only interested in all "even" fields
        printf("%s%s%s", ((i==2)?"":OFS), $i, ((i==NF)?"\n":""))
}

但你可以用一次性完成整个事情:

$ cat tst.awk
BEGIN{OFS=","}                               # set output field sep to ","
NF{                                          # if NF (i.e. number of fields) > 0 
                                             #   - to skip empty lines -
   if (match($0,/msgid "(.*)"/,a)) id=a[1]   # if line matches 'msgid "something", 
                                             #   set "id" to "something" 
   if (match($0,/msgstr "(.*)"/,b)) str=b[1] # same here for 'msgstr'
   if (id && str){                           # if both "id" and "str" are set
       r[id]=(id in r)?r[id] OFS str:str     # save "str" in array r with index "id".
                                             # if index "id" already exists, 
                                             #   add  "str" preceded by OFS (i.e. "," here) 
       id=str=0                              # after printing, reset "id" and "str"
   }
}
END { for (i in r) printf "%s : %s\n", i, r[i] } # print array "r"

并将其称为:

awk -f tst.awk *.po