Leetcode Shell

Posted by Meng Cao on 2019-06-12

192. Word Frequency

Write a bash script to calculate the frequency of each word in a text file words.txt.

For simplicity sake, you may assume:

words.txt contains only lowercase characters and space ’ ’ characters.
Each word must consist of lowercase characters only.
Words are separated by one or more whitespace characters.
Example:

Assume that words.txt has the following content:

the day is sunny the the
the sunny is is
Your script should output the following, sorted by descending frequency:

the 4
is 3
sunny 2
day 1
Note:

Don’t worry about handling ties, it is guaranteed that each word’s frequency count is unique.
Could you write it in one-line using Unix pipes?

1
2
# Read from the file words.txt and output the word frequency list to stdout.
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{print $2, $1}'

cat : 取得文件内容;
tr : 转换或删除文件中的字符; -s: 缩减连续重复的字符成制定的单个字符。
uniq : 重复数据只取一个; -i : 忽略大小写; -c: 进行计数。
sort : 排序; -r: 反向排序
awk: 每次处理一行,处理最小单位为字段。

193. Valid Phone Numbers

Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers.

You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)

You may also assume each line in the text file must not contain leading or trailing white spaces.

Example:

Assume that file.txt has the following content:

987-123-4567
123 456 7890
(123) 456-7890
Your script should output the following valid phone numbers:

987-123-4567
(123) 456-7890

分析:正则表达式匹配,其实后面都一样,只是前面可能是(xxx) 或者xxx-.

1
2
3
4
5
6
7
8
9
10
11
12
# Read from the file file.txt and output all valid phone numbers to stdout.

#Solution 1:
#awk '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/' file.txt
#正则表达式匹配'//'; |:或运算符, (A|B); [0-9]{3}: 3个0-9的数字; \(: 转义运算符,显示给出(的意思。^: 行开头; $:行末尾

#Solution 2:
#sed -n -r '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/p' file.txt
#-n 表示关闭默认输出; -r 表示支持正则扩展; 正则表达式后面多了一个p, 在用sed时, p和-n合用,表示打印某一行.

#Solution 3:
grep -P '^(\d{3}-|\(\d{3}\) )\d{3}-\d{4}$' file.txt

194. Transpose File
Given a text file file.txt, transpose its content.

You may assume that each row has the same number of columns and each field is separated by the ’ ’ character.

Example:

If file.txt has the following content:

name age
alice 21
ryan 30
Output the following:

name alice ryan
age 21 30

1
2
3
4
5
6
7
8
9
10
11
# Read from the file file.txt and print its transposed content to stdout.
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

awk命令中NF表示当前记录中的字段个数,也就是列数;
NR表示已读出的行数,编号从1开始;
这个for循环与常规循环不一样,需要循环NR*NF次数;在这边也就是1,2,1,2,1,2这样的情况。

1
2
3
4
5
6
i = 1, s = [name]
i = 2, s = [name; age]
i = 1, s = [name alice; age]
i = 2, s = [name alice; age 21]
i = 1, s = [name alice ryan; age 21]
i = 2, s = [name alice ryan; age 21 30]

195. Tenth Line

Given a text file file.txt, print just the 10th line of the file.

Example:

Assume that file.txt has the following content:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Your script should output the tenth line, which is:

Line 10

1
2
3
4
5
6
7
8
9
10
11
# Read from the file file.txt and output the tenth line to stdout.
#Solution 1
#awk '{if(NR == 10) print $0}' file.txt
#Solution 2
#awk 'NR == 10' file.txt
#Solution 3
#sed -n 10p file.txt
#Solution 4
#tail -n +10 file.txt | head -n 1
#Solution 5
head -n 10 file.txt | tail -n +10

tail -n 3 file.txt: 打印file文件的最后三行内容      
tail -n +3 file.txt: 从file文件第三行开始打印所有内容
head -n 3 file.txt: 打印file文件的前三行
head -n -3 file.txt: 打印file文件除了最后三行的所有内容