Leetcode Shell

Some interesting shell script on Leetcode

Word Frequency

1
grep -oE '[a-z]+' words.txt | sort | uniq -c | sort -r | awk '{print $2" "$1}'

Transpose File

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
## 任何在BEGIN之后列出的操作(在{}内)将在Unix awk开始扫描输入之前执行,而END之后列出的操作将在扫描完全部的输入之后执行。因此,通常使用BEGIN来显示变量和预置(初始化)变量,使用END来输出最终结果。
awk '
{
for (i = 1; i <= NF; i++) {
if(NR == 1) {
s[i] = $i;
} else {
s[i] = s[i] " " $i;
}
}
}
END {
for (i = 1; s[i] != ""; i++) {
print s[i];
}
}' file.txt

Valid Phone Numbers

1
grep -Eo '^(\([0-9]{3}\) ){1}[0-9]{3}-[0-9]{4}$|^([0-9]{3}-){2}[0-9]{4}$' file.txt

Tenth Line

1
sed -n  '10p' file.txt
Contents
  1. 1. Some interesting shell script on Leetcode
    1. 1.1. Word Frequency
    2. 1.2. Transpose File
    3. 1.3. Valid Phone Numbers
    4. 1.4. Tenth Line