linux - 检查数据应为两列格式,带逗号 | Unix |
问题描述
我在文件中有数据
File : Student.txt
AB,A1
BC,A2
CD,A3
,A4 --> not in proper format position value is missing
MM,A5
SD --> not in proper format position value is missing
How to write code so that the Position 0 : AB , Position 0 : A1 should be with comma (,)
如果其中一个不存在,则两个位置值都应该存在,那么它应该显示消息,例如文件格式不正确
解决方案
你可以试试这个:
#!/bin/bash
msg="not in proper format position value is missing"
while read -r line; do
col1="${line%,*}"
col2="${line#*,}"
[[ -z $col1 || -z $col2 || $line != *","* ]] && echo "'$line' $msg"
done < Student.txt
解释:
-z $col1 check if first column has no value
-z $col2 check if second column has no value
$line != *","* check if line contains no comma
推荐阅读
- kotlin - Spring Data Neo4j 5:更新父节点时,@Version 字段意外增加
- c++ - 在 Linux 中构建时出现 Mysqlcppconn8 和 cmake 错误
- f# - 包含带有异步调用的 while 循环的 F# 异步函数无法编译
- c - 使用 gcc 编译时无法链接库
- java - 带有用于字符串列表的弹簧引导规范的 IN 子句
- android - 如何在具有不同 SIGN 凭据的终端中构建 apk
- visual-studio-app-center - 如何在 App Center 中为 Android 或 iOS 构建指定文件名
- postgresql - SpringbootTest + TestContainers:测试污染数据库后如何刷新数据库
- c# - 输入模拟器虚拟键代码 RETURN 充当空格键
- swift4 - 将 responseJSON 解析为 ObjectMapper