使用UNIX对2行文件.csv进行排序
我需要一些帮助来整理这个.csv 排序需要与y和y相关的第一行相对应,因此它们必须按y排序,以此类推使用UNIX对2行文件.csv进行排序,unix,Unix,我需要一些帮助来整理这个.csv 排序需要与y和y相关的第一行相对应,因此它们必须按y排序,以此类推 y, d, a, w, c,....... y, d, a, w, c,....... 输出 a, c ,d ,w ,y a, c ,d ,w ,y 谢谢如果您的输入是非常直接的逗号分隔字符串列表,那么此列表可以通过使用tr和排序来完成这项工作 $ echo " y, d, a, w, c" | tr , "\n" | sort | tr "\n" , | sed 's@,$@\n@' a
y, d, a, w, c,.......
y, d, a, w, c,.......
输出
a, c ,d ,w ,y
a, c ,d ,w ,y
谢谢如果您的输入是非常直接的逗号分隔字符串列表,那么此列表可以通过使用
tr
和排序来完成这项工作
$ echo " y, d, a, w, c" | tr , "\n" | sort | tr "\n" , | sed 's@,$@\n@'
a, c, d, w, y
要获得更通用的解决方案,请尝试使用GNU awk
,如下所示:-
$ cat script.awk
#!/bin/gawk
BEGIN {
FS="," # Setting input-field-separator to ','
OFS="," # Setting output-field-separator to ','
}
{
split($0, words) # Split the strings and store it in array
asort(words) # Using gawk's inherent sort function 'asort' sort the words alphabetically
for (i in words) $i=words[i] # Store the words back in the array and printing the same
print
}
和一个示例输入文件
$ cat input.csv
pineapple,blueberries,strawberries
pencil,crayon,chalk,marker
bus,car,train,motorcycle,bicycle,skateboard
使用gawk
as运行脚本
$ gawk -f script.awk input.csv
blueberries,pineapple,strawberries
chalk,crayon,marker,pencil
bicycle,bus,car,motorcycle,skateboard,train
如果您的输入是非常直接的逗号分隔的字符串列表,那么使用tr
和sort
可以完成这项工作
$ echo " y, d, a, w, c" | tr , "\n" | sort | tr "\n" , | sed 's@,$@\n@'
a, c, d, w, y
要获得更通用的解决方案,请尝试使用GNU awk
,如下所示:-
$ cat script.awk
#!/bin/gawk
BEGIN {
FS="," # Setting input-field-separator to ','
OFS="," # Setting output-field-separator to ','
}
{
split($0, words) # Split the strings and store it in array
asort(words) # Using gawk's inherent sort function 'asort' sort the words alphabetically
for (i in words) $i=words[i] # Store the words back in the array and printing the same
print
}
和一个示例输入文件
$ cat input.csv
pineapple,blueberries,strawberries
pencil,crayon,chalk,marker
bus,car,train,motorcycle,bicycle,skateboard
使用gawk
as运行脚本
$ gawk -f script.awk input.csv
blueberries,pineapple,strawberries
chalk,crayon,marker,pencil
bicycle,bus,car,motorcycle,skateboard,train
这是一个基于伊尼安最初答案的方法-积分应该留给他
while true
do read line
if [ "x${line}" == "x" ]
then break
else
echo $line | tr , "\n" | sort | tr "\n" ,
echo
fi
done
只需这样使用:line\u sort.shsorted\u file
免责声明:这只适用于非常简单的csv。电子表格生成的CSV可能要复杂得多,因为CSV可以在字段内支持换行符和逗号。如果我想要一个健壮的解决方案,我会使用真正的语言和CSV库。Python附带了一个优秀的CSV模块…这里有一个基于伊尼安原始答案的方法-积分应该留给他
while true
do read line
if [ "x${line}" == "x" ]
then break
else
echo $line | tr , "\n" | sort | tr "\n" ,
echo
fi
done
只需这样使用:line\u sort.shsorted\u file
免责声明:这只适用于非常简单的csv。电子表格生成的CSV可能要复杂得多,因为CSV可以在字段内支持换行符和逗号。如果我想要一个健壮的解决方案,我会使用真正的语言和CSV库。Python附带了一个优秀的CSV模块…我们能不能编写一个小java程序来读取这些内容并根据需要进行排序。如果是这样的话,我可以写一个吗?我需要使用unix shell脚本:/我们能不能编写一个小java程序来读取这些内容并根据需要进行排序。如果是这样的话,我可以写一个吗?我需要使用unix shell脚本:/@Johnny:使用gawk
解决方案,在最近的更新中,我真的很喜欢hacky first@SergeBallesta:作为一个长期用户,你可以通过一个漂亮的“向上投票”来表示感谢(除非你是认真的)ironically@Inian我正要这么做(我现在已经这么做了),但我正在准备一种基于您对多行输入的工作的方法。如果你喜欢的话,可以把它包括在你的答案中,然后打电话给我,这样我就可以删除我的。@SergeBallesta:干杯@约翰尼:使用gawk
解决方案,在最近的更新中,我真的很喜欢hacky first@SergeBallesta:作为一个长期用户,你可以通过一个漂亮的“向上投票”来表示感谢(除非你是认真的)ironically@Inian我正要这么做(我现在已经这么做了),但我正在准备一种基于您对多行输入的工作的方法。如果你喜欢的话,可以把它包括在你的答案中,然后打电话给我,这样我就可以删除我的。@SergeBallesta:干杯!用<>
re direction:)用<>
re direction:)的方法很好