shell-一些小问题

Posted 悟空很开心

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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<Integer>[] bucket = new List[nums.length + 1];
Map<Integer, Integer> frequencyMap = new HashMap<Integer, Integer>();

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<Integer> 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]\\d2|[1-9]?\\d).(25[0-5]|2[0-4]\\d|[0-1]\\d2|[1-9]?\\d).(25[0-5]|2[0-4]\\d|[0-1]\\d2|[1-9]?\\d).(25[0-5]|2[0-4]\\d|[0-1]\\d2|[1-9]?\\d)
手机号码:0?(13|14|15|18)[0-9]9

 (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit):grep -P ‘^(\\d3-|(\\d3) )\\d3-\\d4 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

以上是关于shell-一些小问题的主要内容,如果未能解决你的问题,请参考以下文章

shell脚本小练习

vcenter一些小知识

Linux的一个小问题的研究琐事记 - 1

shell脚本小实例

shell 编程 之 小技巧

小工具shell检测进程状态异常自动重启