从 AWK 字段打印不同的值
Print Distinct Values from Field AWK
我正在寻找一种在命令提示符环境中使用 AWK 打印字段中不同值的方法。
ID Title Promotion_ID Flag
12 Purse 7 Y
24 Wallet 7 Y
709 iPhone 1117 Y
74 Satchel 7 Y
283 Xbox 84 N
理想情况下,我想 return promotion_ids: 7, 1117, 84。
我研究了 Google 上的问题并找到了一些示例,例如:
`cut -f 3 | uniq *filename.ext*` (returned error)
`awk cut -f 3| uniq *filename.ext*` (returned error)
`awk cut -d, -f3 *filename.ext* |sort| uniq` (returned error)
解决方案一:简单的awk
可能会有帮助。(以下将删除[=的header 25=])
awk 'FNR>1 && !a[]++{print }' Input_file
解决方案 2: 如果您需要保留 Input_file 的 Header 那么以下可能同样帮助你。
awk 'FNR==1{print;next} !a[]++{print }' Input_file
awk 'NR>1{a[]++} END{for(b in a) print b}' file
输出:
7
84
1117
与管线
$ sed 1d file | # remove header
tr -s ' ' '\t' | # normalize space delimiters to tabs
cut -f3 | # isolate the field
sort -nu # sort numerically and report unique entries
7
84
1117
[root@test ~]# cat test
ID Title Promotion_ID Flag
12 Purse 7 Y
24 Wallet 7 Y
709 iPhone 1117 Y
74 Satchel 7 Y
283 Xbox 84 N
输出-:
[root@test ~]# awk -F" " '!s[]++' test
ID Title Promotion_ID Flag
12 Purse 7 Y
709 iPhone 1117 Y
283 Xbox 84 N
[root@test ~]#
mawk '!__[$!NF=$--NF]--^(!_<NR)'
or
gawk' !__[$!--NF=$NF]--^(!_<NR)'
or perhaps
gawk '!__[$!--NF=$NF]++^(NF<NR)'
or even
mawk '!__[$!--NF=$NF]++^(NR-!_)' # mawk-only
gawk '!__[$!--NF=$--NF]--^(NR-NF)' # gawk-equiv of similar idea
7
1117
84
我正在寻找一种在命令提示符环境中使用 AWK 打印字段中不同值的方法。
ID Title Promotion_ID Flag
12 Purse 7 Y
24 Wallet 7 Y
709 iPhone 1117 Y
74 Satchel 7 Y
283 Xbox 84 N
理想情况下,我想 return promotion_ids: 7, 1117, 84。 我研究了 Google 上的问题并找到了一些示例,例如:
`cut -f 3 | uniq *filename.ext*` (returned error)
`awk cut -f 3| uniq *filename.ext*` (returned error)
`awk cut -d, -f3 *filename.ext* |sort| uniq` (returned error)
解决方案一:简单的awk
可能会有帮助。(以下将删除[=的header 25=])
awk 'FNR>1 && !a[]++{print }' Input_file
解决方案 2: 如果您需要保留 Input_file 的 Header 那么以下可能同样帮助你。
awk 'FNR==1{print;next} !a[]++{print }' Input_file
awk 'NR>1{a[]++} END{for(b in a) print b}' file
输出:
7 84 1117
与管线
$ sed 1d file | # remove header
tr -s ' ' '\t' | # normalize space delimiters to tabs
cut -f3 | # isolate the field
sort -nu # sort numerically and report unique entries
7
84
1117
[root@test ~]# cat test
ID Title Promotion_ID Flag
12 Purse 7 Y
24 Wallet 7 Y
709 iPhone 1117 Y
74 Satchel 7 Y
283 Xbox 84 N
输出-:
[root@test ~]# awk -F" " '!s[]++' test
ID Title Promotion_ID Flag
12 Purse 7 Y
709 iPhone 1117 Y
283 Xbox 84 N
[root@test ~]#
mawk '!__[$!NF=$--NF]--^(!_<NR)'
or
gawk' !__[$!--NF=$NF]--^(!_<NR)'
or perhaps
gawk '!__[$!--NF=$NF]++^(NF<NR)'
or even
mawk '!__[$!--NF=$NF]++^(NR-!_)' # mawk-only
gawk '!__[$!--NF=$--NF]--^(NR-NF)' # gawk-equiv of similar idea
7
1117
84