在bash中添加文件的相似行中存在的数字

时间:2018-12-01 03:29:36

标签: sorting awk scripting uniq

我有这样的文件:

public class Presidents 
{   

    public int presidentNumber;
    public String presidentName;
    public int startingYear;
    public int endingYear;

    public Presidents(int PresidentNumber, String NameOfPresident, int StartingYear, int EndingYear)
    {
        this.presidentNumber = PresidentNumber;
        this.presidentName = NameOfPresident;
        this.startingYear = StartingYear;
        this.endingYear = EndingYear;

    }

    public Presidents() {

    }

    public Presidents [] PresidentList () 
    {           
        Presidents [] president = new Presidents [45];
        president [0] = Presidents(1, "George Washington", 1789, 1797);
        president [1] = Presidents(2, "John Adams", 1797, 1801);
        president [2] = Presidents(3, "Thomas Jefferson", 1801, 1809);
        president [3] = Presidents(4, "James Madison", 1809, 1817);
        president [4] = Presidents(5, "James Monroe", 1817, 1825);
        president [5] = Presidents(6, "John Quincy Adams", 1825, 1829);
        president [6] = Presidents(7, "Andrew Jackson", 1829, 1837);
        president [7] = Presidents(8, "Martin Van Buren", 1837, 1841);
        president [8] = Presidents(9, "William Henry Harrison", 1841, 1841);
        president [9] = Presidents(10, "John Tyler", 1841, 1845);
        president [10] = Presidents(11, "James K. Polk", 1845, 1849);
        president [11] = Presidents(12, "Zachary Taylor", 1849, 1850);
        president [12] = Presidents(13, "Millard Fillmore", 1850, 1853);
        president [13] = Presidents(14, "Franklin Pierce", 1853, 1857);
        president [14] = Presidents(15, "James Buchanan", 1857, 1861);
        president [15] = Presidents(16, "Abraham Lincoln", 1861, 1865);
        president [16] = Presidents(17, "Andrew Johnson", 1865, 1869);
        president [17] = Presidents(18, "Ulysses S. Grant", 1869, 1877);
        president [18] = Presidents(19, "Rutherford B. Hayes", 1877, 1881);
        president [19] = Presidents(20, "James A. Garfield", 1881, 1881);
        president [20] = Presidents(21, "Chester A. Arthur", 1881, 1885);
        president [21] = Presidents(22, "Grover Cleveland", 1885, 1889);
        president [22] = Presidents(23, "Benjamin Harrison", 1889, 1893);
        president [23] = Presidents(24, "Grover Cleveland", 1893, 1897);
        president [24] = Presidents(25, "William McKinley", 1897, 1901);
        president [25] = Presidents(26, "Theodore Roosevelt", 1901, 1909);
        president [26] = Presidents(27, "William Howard Taft", 1909, 1913);
        president [27] = Presidents(28, "Woodrow Wilson", 1913, 1921);
        president [28] = Presidents(29, "Warren G. Harding", 1921, 1923);
        president [29] = Presidents(30, "Calvin Coolidge", 1923, 1929);
        president [30] = Presidents(31, "Herbert Hoover", 1929, 1933);
        president [31] = Presidents(32, "Franklin D. Roosevelt", 1933, 1945);
        president [32] = Presidents(33, "Harry S. Truman", 1945, 1953);
        president [33] = Presidents(34, "Dwight D. Eisenhower", 1953, 1961);
        president [34] = Presidents(35, "John F. Kennedy", 1961, 1963);
        president [35] = Presidents(36, "Lyndon B. Johnson", 1963, 1969);
        president [36] = Presidents(37, "Richard Nixon", 1969, 1974);
        president [37] = Presidents(38, "Gerald Ford", 1974, 1977);
        president [38] = Presidents(39, "Jimmy Carter", 1977, 1981);
        president [39] = Presidents(40, "Ronald Reagan", 1981, 1989);
        president [40] = Presidents(41, "George H. W. Bush", 1989, 1993);
        president [41] = Presidents(42, "Bill Clinton", 1993, 2001);
        president [42] = Presidents(43, "George W. Bush", 2001, 2009);
        president [43] = Presidents(44, "Barack Obama", 2009, 2017);
        president [44] = Presidents(45, "Donald Trump", 2017, 2018);

        return president;
    }   

    private Presidents Presidents(int i, String string, int j, int k) {         
        return Presidents (i, string, j, k);
    }

}
import java.util.Scanner;
import java.util.Random;


public class PresidentsQuiz {

    public static void main (String [] args)    
    {       
        System.out.println("Do you know the 45 presidents of the United States? Enter a number between 1 and 45");

        Scanner kb = new Scanner (System.in);           
        int input =  kb.nextInt();

        while (input < 1 ||  input > 45)        
        {
            System.out.println("Enter a number between 1 and 45");
            input =  kb.nextInt();
        }

        Presidents president = new Presidents ();                   
        Presidents [] presidentList = president.PresidentList();                        
        System.out.println(presidentList [0]);
        kb.close();

    }

}

我想要输出:

[host]$ cat /tmp/data
Breakfast 1
Lunch 1
Dinner 1
Dinner 1
Dinner 1
Lunch 1
Lunch 1
Dinner 1

如何使用awk / sed命令行脚本来做到这一点?

执行以下命令后,我得到了:

Breakfast 1
Lunch 3
Dinner 4

我现在不知道如何添加这些数字。

3 个答案:

答案 0 :(得分:1)

awk '{a[$1]+=$2} END{for(i in a){print i, a[i]}}' /tmp/data
Dinner 4
Breakfast 1
Lunch 3

答案 1 :(得分:1)

请尝试以下操作,它将按照输入文件中第1个字段的输入顺序输出。

awk '!a[$1]++{b[++count]=$1} {c[$1]++} END{for(i=1;i<=count;i++){print b[i],c[b[i]]}}' Input_file

输出如下。

Breakfast 1
Lunch 3
Dinner 4

说明: 现在也添加了上述代码的说明。

awk '
!a[$1]++{                    ##Checking condition if current lines first field is having only 1 count in array a then do following.
  b[++count]=$1              ##Creating an array named b whose index is variable count whose value is increasing number by 1 and value is $1.
}
{
  c[$1]++                    ##Creating an array named c whose index is $1 with increment value by 1.
}
END{                         ##Starting END block of awk code here.
  for(i=1;i<=count;i++){     ##Starting a for loop from i=1 to till value of count here.
    print b[i],c[b[i]]       ##Printing value of array b whose index is variable i and printing value of array c whose index is value of array b.
  }
}'  Input_file               ##Mentioning Input_file name here.

答案 2 :(得分:1)

由于每行输入的数字始终为1,因此您可以忽略它:

$ sort file | uniq -c | awk '{print $2, $1}'
Breakfast 1
Dinner 4
Lunch 3

或按出现次数排序:

$ sort file | uniq -c | sort -n | awk '{print $2, $1}'
Breakfast 1
Lunch 3
Dinner 4