gpt4 book ai didi

bash - 使用 bash 脚本对 CSV 文件进行排序和比较

转载 作者:行者123 更新时间:2023-12-04 18:48:27 25 4
gpt4 key购买 nike

我正在尝试比较两个排序的 csv 文件:

for filename in `ls "$1"/dev`
do
dev_file="$1/dev/$filename"
ccu_file="$1/ccu/$filename "
echo "diff of $dev_file ...and......$ccu_file"
result=$(diff <(sort $dev_file) <(sort $ccu_file ))
if [ $? -eq 0 ]
then
echo "files are the same"
else
echo "files are different"
echo "$result"
fi
done
看来,有语法错误:
test.sh: command substitution: line 6: syntax error near unexpected token `('
test.sh: command substitution: line 6: `diff <(sort $dev_file) <(sort $ccu_file )'
任何帮助,将不胜感激。谢谢你。

最佳答案

您可以像这样比较两个文件:

if cmp -s file1.csv file2.csv; then
echo files are identical
else
echo files are different
fi
从您的脚本来看,您似乎想要比较 dev/ 中的每个 csv 文件 ccu/中的同名文件.你可以这样做:
#!/bin/bash

cd "${1:?}/dev" && [[ -d ../ccu ]] || exit 1

for i in *.csv; do
if [[ -e ../ccu/"$i" ]]; then
if cmp -s "$i" ../ccu/"$i"; then
echo files "$i" and ../ccu/"$i" are identical
else
echo files "$i" and ../ccu/"$i" are different
fi
else
echo "../ccu/$i: not found" >&2
fi
done

关于bash - 使用 bash 脚本对 CSV 文件进行排序和比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69810394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com