我怎么能回应一次线,然后其余部分保持它们在unix bash中的方式?

时间:2013-08-16 10:03:18

标签: bash echo solaris

我有以下评论:

(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

2 个答案:

答案 0 :(得分:1)

尝试在for循环之前移动标题行的回显:

(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)

答案 1 :(得分: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”,如果没有数字出现多次,则不打印输出(标题除外)。