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. 音乐社交APP源码 V1.1

    1.关于音乐曲库,对接的是百度音乐,会自动随搜索链接百度曲库2.便捷聊天,采用xmpp基本架构.3.加入和整理了群聊天.4.分布式聊天,喜欢该专辑直接进入聊天,喜欢该音乐的进入聊天.5.采用兴趣社交和 ...

  2. 第一部分 CLR基础:第1章 CLR的执行模型

    1.1将源代码编译成托管模块

  3. 转载字典地址:http://blog.csdn.net/aladdinty/article/details/3591789

    相关文章: http://www.360doc.com/content/13/1003/23/14070959_318861279.shtml http://www.360doc.com/conten ...

  4. PHP判断用户所在国家并跳转对应的目录

    <?php // 淘宝API查询国家代码 $url = "http://ip.taobao.com/service/getIpInfo.php?ip=".get_client ...

  5. Python之MySql操作

    1.安装驱动 输入命令:pip install MySQL-python 2.直接使用驱动 #coding=utf-8 import MySQLdb conn= MySQLdb.connect( ho ...

  6. WIN8+VS2013编写发布WCF之二(部署)

    上文简介了如何建立WCF工程并且调试,下面说一下如何部署. 本文将陆陆续续讲述三种部署方式,随着项目的进展将不断补全. 声明: 用管理员身份打开VS2013,发布前请将程序的.net版本改成与服务器相 ...

  7. ROS多个master消息互通

    需求 有时候我们需要有几个不同的master, 他们之间要交换topic的内容,这时候就不能使用ros自带的设置同一个master的方法. 我们的处理方法是,构造一个client和一个server,他 ...

  8. Python脚本控制的WebDriver 常用操作 <十一> 操作测试对象

    下面将使用WebDriver来模拟键盘的输入操作,以及复习上节的层对象操作 测试用例场景 定位到具体的对象后,我们就可以对这个对象进行具体的操作,比如先前已经看到过的点击操作(click).一般来说, ...

  9. JS中的DOM与BOM

    javascript组成: 1. ECMAScript 基本语法. 2. BOM (浏览器对象模型) 3. DOM (文档对象模型) 一)BOM(borwser Object  Model) 浏览器对 ...

  10. 【转】IT领域技能图谱