shell-一些小问题

1 单词统计
cat words.txt | tr -s ’ ’ ‘\n’ | sort | uniq -c | sort -r | awk ‘{ print 2, 1 }’
结果:
f 3
sd 1
ok. 1
Im 1
Ihe 1
hsha 1
fsdjfjsaf 1
fd 1
fas 1
ds 1
asd. 1
2 top K
Given [1,1,1,2,2,3] and k = 2, return [1,2].
public List topKFrequent(int[] nums, int k) {

List[] bucket = new List[nums.length + 1];
Map frequencyMap = new HashMap();

for (int n : nums) {
    frequencyMap.put(n, frequencyMap.getOrDefault(n, 0) + 1);
}

for (int key : frequencyMap.keySet()) {
    int frequency = frequencyMap.get(key);
    if (bucket[frequency] == null) {
        bucket[frequency] = new ArrayList<>();
    }
    bucket[frequency].add(key);
}

List res = new ArrayList<>();

for (int pos = bucket.length - 1; pos >= 0 && res.size() < k; pos--) {
    if (bucket[pos] != null) {
        res.addAll(bucket[pos]);
    }
}
return res;

}

合法IP:(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d).(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d).(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d).(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)
手机号码:0?(13|14|15|18)[0-9]{9}

 (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit):grep -P ‘^(\d{3}-|(\d{3}) )\d{3}-\d{4} file.txtfile.txtsednr/([09]3|([09]3))[09]3[09]4 /p’ file.txt
awk ‘/^([0-9]{3}-|([0-9]{3}) )[0-9]{3}-[0-9]{4} /file.txtawk/([[:digit:]]3|([[:digit:]]3))[[:digit:]]3[[:digit:]]4 /’ file.txt

指定输出文件的第几行:比如第10行
awk ‘NR==10’ file.txt
sed -n ‘1p;’ file
多行:
awk ‘NR==1||NR==4||NR==7||NR==9’ file
sed -n ‘1p;4p;7p;9p’ file

你可能感兴趣的:(Linux)