leetcode 的shell部分4道题整理

对shell的某些细节还不是十分熟悉,借鉴了好多别人的东西

1. Word Frequency

此题很简单,只要能排序就可以

cat words.txt |tr -s " " "\n" sort | unique -c | sort -r | awk ‘{print $2" "$1}‘

2.

Valid Phone Numbers

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

3.

Transpose File

cat file.txt| awk ‘{

max_nf = NF

max_nr = NR

for(x=1;x<=max_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")

}

}

4.

Tenth Line

cat file.txt|awk ‘NR==10‘

时间: 2024-08-29 09:48:15

leetcode 的shell部分4道题整理的相关文章

Learning The Bash Shell读书笔记(整理)

最近搞了一本书 Learning Bash Shell,发现有人已经写了阅读笔记,我就在这边整理一下 来自blog:http://blog.sina.com.cn/n4mine Learning The Bash Shell读书笔记(1)bash初识,通配符 Learning The Bash Shell读书笔记(2)重定向,管道,后台 Learning The Bash Shell读书笔记(3)特殊字符,引用,控制键 Learning The Bash Shell读书笔记(4)历史命令 Lea

lunix shell 基础经常使用整理

1   ps  -ef    显示正在执行的进程,pid 等信息 UID PID PPID C STIME TTY TIME CMD root 1 0 0 03:45 ? 00:00:02 init [5] root 2 1 0 03:45 ? 00:00:00 [migration/0] root 3 1 0 03:45 ? 00:00:00 [ksoftirqd/0] root 4 1 0 03:45 ? 00:00:00 [events/0] root 5 1 0 03:45 ? 00:0

Shell脚本使用汇总整理

一.Shell脚本常用的头部格式: 头部的作用就是告知linux此脚本的类型: 常用的头部格式如下:(/bin/bash,是bash的路径,如果不知道路径可以通过which bash进行查看,其它命令的路径也是类似查看的) (1)#!/bin/bash:普通的linux脚本,也是最常用的,不需要交互: (2)#!/usr/bin/expect:可以自动交互的linux脚本,有可能需要安装expect,如果linux服务器上没有此expect命令的话. 二.Shell脚本中如何执行Linux或自定

Shell脚本使用汇总整理——mysql数据库5.7.8以前备份脚本

Shell脚本使用的基本知识点汇总详情见连接: https://www.cnblogs.com/lsy-blogs/p/9223477.html 脚本分为三部分配置信息.脚本文件.定时任务: 1.配置信息: username=rootpassword=123456backupsFileStr=/wocloud/db/backupsbackupsFileDay=3mysqldumpStr=/usr/bin/mysqldumpcopydb=hebei,reportSystem 参数说明: usern

shell技巧(不断整理)

其中的换行符被替换成了空格,若输出时保留换行,应该使用如下命令: echo "${foo}" 2.为不同的主机,指定不同的ssh密钥 Host gitolite-as-alice   HostName git.company.com   User git   IdentityFile /home/whoever/.ssh/id_rsa.alice   IdentitiesOnly yes Host gitolite-as-bob   HostName git.company.com  

PHP数据库45道题整理~~~啦啦啦

select * FROM student-- 二:查询student表中所有记录select * FROM score WHERE Degree>60 AND Degree<80-- 四:查询Score表中成绩在60到80之间的所有记录SELECT * FROM score WHERE Degree in ('85','86','88'); -- 五:查询Score表中成绩为85,86或88的记录.SELECT * FROM student WHERE Class = '95031' AND

Shell操作之细节整理(未完结)

在学习Linux过程中,曾经遇到过一些小问题,虽然可能微不足道,但是在追求细节的时候往往会比较纠结(强迫症犯了),空出一个博客文章空间,记录一些细节上的内容,都是很小很简单的东西,不喜勿喷. 01.bc计算时浮点问题 记的用bc计算数字的时候,如果结果出现小数点,则小数点后内容默认不显示,当时没太在意. [[email protected] wangdong]# echo "1500/1024" | bc 1 [[email protected] wangdong]#  [[email

leetcode处女作

闲来无事[真的吗?你确定→_→ 在leetcode上刷了一道题.费时一小时,也是醉了.谨以此文,纪念我的伟大成果.[呵呵 题目是找出非排序数组中缺少的最小正整数.要求时间复杂度O(n),空间复杂度为常数. 首先考虑把数组排序.发现没有排序算法满足要求,放弃. 想到找出数组中最大的数和最小的数,定义k值,从最小的数开始,在数组中依次查找,有对应的即k++,没有,返回k值,即为所求.如果min到max都存在,返回max+1. 期间忘记考虑数组长度为0的情况,报错:比较时的逻辑错误:print语句忘删

[LeetCode] Rotate Function 旋转函数

Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a "rotation function" F on A as follow: F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1]. Ca