Linux 检查数据应为带逗号的两列格式|
我有一个数据档案Linux 检查数据应为带逗号的两列格式|,linux,shell,Linux,Shell,我有一个数据档案 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 shoul
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
你试过什么?你的问题是什么?你试过什么东西了吗?你卡住了吗?如果你希望有人为你工作,我们的文化习惯是为别人的工作付钱。为了解决你的问题,我建议你学习正则表达式。然后使用
awk
或sed
匹配该行(或匹配格式错误的数据),并执行检查。是否正在尝试逐行分析文本文件,以检查它是否由逗号分隔的两个值分隔?如果是,您是否希望它检查这两个值是否也是特定长度(即两个字符串,如您的示例)?@Carley在处理第一条记录AB时正在使用while循环,A1->我想添加一个检查,比如它们的as是两个参数第一个参数AB和第二个参数A1,如果其中任何一个参数缺失,则退出循环并显示消息文件格式不正确
-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