Unix命令从2个单独的文件中添加2个数字并将其写入第三文件

发布于 2025-01-23 07:04:47 字数 716 浏览 3 评论 0原文

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我还不会笑 2025-01-30 07:04:47

需要一些修复程序,这是我的版本:

#!/bin/bash

file_1=$(wc -l p.csv | cut -d' ' -f1)
echo "file_1=$file_1"
file_2=$(wc -l m.csv | cut -d' ' -f1)
echo "file_2=$file_2"

file_3=$(( file_1 + file_2 ))
echo "file_3=$file_3"

if [[ $file_3 -gt 25 ]]
then
    echo "ERROR"
    touch error.txt
else
    echo "Success"
    touch success.txt
fi
  • 修改了算术行以使用$(())语法。
  • 如果,您不需要file_3.txt。如果由于某些其他原因需要它,则可以将Bach放置为echo“ $ file_3”&gt; file_3.txt“ line。
  • 我添加了一个echo语句,以进行调试。

Some fixes are required, here is my version:

#!/bin/bash

file_1=$(wc -l p.csv | cut -d' ' -f1)
echo "file_1=$file_1"
file_2=$(wc -l m.csv | cut -d' ' -f1)
echo "file_2=$file_2"

file_3=$(( file_1 + file_2 ))
echo "file_3=$file_3"

if [[ $file_3 -gt 25 ]]
then
    echo "ERROR"
    touch error.txt
else
    echo "Success"
    touch success.txt
fi
  • The arithmetic line was modified to use the $(( )) syntax.
  • you do not need file_3.txt for the if. If you required it for some other reason, you can put bach the echo "$file_3" > file_3.txt" line.
  • I added a couple echo statements for debugging purposes.
萌︼了一个春 2025-01-30 07:04:47

您犯了一些错误:

echo $file_1
# Not really wrong, but use quotes
echo "$file_1"

不要在分配中使用=周围的空格,请勿使用backtics,并且使用{}在变量周围

file_3 = $(`expr $(file_1) + $(file_2)`)
# change this to
file_3=$(expr ${file_1} + ${file_2})
# and consider using (( a = b + c )) for calculations

只需要最终结果时,请考虑

if (( $(cat [pm].csv | wc -l) > 25 )); then
  touch error.txt 
else
  touch success.txt
fi

Some errors you made:

echo $file_1
# Not really wrong, but use quotes
echo "$file_1"

Don't use spaces around = in assignment, don't use backtics and use {} around variables

file_3 = $(`expr $(file_1) + $(file_2)`)
# change this to
file_3=$(expr ${file_1} + ${file_2})
# and consider using (( a = b + c )) for calculations

When you only want the final results, consider

if (( $(cat [pm].csv | wc -l) > 25 )); then
  touch error.txt 
else
  touch success.txt
fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文