0

我有以下评论:

(for i in 'cut -d "," -f1 file.csv | uniq`; do var =`grep -c $i file.csv';if (($var > 1 )); then echo " you have   the following repeated numbers" $i ; fi ; done)

我得到的输出是: 你有以下重复的数字 455 你有以下重复的数字 879 你有以下重复的数字 741

我想要的是以下输出:

                        you have the following repeated numbers:
                        455
                        879
                        741
4

2 回答 2

1

尝试在 for-loop 之前移动标题行的回声:

(echo " you have   the following repeated numbers"; for i in 'cut -d "," -f1 file.csv | uniq`; do var =`grep -c $i file.csv';if (($var > 1 )); then echo $i ; fi ; done)

或者只打印一次标题:

(header=" you have   the following repeated numbers\n"; for i in 'cut -d "," -f1 file.csv | uniq`; do var =`grep -c $i file.csv';if (($var > 1 )); then echo -e $header$i ; header=""; fi ; done)
于 2013-08-16T10:15:24.783 回答
0

好吧,这就是我的想法:

1) 为测试生成的输入

for x in {1..35},aa,bb ; do echo $x ; done > file.csv 
for x in {21..48},aa,bb ; do echo $x ; done >> file.csv 
for x in {32..63},aa,bb ; do echo $x ; done >> file.csv
unsort file.csv > new.txt ; mv new.txt file.csv 

2)你的行(更正的语法错误)

dtpwmbp:~ pwadas$ for i in $(cut -d "," -f1 file.csv | uniq); 
do var=`grep -c $i file.csv`; if [ "$var" -ge 1 ] ; 
 then echo " you have the following repeated numbers" $i ; fi ; done | head -n 10 

you have the following repeated numbers 8
you have the following repeated numbers 41
you have the following repeated numbers 18
you have the following repeated numbers 34
you have the following repeated numbers 3
you have the following repeated numbers 53
you have the following repeated numbers 32
you have the following repeated numbers 33
you have the following repeated numbers 19
you have the following repeated numbers 7
dtpwmbp:~ pwadas$ 

3)我的线路:

dtpwmbp:~ pwadas$ echo "you have the following repeated numbers:";  
 for i in $(cut -d "," -f1 file.csv | uniq); do var=`grep -c $i file.csv`; 
  if [ "$var" -ge 1 ] ; then echo $i ; fi ; done | head -n 10 
you have the following repeated numbers:
8
41
18
34
3
53
32
33
19
7

dtpwmbp:~ pwadas$

我添加了引号,将 if() 更改为 [..] 表达式,最后将描述语句移出循环。测试的出现次数是“-ge”条件附近的数字。如果为“1”,则打印出现一次或多次的数字。请注意,在此表达式中,如果文件包含例如数字

8 12 48

然后“8”在输出中被列为出现两次。使用“-ge 2”,如果没有数字出现多次,则不打印输出(标题除外)。

于 2013-08-16T10:21:34.897 回答