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
-------------------

# Read from the file file.txt and output the tenth line to stdout.

#Solution One:
#head -n 10 file.txt | tail -n +10 #Solution Two:
#awk 'NR==10' file.txt #Solution Three:
sed -n 10p file.txt

涉及知识点:

->head 用来显示档案的开头至标准输出中,默认head命令打印其相应文件的开头10行。

语法格式:head [参数]... [文件]...

命令参数:

-q 隐藏文件名

-v 显示文件名

-c<字节> 显示字节数

-n<行数> 显示的行数

->tail命令用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理。常用查看日志文件。

命令参数:

-f 循环读取

-q 不显示处理信息

-v 显示详细的处理信息

-c<数目> 显示的字节数

-n<行数> 显示行数

--pid=PID 与-f合用,表示在进程ID,PID死掉之后结束.

-q, --quiet, --silent 从不输出给出文件名的首部

-s, --sleep-interval=S 与-f合用,表示在每次反复的间隔休眠S秒

可参考:我使用过的Linux命令之tail - 输出文件尾部/动态监视文件尾部

->awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大。简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理。

语法格式:

awk '{pattern + action}' {filenames}

pattern 表示 AWK 在数据中查找的内容,而 action 是在找到匹配内容时所执行的一系列命令  

可参考:linux awk命令详解

->sed 是一种在线编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。

语法格式:

sed [-hnV][-e<script>][-f<script文件>][文本文件]

2.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

---------

# Read from the file file.txt and print its transposed content to stdout.
# using awk for this purpose
awk '
{
for(i=1; i<=NF; i++)
{
if(line[i] == "")
{
line[i] = $i
}
else
{
line[i] = line[i]" "$i
}
}
}
END{
for(i=1; i<=NF; i++)
{
print line[i]
}
}
' file.txt

如果The number of columns is  two.则可以用以下方法:

test2

name age
alice 21
ryan 30

solution:

MindeMacBook-Pro:闲杂笔记 minzhu$ cut -d " " -f1 test2 |xargs
name alice ryan
MindeMacBook-Pro:闲杂笔记 minzhu$ cut -d " " -f2 test2 |xargs
age 21 30

  

3.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

------------

file.txt

987-123-4567
123 456 7890
(123) 456-7890

solution1:

grep -e '\(^[0-9]\{3\}-[0-9]\{3\}-[0-9]\{4\}$\)' -e '\(^([0-9]\{3\})[ ]\{1\}[0-9]\{3\}-\([0-9]\{4\}\)$\)'  file.txt

explanation:

  1. In Bash, we use \ to escape next one trailing character;
  2. ^ is used to denote the beginning of a line
  3. $ is used to denote the end of a line
  4. {M} is used to denote to match exactly M times of the previous occurence/regex
  5. (...) is used to group pattern/regex together

Back to this problem: it requires us to match two patterns, for better readability, I used -e and separate the two patterns into two regexes, the first one matches this case: xxx-xxx-xxxx and the second one matches this case: (xxx) xxx-xxxx

solution2:

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

The format for 'awk':
awk < file 'pattern {action}'
or
awk 'pattern {action}' file

Note: 'print' action without any arguments means print out the whole line.

4.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

-----------------  

words.txt

the day is sunny the the
the sunny is is

solution1:

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

solution2:

sed 's/^\s+//g; s/\s+/ /g; s/\s+$//g' words.txt | tr ' ' '\n' | sort | uniq -c | sort -nr | awk -F' ' '{print $2" "$1}'
  1. use sed to strip head & tail spaces,and change inline spaces to one space
  2. use tr to trans space to return (these two steps also can be done cat words.txt | tr -s ' ' '\n')
  3. sort the words
  4. uniq to count words
  5. sort the stats result,-n for numeric sort,-r for reverse
  6. use awk to format the output

  

参考:leetcode  

  

  

  

  

shell of leetcode的更多相关文章

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

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

  2. leetcode shell

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

  3. 刷题中熟悉Shell命令之Tenth Line和Transpose File [leetcode]

    首先介绍题目中要用的4个Shell命令 sed awk head tail的常用方法.(打好地基,才能建成高楼!) sed:(转自:http://www.cnblogs.com/barrychiao/ ...

  4. LeetCode Shell Problems

    195. Tenth Line -- 第十行 How would you print just the 10th line of a file? Solution: awk 'NR==10' file ...

  5. [leetcode shell]194. Transpose File

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

  6. [leetcode shell]192. Word Frequency

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

  7. 【LeetCode】shell

    195. Tenth Line 输出file.txt中的第十行 答案: # Read from the file file.txt and output the tenth line to stdou ...

  8. [LeetCode] Transpose File 转置文件

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

  9. Codewars, Leetcode, Hackerrank. Online Judges Reviews

    http://jasonjl.me/blog/2015/03/30/practical-programming-practice-services/ Codewars, Leetcode, Hacke ...

随机推荐

  1. Shell逻辑语句

    case esac 语句 参考 :https://blog.csdn.net/wu20093346/article/details/47210809 case ... esac 与其他语言中的 swi ...

  2. Jenkins忘记密码解决方案

    # 当jenkins忘记了管理用户的密码时,只能通过修改配置文件并重启的方式初始化设置用户名及密码,操作如下: 找到jenkins的配置目录,笔者的jenkins是下载的war包直接丢在tomcat下 ...

  3. 一点不懂到小白的linux系统运维经历分享

    进入运维行业刚不到二个年头, 刚刚从大白变成小白.都说it行业是青春的饭.但是运维行业可不这么认为.运维工程师便是经验技术的积累,经历的过的沟沟坑坑都会融入你的血液,成为你的智慧. 二年前接触linu ...

  4. 【Codeforces 467D】Fedor and Essay

    Codeforces 467 D 题意:给\(m​\)个单词,以及\(n​\)个置换关系,问将\(m​\)个单词替换多次后其中所含的最少的\(R​\)的数量以及满足这个数量的最短总长度 思路:首先将置 ...

  5. linux下模拟一个木马程序运行过程

    预备知识: 将一个程序放入到后台,悄悄的执行 ./xxx.sh & 进程: 用户进程:由用户来管理 系统进程:由系统内核自行管理 系统中的每个进程,都有一个位置的ID,这就是pid,而且每次启 ...

  6. webstorm破解汉化

    一.下载webstorm软件和汉化包 webstorm安装包下载链接 : 链接: https://pan.baidu.com/s/1VmOPNVL2GRgAb_0tAJhy8A 密码: am7e 汉化 ...

  7. aurora 64B/66B ip核设置与例程代码详解

    见网页https://blog.csdn.net/u014586651/article/details/84349328 https://blog.csdn.net/u012135070/articl ...

  8. C#一些常用的图片操作方法:生成文字图片 合并图片等

    生成文字图片: /// <summary> /// 生成文字图片 /// </summary> /// <param name="text">& ...

  9. 【转】Git版本控制软件从入门到精通学习手册

    GIT 学习手册简介 本站为 Git 学习参考手册.目的是为学习与记忆 Git 使用中最重要.最普遍的命令提供快速翻阅. 这些命令以你可能需要的操作类型划分,并且将提供日常使用中需要的一些常用的命令以 ...

  10. 开源数据同步神器——canal

    前言 如今大型的IT系统中,都会使用分布式的方式,同时会有非常多的中间件,如redis.消息队列.大数据存储等,但是实际核心的数据存储依然是存储在数据库,作为使用最广泛的数据库,如何将mysql的数据 ...