LeetCode Shell 的试题多为文本操作,195. Tenth Line、193. Valid Phone Numbers、192. Word Frequency、194. Transpose File 暂时只有 4 道题,就整合在这一起了
Shell 中文本处理的事情基本 awk
sed
grep
sort
uniq
tail
head
几个命令组合组合就搞定了
195. Tenth Line
大体意思
How would you print just the 10th line of a file?
For example, assume that file.txt has the following content:
Line 1 |
Your script should output the tenth line, which is:
Line 10 |
打印出文本文件中第 10 行的内容。
自己的解法
cat file.txt | tail -n +10 | head -n 1 |
这是学习了:linux 如何显示一个文件的某几行(中间几行) - 香格里拉(^o^)/
别人的解法
方法一:
awk 'NR==10' file.txt |
方法二:
sed -n '10p' file.txt |
方法三:
line=$(cat file.txt | wc -l) # 千万注意,等号前后一定不要有空格 |
Reference: leetcode-195 Tenth Line - 2> /dev/null
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.
For example, assume that file.txt
has the following content:
987-123-4567 |
Your script should output the following valid phone numbers:
987-123-4567 |
匹配有效的电话号码
自己的解法
cat file.txt | grep '^[0-9]\{3\}-[0-9]\{3\}-[0-9]\{4\}$\|^([0-9]\{3\}) [0-9]\{3\}-[0-9]\{4\}$' |
别人的解法
grep -P '^(\(\d{3}\)|\d{3}-)\d{3}-\d{4}$' file.txt |
Reference: LeetCode 193: Valid Phone Numbers – Revo
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.
For example, assume that words.txt
has the following content:
the day is sunny the the |
Your script should output the following, sorted by descending frequency:
the 4 |
Note:
Don’t worry about handling ties, it is guaranteed that each word’s frequency count is unique.
统计单词出现的频次,然后倒序排列
别人的解法
awk '{i=1;while(i<=NF){print $i;i++}}' words.txt | sort | uniq -c | sort -k1nr | awk '{print $2 " " $1}' |
1、利用 awk 默认一行一条记录,默认以空格划分每条记录,NF 为划分的总块数先打印出所有单词
2、排序 + 统计 + 消除重复
3、输出
Reference: LeetCode 192 Word Frequency - wangxiaobupt 的专栏
awk ' |
Reference: leetcode/solutions/192.Word_Frequency at master · illuz/leetcode
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.
For example, if file.txt
has the following content:
name age |
Output the following:
name alice ryan |
别人的解法
awk ' |
Reference: leetcode/solutions/194.Transpose_File at master · illuz/leetcode
总结
Shell 中文本处理的事情基本 awk
sed
grep
sort
uniq
tail
head
几个命令组合组合就搞定了,各命令的常用方法之后总结