Unix命令从2个单独的文件中添加2个数字并将其写入第三文件
我有2个文件。我需要添加两者的行计数,然后将其写入第三文件。 如果第三文件的内容为25,我需要打印error.txt和如果=< 25,我需要打印成功
。 文件1(P.CSV),计数:20 文件2(M.CSV),计数:5 文件3,计数25 --->应该打印错误.txt
我尝试了以下代码,但是文件3并未通过预期输出生成。
file_1=$(cat p.csv | wc -l)
echo $file_1
file_2=$(cat m.csv | wc -l)
echo $file_2
file_3 = $(`expr $(file_1) + $(file_2)`)
echo $file_3 > file_3.txt
if [ $file_3 -gt 25 ]; then
touch error.txt
else
touch success.txt
fi
错误信息:
20
5
count_test.sh: line 16: file_1: command not found
count_test.sh: line 16: file_2: command not found
expr: syntax error
count_test.sh: line 16: file_3: command not found
count_test.sh: line 20: [: -gt: unary operator expected
I have 2 files. I need to add the count of rows of the both and write it to 3rd file.
If the content of 3rd file is >25 , i need to print error.txt and if =<25 , i need to print success.txt
Scenario:
file 1(p.csv) , count: 20
file 2 (m.csv), count : 5
file 3 , count 25
--->should print error.txt
I have tried the below code, but file 3 is not getting generated with expected output.
file_1=$(cat p.csv | wc -l)
echo $file_1
file_2=$(cat m.csv | wc -l)
echo $file_2
file_3 = $(`expr $(file_1) + $(file_2)`)
echo $file_3 > file_3.txt
if [ $file_3 -gt 25 ]; then
touch error.txt
else
touch success.txt
fi
Error message:
20
5
count_test.sh: line 16: file_1: command not found
count_test.sh: line 16: file_2: command not found
expr: syntax error
count_test.sh: line 16: file_3: command not found
count_test.sh: line 20: [: -gt: unary operator expected
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
需要一些修复程序,这是我的版本:
$(())
语法。file_3.txt
。如果由于某些其他原因需要它,则可以将Bach放置为echo“ $ file_3”&gt; file_3.txt“
line。echo
语句,以进行调试。Some fixes are required, here is my version:
$(( ))
syntax.file_3.txt
for theif
. If you required it for some other reason, you can put bach theecho "$file_3" > file_3.txt"
line.echo
statements for debugging purposes.您犯了一些错误:
不要在分配中使用
=
周围的空格,请勿使用backtics,并且使用{}
在变量周围只需要最终结果时,请考虑
Some errors you made:
Don't use spaces around
=
in assignment, don't use backtics and use{}
around variablesWhen you only want the final results, consider