awk 输出格式化
Awk output formatting
我有 2 个 .po 文件,其中的一些词有 2 个不同的含义
想用 awk 把它变成某种翻译器
例如
在 .po 文件 1
msgid "example"
msgstr "something"
在 .po 文件 2
msgid "example"
msgstr "somethingelse"
我想出了这个
awk -F'"' 'match(, /^example$/) {printf "%s", ": ";getline; printf "%s", }' file1.po file2.po
输出将是
example:something example:somethinelse
如何做成这种格式
example : something, somethingelse.
重新格式化
example:something example:somethinelse
进入
example : something, somethingelse
可以用这个 one-liner:
awk -F":| " -v OFS="," '{printf "%s:", ; 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:", ; \
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:", # 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([=15=],/msgid "(.*)"/,a)) id=a[1] # if line matches 'msgid "something",
# set "id" to "something"
if (match([=15=],/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
$ awk -F'"' 'NR%2{k=; next} NR==FNR{a[k]=; next} {print k" : "a[k]", "}' file1 file2
example : something, somethingelse
我有 2 个 .po 文件,其中的一些词有 2 个不同的含义 想用 awk 把它变成某种翻译器
例如
在 .po 文件 1
msgid "example"
msgstr "something"
在 .po 文件 2
msgid "example"
msgstr "somethingelse"
我想出了这个
awk -F'"' 'match(, /^example$/) {printf "%s", ": ";getline; printf "%s", }' file1.po file2.po
输出将是
example:something example:somethinelse
如何做成这种格式
example : something, somethingelse.
重新格式化
example:something example:somethinelse
进入
example : something, somethingelse
可以用这个 one-liner:
awk -F":| " -v OFS="," '{printf "%s:", ; 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:", ; \
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:", # 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([=15=],/msgid "(.*)"/,a)) id=a[1] # if line matches 'msgid "something",
# set "id" to "something"
if (match([=15=],/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
$ awk -F'"' 'NR%2{k=; next} NR==FNR{a[k]=; next} {print k" : "a[k]", "}' file1 file2
example : something, somethingelse