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 | # Read from the file words.txt and output the word frequency list to stdout. |
cat : 取得文件内容;
tr : 转换或删除文件中的字符; -s: 缩减连续重复的字符成制定的单个字符。
uniq : 重复数据只取一个; -i : 忽略大小写; -c: 进行计数。
sort : 排序; -r: 反向排序
awk: 每次处理一行,处理最小单位为字段。
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 | Read from the file file.txt and output all valid phone numbers to stdout. |
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 | # Read from the file file.txt and print its transposed content to stdout. |
awk命令中NF表示当前记录中的字段个数,也就是列数;
NR表示已读出的行数,编号从1开始;
这个for循环与常规循环不一样,需要循环NR*NF次数;在这边也就是1,2,1,2,1,2这样的情况。
1 | i = 1, s = [name] |
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 | Read from the file file.txt and output the tenth line to stdout. |
tail -n 3 file.txt: 打印file文件的最后三行内容
tail -n +3 file.txt: 从file文件第三行开始打印所有内容
head -n 3 file.txt: 打印file文件的前三行
head -n -3 file.txt: 打印file文件除了最后三行的所有内容