bash - 根据 shell 中的某个字段在 csv 中查找唯一的出现
问题描述
我有一个文件emails.csv
:
>cat emails.csv
1,joe,joe@gmail.com,32
2,jim,jim@hotmail.fr,23
3,steve,steve_smith@temporary.com.br,45
4,joseph,joseph@protonmail.com,23
5,jim,jim29@bluewin.ch,29
6,hilary,hilary@bluewin.ch,32
当我找到具有相同最后一个字段(年龄)的另一个条目时,我只想保留第一个条目 - 基于最后一个字段的唯一条目。我想要的输出是:
1,joe,joe@gmail.com,32
2,jim,jim@hotmail.fr,23
3,steve,steve_smith@temporary.com.br,45
5,jim,jim29@bluewin.ch,29
以下脚本能够进行过滤:
> cut -d, -f4 emails.csv |
> while read age1;
> do line=1;continue_loop=1 cut -d, -f4 emails.csv | while read age;
> do if [[ $age1 == $((age)) ]] && [[ $continue_loop == $1 ]];
> then cat emails.csv | head -n $line | tail -n 1;
> continue_loop=0; fi;
> let line++;
> done;
> done | sort
但是,我正在寻找一种不需要两个循环的解决方案,因为这似乎有点过于复杂。
解决方案
sort -t, -k4 emails.csv | sed -e 's/,/ /g' | uniq -f3 | sed -e 's/ /,/g'
但似乎 Perl 或 Pyhon 等其他语言会帮助您编写更稳定且不那么丑陋的解决方案
推荐阅读
- flutter - 如何为 graphql/flutter 设置登录令牌
- javascript - 将 TSX/JSX React 转换为字符串或 DOM 元素
- c - 动态内存分配时的垃圾值
- c# - Android 可调整大小的窗口
- wordpress - Wordpres:安装失败:下载失败。找不到工作的交通工具
- javascript - 如何将数字字节拆分为 Uint8Array
- javascript - 正确的交叉点观察器集
- angular - 从 npm-cache 在离线机器中安装 angular cli 后找不到模块'@angular-devkit/core'
- ios - [引脚纬度]:发送到实例的无法识别的选择器
- ruby-on-rails - 有没有办法在 RSpec 控制器测试中测试模板是否具有属性?