2013-08-16 49 views
0

我有以下評論:我該如何迴應一行,然後讓其他人保持他們在unix bash中的方式?

(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

試圖在for循環之前移動標題行的回顯? – svante

+0

但是在這種情況下,如果沒有重複的數字,那麼它就沒有意義了 – t28292

+0

如果你可以建議另一個命令,因爲我面臨這個問題:如果455在輸入的第2,3和15行重複文件,它會顯示兩次,如455 879 741 455(肯定是列形式) – t28292

回答

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) 
+0

,但是如果沒有重複的數字,它會顯示句子嗎? – t28292

+0

我的第二個解決方案可能有幫助(編輯)。 – svante

+0

我試過你的第二個解決方案,我正面臨着這個問題:如果455在輸入文件的第2行,第3行和第15行中重複出現,它將顯示兩次,如455 879 741 455(確實是列形式) – t28292

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 $

我加了引號,改變,如果()以[..]的表達,終於感動描述語句退出循環。測試的發生次數在「-ge」狀態附近是數字。如果是「1」,則打印出現一次或多次的數字。注意,在這個表達式中,如果文件包含例如數字

然後 「8」 輸出被列爲出現兩次。使用「-ge 2」時,如果沒有數字出現超過一次,則不會輸出(標題除外)。

相關問題