awk

Awk基本入门[3] Awk Variables and Operators

落花浮王杯 提交于 2020-02-15 01:02:44
1、Variables You don't need to declare an variable to use it. If you wish to initialize an awk variable, it is better to do it in the BEGIN section, which will be executed only once. There are no data types in Awk. Whether an awk variable is a number or a string depends on the context in which the variable is used in. employee-sal.txt sample file employee-sal.txt is a comma delimited file that contains 5 employee records in the following format: $ vi employee-sal.txt 101,John Doe,CEO,10000 102,Jason Smith,IT Manager,5000 103,Raj Reddy,Sysadmin,4500 104,Anand Ram,Developer,4500 105,Jane Miller

Awk基本入门[6] Additional Awk Commands 5

做~自己de王妃 提交于 2020-02-14 19:00:58
1、System Function You can use the system built-in function to execute system commands. Please note that there is a difference between two way communication and system command. In "|&", you can pass the output of any awk command as input to an external command, and you can receive the output from the external command in your awk program (basically it is two way communication). Using the system command, you can pass any string as a parameter, which will get executed exactly as given in the OS command line, and the output will be returned (which is not same as the two way communication). The

awk

拜拜、爱过 提交于 2020-02-14 02:18:07
内容来自链接 : http://blog.chinaunix.net/uid-23302288-id-3785105.html awk是行处理器 : 相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢出或是处理缓慢的问题,通常用来格式化文本信息 awk处理过程: 依次对每一行进行处理,然后输出 awk命令形式: awk [-F|-f|-v] ‘BEGIN{} //{command1; command2} END{}’ file [-F|-f|-v] 大参数,-F指定分隔符,-f调用脚本,-v定义变量 var=value ' ' 引用代码块 BEGIN 初始化代码块,在对每一行进行处理之前,初始化代码,主要是引用全局变量,设置FS分隔符 // 匹配代码块,可以是字符串或正则表达式 {} 命令代码块,包含一条或多条命令 ; 多条命令使用分号分隔 END 结尾代码块,在对每一行进行处理之后再执行的代码块,主要是进行最终计算或输出结尾摘要信息 特殊要点: $0 表示整个当前行 $1 每行第一个字段 NF 字段数量变量 NR 每行的记录号,多文件记录递增 FNR 与NR类似,不过多文件记录不递增,每个文件都从1开始 \t 制表符 tab键 \n 换行符 FS BEGIN时定义分隔符 RS 输入的记录分隔符, 默认为换行符(即文本是按一行一行输入) ~ 匹配,与==相比不是精确比较 !~

Linux命令——awk

泪湿孤枕 提交于 2020-02-14 01:45:07
awk是一个功能强大的编辑工具,逐行读取输入文本,并根据指定的匹配模式进行查找,对符合条件的内容进行格式化输出或者过滤处理,可以在无交互的情况下实现相当复杂的文本操作,被广泛应用于Shell脚本,完成各种自动化配置任务。 一、awk命令的语法及概述 awk 选项 '模式或条件 { 编辑指令 }' 文件1 文件2 … //过滤并输出文件符合条件的内容 awk -f 脚本文件 文件1 文件2 … //从脚本中调用编辑指令,过滤并输出内容 awk执行结果可以通过print的功能将字段数据打印显示。在使用awk命令的过程中,可以使用逻辑操作符“&&”和“||”; 也可以进行简单的数学运算,如+ 、-、、/、%、^分别表示加、减、乘、除、取余、乘方。 awk从输入文件或者标准输入中读入信息,与sed一样,信息的读入也是逐行读取的。不同的是,awk命令将文本文件中的一行视为一个记录,而将一行中的某一部分(列)作为记录的一个字段。为了操作这些不同的字段(列),awk借用shell中类似于位置变量的方法,用$1、$2…$9顺序的表示不同列,$0表示整行。不同字段与不同字段可以通过指定的方式进行分隔,awk默认的分隔符是空格。awk命令允许使用“-F分隔符”的形式来指定分隔符。 二、awk命令用法示例 1)按行输出文本 1、输出所有内容 raoyi@Linux:~$ awk '{print}'

限制ssh远程登陆

假装没事ソ 提交于 2020-02-10 05:01:31
超过十次,就添加到hosts.deny里面去 #!/bin/bash date=`date +%Y%m%d` file="/var/log/secure" max=10 if [[ -f $file ]] then grep Failed $file | awk '{print $(NF-3)}' | sort -rn | uniq -c | awk '{print $2 "=" $1}'>/shell/ip.txt fi for a in `cat /shell/ip.txt` do if [[ `echo $a| awk -F"=" '{print $2}'` -gt $max ]] then b=`echo $a | awk -F"=" '{print $1}'` grep $b /etc/hosts.deny >/dev/null if [[ $? != 0 ]] then echo "sshd:$b" >> /etc/hosts.deny fi fi done 来源: https://www.cnblogs.com/GXLo/p/5169172.html

[NoSQL]-mongodb-分片

不羁岁月 提交于 2020-02-09 13:05:24
第1章 分片的概念 1.有了副本集,为什么还需要分片? 副本集资源利用率不高 分片可以提高资源利用率 2.分片的缺点 理想情况下需要机器比较多 配置和运维变得复杂且困难 提前规划好特别重要,一旦建立后在想改变架构变得很困难 第2章 分片工作流程 1.路由服务mongos 路由服务,提供代理,替用户去向后请求shard分片的数据 2.数据节点shard 负责处理数据的节点,每个shard都是分片集群的一部分 3.分片配置信息服务器config 保存数据分配在哪个shard上 保存所有shard的配置信 提供给mongos查询服务 4.片键 数据存放到哪个shard的区分规则 片键就是索引 选择片键的依据: 能够被经常访问到的字段 索引字段基数够大 第3章 分片的分类 1.区间片键 id name host sex 1 zhang SH boy 2 ya BJ boy 3 yaya SZ girl 如果以id作为片键: id 1-100 shard1 100-200 shard2 200-300 shard3 300-+无穷 shard4 如果以host作为片键: SH shard1 BJ shard2 SZ shard3 2.hash片键: 特点:足够平均,足够随机 id name host sex 1 zhang SH boy 2 ya BJ boy 3 yaya SZ girl

How do I use shell variables in an awk script?

…衆ロ難τιáo~ 提交于 2020-02-07 03:08:10
问题 I found some ways to pass external shell variables to an awk script, but I'm confused about ' and " . First, I tried with a shell script: $ v=123test $ echo $v 123test $ echo "$v" 123test Then tried awk: $ awk 'BEGIN{print "'$v'"}' $ 123test $ awk 'BEGIN{print '"$v"'}' $ 123 Why is the difference? Lastly I tried this: $ awk 'BEGIN{print " '$v' "}' $ 123test $ awk 'BEGIN{print ' "$v" '}' awk: cmd. line:1: BEGIN{print awk: cmd. line:1: ^ unexpected newline or end of string I'm confused about

Linux三剑客

匆匆过客 提交于 2020-02-07 02:46:48
一、Linux三剑客-Sed 1、Sed是什么 Sed:字符流编辑器,Stream Editor 2、Sed功能与版本 处理日志文件,日志,配置文件等 增加、删除、修改、查询 sed --version 可以通过man sed 来检验系统中有没有安装sed [root@luffy-001 ~]# sed --version GNU sed version 4.2.1 Copyright (C) 2009 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, to the extent permitted by law. GNU sed home page: <http://www.gnu.org/software/sed/>. General help using GNU software: <http://www.gnu.org/gethelp/>. E-mail bug reports to: <bug-gnu-utils@gnu.org>. Be sure to

【shell】分割字符串循环输出,dash和bash的不同写法

[亡魂溺海] 提交于 2020-02-06 17:38:27
1. dash的写法: 使用awk string= "abc,def,h" for var in $ ( echo $ { string } | awk '{split($0,arr,",");for(i in arr) print arr[i]}' ) do echo $ { var } done 2. bash可以这样写: string= "abc,def,h" OLD_IFS=” $IFS ” IFS=” , ” arr= ( $ { string } ) IFS=” $OLD_IFS ” for var in $ { arr [ @ ] } do echo $ { var } done 或 string= "abc,def,h" arr= ( $ { string/ / , / } ) for var in $ { arr [ @ ] } do    echo $ { var } done 以上方法输出都为: abc def h 来源: CSDN 作者: 小时hour 链接: https://blog.csdn.net/c753575/article/details/104196869

Print line with match and line above that matches another pattern

佐手、 提交于 2020-02-06 08:01:07
问题 I have a file with certain IDs. ID.txt aaa bbb ccc I have a another file like this File.txt Query: ABC1 aaa abc bbb ccc Query: CAB1 bbb ccc abc Query: CBB1 ass aaa bbc **Expected output:** Query: ABC1 aaa bbb ccc Query: CAB1 bbb ccc Query: CBB1 aaa Real example: **IDs** LYSC_CHICK LACB_BOVIN B5B0D4_BOVIN DEF1_ARAHY DEF2_ARAHY DEF3_ARAHY TRFL_BOVIN Q0PKR4_ARAHY Q0GM57_ARAHY Q647G5_ARAHY Q6JYQ7_HEVBR AMP2_FAGES **File** Query: PROKKA_00022 hypothetical protein - 36 aa Hit: AMP1_FAGES UniProt