195. Tenth Line -- 第十行

How would you print just the 10th line of a file?

Solution:

awk 'NR==10' 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.

For example, if file.txt has the following content:

name age

alice 21

ryan 30

Output the following:

name alice ryan

age 21 30

Solution:

awk '{
max_nf = NF
max_nr = NR
for (x = 1; x <= NF; x++) {
vector[x, NR] = $x
}
}
END {
for (x = 1; x <= max_nf; x++) {
for (y = 1; y <= max_nr; y++) {
printf("%s", vector[x, y])
if (y < max_nr)
printf(" ")
}
if (x < max_nf)
printf("\n")
}
}' file.txt

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

123 456 7890

(123) 456-7890

Your script should output the following valid phone numbers:

987-123-4567

(123) 456-7890

Solution:

awk '/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/ || /^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$/' file.txt

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

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.

Solution:

awk '{for(i=1;i<=NF;i++) a[$i]+=1} END{for(i in a) print i,a[i] | "sort -r -n -k2"}' words.txt

LeetCode Shell Problems的更多相关文章

  1. leetcode shell

    leetcode 195. 第十行 # | | 第一种是先取出前10行,然后取出最后一行.(但是不足10行,也可以取出最后一行) 正解: tail -n +K :从第K行取出所有 然后取出第一行 le ...

  2. https://oj.leetcode.com/problems/majority-element/

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  3. leetcode https://oj.leetcode.com/problems/jump-game-ii/

    1.超时的,效率太低 public class Solution { public int jump(int[] A) { int len=A.length; int d[]=new int[len] ...

  4. leetcode shttps://oj.leetcode.com/problems/surrounded-regions/

    1.从外围搜索O,深度搜索出现了 Line 35: java.lang.StackOverflowError Last executed input: ["OOOOOOOOOOOOOOOOO ...

  5. https://leetcode.com/problems/palindromic-substrings/description/

    https://www.cnblogs.com/grandyang/p/7404777.html 博客中写的<=2,实际上<=1也是可以的 相当于判断一个大指针内所有子字符串是否可能为回文 ...

  6. [leetcode shell]194. Transpose File

    Given a text file file.txt, transpose its content. You may assume that each row has the same number ...

  7. [leetcode shell]192. Word Frequency

    统计words.txt中每个单词出现的次数并排序 解法1: cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{prin ...

  8. leetcode 新题型----SQL,shell,system design

    leetcode 主要是一个针对北美的coder人群找工作的代码练习网站,我在2015年初次接触这个网站的时候,总共只有200多道题目,是一个类似acm 的a题网站.这些年变化越来越大,主要是因为找工 ...

  9. [LeetCode] Transpose File 转置文件

    Given a text file file.txt, transpose its content. You may assume that each row has the same number ...

随机推荐

  1. HLS入门收集(1)

    使用HLS各种问题 关于求指数函数 exp(x) 在HLS中使用exp(x),也就是指数函数.不能导出RTL到EDK也就是Pcore  只能导出为VIVADO IP:相关解释:见官方论坛 http:/ ...

  2. 安装 android sdk 不能更新问题

    1 要更改host 文件 2在Android SDK Manager的Tool->Option中按照如下修改

  3. 在javascript中如何取消事件冒泡

    如果在javascript中只希望事件发生在它的目标而不是在它的父元素上,即取消它的冒泡事件的发生,该如何做?因为按照javascript发生事件的顺序,它由两个阶段:分别从根元素--父元素--目标元 ...

  4. ASP.NET中的状态保持(转载)

    状态是某一类型的数据在一定时期内保持活跃的信息.这里说的一定时期可以使整个应用程序的生命周期,可以使用户操作程序的时间,当然也可以是单个页面的生命周期等.  为了解决传统Web编程中固有的限制,ASP ...

  5. Oracle存储过程知识汇总

    基本语法篇: CREATE OR REPLACE PROCEDURE 存储过程名 //CREATE OR REPLACE PROCEDURE 是一个SQL语句通知Oracle数据库去创建一个叫做ske ...

  6. IOS绘图——简单三角形

    #import <UIKit/UIKit.h> @interface MyView : UIView @end #import "MyView.h" @implemen ...

  7. PHP函数:生成N个不重复的随机数

    思路:将生成的随机数存入数组,再在数组中去除重复的值,即可生成一定数量的不重复随机数. 程序: <?php /* * array unique_rand( int $min, int $max, ...

  8. 14)Java中Assert

    J2SE 1.4在语言上提供了一个新特性,就是assertion(断言)功能,它是该版本在Java语言方面最大的革新.在软件开发中,assertion是一种经典的调试.测试方式. jvm 断言默认是关 ...

  9. jquery mobile最棘手的一个问题

    大多数jquery mobile开发的妹子们都碰到过这个问题: 如何调用loading效果   这里给出一段代码,赶紧练手吧. //显示loading function showLoading(){ ...

  10. spring debug

    DispatcherServlet{ getHandler()}handlerMappings{ RequestMappingHandlerMapping BeanNameUrlHandlerMapp ...