English 中文(简体)
在壳牌上添加一些数字?
原标题:Adding some numbers in Shell Script?
  • 时间:2012-05-15 21:20:46
  •  标签:
  • shell

我如何能够得到以下数字的总和? 在加入之前,他们正在倍增。

Input File (count.txt)
2*4
2*1
3*1
3*1
2*1
2*1

Command I am using:
( echo 0 ; sed  s/$/ +/  count.txt; echo p ) | dc.

The Sum I am getting is 343 which is not correct. I am expecting 20 as sum.

2*4
2*1
3*1
3*1
2*1
2*1

概述:20(每行一次复制,然后是总和)

任何想法?

Thanks, Raj

最佳回答

还将完成以下工作:

awk -F *   {i+=$1*$2} END{print i}  count.txt
问题回答

这项工作应当:

cat count.txt | tr \n + | sed  s/$/
/  | bc

否则,你必须修改原封不动的表述。

cat count | while read line; do echo $(($line)); done | awk  {i+=$1} END {print i} 

This is the best solution I could come up with, hope it helps.





相关问题
KornShell- Creating a fixed width text file

I need to create a simple fixed width text file in KornShell (ksh). My current attempt using printf to pad the string isn t working out very well. What s the shortest, cleanest way to create a fixed ...

unix find command

how to use the find command to find files/directories which are not matching the pattern. for eg: find <some options > -name "dontfile.txt" should give me output of all the find whose file ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

1. 露台式照像

在Windows XP中,没有一时出现指挥时,是否有办法操作一种灰色文字? 我常常需要把“善待”(工作)与“灰色”同起来,即使我的文字没有产出,......

热门标签