根据包含数字和不包含数字的行对CSV中的行进行排序

Vil*_*age 5 csv sorting bash shell awk

我有一个4列的CSV文件.我想对行进行排序,这样,第三列中某处包含任意数字的行被推到文档的末尾,第三列中没有数字的行被放到开头.如何以这种方式对文件进行排序?

更新:

为了澄清,我需要[0-9]在第三列的字母内的某处移动包含任意数字(即匹配)的行(该行的第三列可能包含其他符号).空间并不重要.例如

dog, eats chicken, has 4 legs, does not like cats
cat, eats mice, has a tail, does not like water
mouse, eats bugs, has 4 legs, does not like cats
elephant, eats peanuts, has a trunk, does not like mice
Run Code Online (Sandbox Code Playgroud)

将分类到:

cat, eats mice, has a tail, does not like water
elephant, eats peanuts, has a trunk, does not like mice
dog, eats chicken, has 4 legs, does not like cats
mouse, eats bugs, has 4 legs, does not like cats
Run Code Online (Sandbox Code Playgroud)

pot*_*ong 2

这可能对你有用:

sed 'h;s/,/\n/2;s/.*\n/0/;s/,.*//;s/[^0-9]//g;G;s/\n/\t/' file | 
sort -k1,1n -k2 | 
cut -f2
Run Code Online (Sandbox Code Playgroud)

解释:

  • 使用第三列中的数字生成数字键(如果没有,则将键设置为 0)
  • 按上面的键排序,然后按原始文件排序
  • 删除数字键