AWK 或 Sed 对每行内容的字段进行排序,
AWK or Sed to sort fields of the content within each line,
我需要对每行内容的字段(分隔符是逗号。)进行排序,
我不确定我该怎么做,
有些行没问题,它们以 DisplayName 开头,有些行不行,因为它们以 DisplayVersion
开头
例如:
输入:
DisplayVersion; 1.0.204, DisplayName; Citrix Online Launcher
DisplayName; Microsoft Automated Troubleshooting Services Shim
DisplayVersion; 3.5.30729, DisplayName; Microsoft .NET Framework 3.5 SP1
DisplayName; Hotfix for Microsoft .NET Framework 3.5 SP1 (KB958484), DisplayVersion; 1
DisplayName; Update for Microsoft .NET Framework 3.5 SP1 (KB963707), DisplayVersion; 1
DisplayVersion; 5.2.3790.1164, DisplayName; Windows Resource Kit Tools - SubInAcl.exe
DisplayVersion; 1.0.1361.27800, DisplayName; Windows Resource Kit Tools - GPInventory.exe
DisplayVersion; 9.0.10.29005, DisplayName; VMware Tools
预期输出
DisplayName; Citrix Online Launcher,DisplayVersion; 1.0.204
DisplayName; Microsoft Automated Troubleshooting Services Shim
DisplayName; Microsoft .NET Framework 3.5 SP1,DisplayVersion; 3.5.30729
DisplayName; Hotfix for Microsoft .NET Framework 3.5 SP1 (KB958484),DisplayVersion; 1
DisplayName; Update for Microsoft .NET Framework 3.5 SP1 (KB963707),DisplayVersion; 1
DisplayName; Windows Resource Kit Tools - SubInAcl.exe,DisplayVersion; 5.2.3790.1164
DisplayName; VMware Tools,DisplayVersion; 9.0.10.29005
呆呆地看着:
gawk -F ', +' -v OFS=, '{ split([=10=], a); asort(a); for(i = 1; i <= NF; ++i) { $i = a[i] } } 1' filename
即:
{ # in every line:
split([=11=], a) # make an array of the fields
asort(a) # sort the array
for(i = 1; i <= NF; ++i) { # put the fields back
$i = a[i]
}
}
1 # print the result.
asort
不是 POSIX 的一部分,因此您需要 GNU awk。
使用 GNU awk 4.* 进行内置排序:
$ cat tst.awk
BEGIN { FS="\s*,\s*"; OFS=","; PROCINFO["sorted_in"]="@val_str_asc" }
{
split([=10=],flds)
fldNr=0
for (i in flds) {
printf "%s%s", flds[i], (++fldNr<NF?OFS:ORS)
}
}
$
$ awk -f tst.awk file
DisplayName; Citrix Online Launcher,DisplayVersion; 1.0.204
DisplayName; Microsoft Automated Troubleshooting Services Shim
DisplayName; Microsoft .NET Framework 3.5 SP1,DisplayVersion; 3.5.30729
DisplayName; Hotfix for Microsoft .NET Framework 3.5 SP1 (KB958484),DisplayVersion; 1
DisplayName; Update for Microsoft .NET Framework 3.5 SP1 (KB963707),DisplayVersion; 1
DisplayName; Windows Resource Kit Tools - SubInAcl.exe,DisplayVersion; 5.2.3790.1164
DisplayName; Windows Resource Kit Tools - GPInventory.exe,DisplayVersion; 1.0.1361.27800
DisplayName; VMware Tools,DisplayVersion; 9.0.10.29005
我需要对每行内容的字段(分隔符是逗号。)进行排序, 我不确定我该怎么做, 有些行没问题,它们以 DisplayName 开头,有些行不行,因为它们以 DisplayVersion
开头例如:
输入:
DisplayVersion; 1.0.204, DisplayName; Citrix Online Launcher
DisplayName; Microsoft Automated Troubleshooting Services Shim
DisplayVersion; 3.5.30729, DisplayName; Microsoft .NET Framework 3.5 SP1
DisplayName; Hotfix for Microsoft .NET Framework 3.5 SP1 (KB958484), DisplayVersion; 1
DisplayName; Update for Microsoft .NET Framework 3.5 SP1 (KB963707), DisplayVersion; 1
DisplayVersion; 5.2.3790.1164, DisplayName; Windows Resource Kit Tools - SubInAcl.exe
DisplayVersion; 1.0.1361.27800, DisplayName; Windows Resource Kit Tools - GPInventory.exe
DisplayVersion; 9.0.10.29005, DisplayName; VMware Tools
预期输出
DisplayName; Citrix Online Launcher,DisplayVersion; 1.0.204
DisplayName; Microsoft Automated Troubleshooting Services Shim
DisplayName; Microsoft .NET Framework 3.5 SP1,DisplayVersion; 3.5.30729
DisplayName; Hotfix for Microsoft .NET Framework 3.5 SP1 (KB958484),DisplayVersion; 1
DisplayName; Update for Microsoft .NET Framework 3.5 SP1 (KB963707),DisplayVersion; 1
DisplayName; Windows Resource Kit Tools - SubInAcl.exe,DisplayVersion; 5.2.3790.1164
DisplayName; VMware Tools,DisplayVersion; 9.0.10.29005
呆呆地看着:
gawk -F ', +' -v OFS=, '{ split([=10=], a); asort(a); for(i = 1; i <= NF; ++i) { $i = a[i] } } 1' filename
即:
{ # in every line:
split([=11=], a) # make an array of the fields
asort(a) # sort the array
for(i = 1; i <= NF; ++i) { # put the fields back
$i = a[i]
}
}
1 # print the result.
asort
不是 POSIX 的一部分,因此您需要 GNU awk。
使用 GNU awk 4.* 进行内置排序:
$ cat tst.awk
BEGIN { FS="\s*,\s*"; OFS=","; PROCINFO["sorted_in"]="@val_str_asc" }
{
split([=10=],flds)
fldNr=0
for (i in flds) {
printf "%s%s", flds[i], (++fldNr<NF?OFS:ORS)
}
}
$
$ awk -f tst.awk file
DisplayName; Citrix Online Launcher,DisplayVersion; 1.0.204
DisplayName; Microsoft Automated Troubleshooting Services Shim
DisplayName; Microsoft .NET Framework 3.5 SP1,DisplayVersion; 3.5.30729
DisplayName; Hotfix for Microsoft .NET Framework 3.5 SP1 (KB958484),DisplayVersion; 1
DisplayName; Update for Microsoft .NET Framework 3.5 SP1 (KB963707),DisplayVersion; 1
DisplayName; Windows Resource Kit Tools - SubInAcl.exe,DisplayVersion; 5.2.3790.1164
DisplayName; Windows Resource Kit Tools - GPInventory.exe,DisplayVersion; 1.0.1361.27800
DisplayName; VMware Tools,DisplayVersion; 9.0.10.29005