linux - Reading and comparing column in .txt file in bash -
i got .txt file content is
5742060626,ms.pimpan tantivaravong,female
5742065826,ms.kaotip tanti,female
- create interface script add list in file first, have compare input id exitsting id in list. use cut command read 1st column of .txt file. but,i got problem when trying compare it.
here code.
-
!/bin/bash # datafile='student-2603385.txt' while read p; if [ "$id" == (echo $p | cut -d, -f1) ] echo 'duplicate id' fi done <$datafile
-
could suggest me, how should do? thank you
your script has numerous quoting bugs, quote variable expansion when variable contains file name, expected when want avoid word splitting , pathname expansion shell.
letting aside, in if [ "$id" == (echo $p | cut -d, -f1) ]
:
you need command substitution,
$()
aroundecho ... | cut ...
, not subshell()
you need quotes around
$()
prevent word splitting (and pathname expansion)==
bash-ism, not defined posix, remindertry use
[[
as possible, being shell keyword[[
handles word splitting
so test
([
):
if [ "$id" == "$(echo "$p" | cut -d, -f1)" ]
better:
if [[ $id == $(echo "$p" | cut -d, -f1) ]]
Comments
Post a Comment