Leetcode Shell Problems
Leetcode上增加了4道Shell题目,进行总结
1. 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
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
a. head -10 file.txt | tail -1
错误: 若文件只有9行,会打印出Line 9,但是应该不打印
修改:tail -n +10 file.txt | head -n1 (+10代表从正数10行开始,如果没有就不输出)
同理:若打印倒数第10行:head -n -10 404.html | tail -1
b. if语句实现相同功能:
c. sed -n '10p' file.txt
不带-n的话,不仅打印匹配行,还接着输出file的全部内容。
d. awk 'NR == 10' file.txt
Number of Field, 默认是换行符,就是第几行
2. 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
123 456 7890
(123) 456-7890
Your script should output the following valid phone numbers:
987-123-4567
(123) 456-7890
几种解法:
3. 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
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
解法:
4. 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
alice 21
ryan 30
Output the following:
name alice ryan
age 21 30
解法: