awk

awk命令

烈酒焚心 提交于 2020-01-31 15:47:37
awk是一个强大的报告生成工具,用于格式化文本输出 语法:   awk [options] -f 'program' filename   program由{ pattern + action statements}组成,动作语句之间用分号“;”分隔 选项:   -F:指定输入分隔符   -v VAR=value:自定义变量 常用命令 1、print   print item1,item2,......   item之间用逗号分隔,如果省略item,相当于print $0 2、变量   内置变量     FS:input field seperator,输入分隔符,与-F指定的相同,默认是空白字符     OFS:output field seperator,输出分隔符,默认空白字符 [root@localhost ~]# awk -v FS=: '{print $1}' /etc/passwd root bin daemon adm [root@localhost ~]# awk -F : '{print $1,$3}' /etc/passwd root 0 bin 1 daemon 2 adm 3 [root@localhost ~]# awk -v FS=: -v OFS=" | " '{print $1,$3}' /etc/passwd root | 0 bin | 1

include library of functions in awk

我们两清 提交于 2020-01-31 11:48:10
问题 There are many common functions (especially arithmetic/mathematics) that are not built into awk that I need to write myself all the time. For example: There is no c=min(a,b) , so in awk i constantly write c=a<b?a:b same for maximum i.e. c=max(a,b) same for absolute value i.e. c=abs(a) so i have to constantly write c=a>0?a:-a and so on.... Ideally, I could write these functions into an awk source file, and "include" it into all of my instances of awk, so I can call them at will. I looked into

Select a particular column using awk or cut or perl

纵饮孤独 提交于 2020-01-30 19:58:09
问题 I have a requirement to select the 7th column from a tab delimited file. eg: cat filename | awk '{print $7}' The issue is that the data in the 4th column has multiple values with blank in between. example - The last line in the below output: user \Adminis FL_vol Design 0 - 1 - group 0 FL_vol Design 19324481 - 3014 - user \MAK FL_vol Design 16875161 - 2618 - tree 826 FL_vol Out Global Doc Mark 16875162 - 9618 - /vol/FL_vol/Out Global Doc Mark 回答1: If the data is unambiguously tab-separated,

linux 取得文件行数

梦想的初衷 提交于 2020-01-30 09:23:55
话题相关 #linux 如何取得文件行数 #linux 如何知道一个文件有多少行 #shell 如何取得文件行数 方法1 wc命令 wc -l file wc命令是word count的缩写,为统计指定文件中的字节数、字数、行数,并将统计结果显示输出。 其中, -l 参数是用来统计行数的。 方法2 利用awk,获取最后一行的行号 awk 'END {print NR}' file 效果与例子 比如,sample.txt 文件有16行。 # wc命令输出行数,同时输出文件名 $ wc -l sample.txt 16 sample.txt # awk的方法,因为是输出行号,所以只输出数字 $ awk 'END {print NR}' sample.txt 16 (以上。) 来源: CSDN 作者: Olivia_Vang 链接: https://blog.csdn.net/Olivia_Vang/article/details/104107491

Awk - field separator behavior

馋奶兔 提交于 2020-01-30 08:14:47
问题 I have a file: CreateSec,explorer.exe,\\WINDOWS\\system32\\verclsid.exe,SUCCESS I want to print "$2" Example 1 It works well: $ awk -F '\\\\\\\\' '{print $2}' file WINDOWS Example 2 It works well: $ awk -F '\\\\'+ '{print $2}' file WINDOWS Example 3 Does not print. $ awk -F "\\\\\\\\" '{print $2}' file . Example 4 So it works well: $ echo "CreateSec,explorer.exe,\\WINDOWS\\system32\\verclsid.exe,SUCCESS" | awk -F '\' '{print $2}' WINDOWS -------------------------------------------------------

grep out load average from uptime

*爱你&永不变心* 提交于 2020-01-30 07:44:05
问题 what I want to do is take the command uptime, and get the load averages $ uptime 07:01:30 up 20:29, 2 users, load average: 0.32, 0.39, 0.54 I have a feeling this is something I can do with awk, but I am not quite sure how. pls assist. 回答1: You can use a regex with backreferences: i.e. find any sequence of characters (.*) but only at a point directly after average: uptime | grep -oP '(?<=average:).*' 回答2: You can use grep uptime | grep -o 'load.*' Also you can extract the three load average

AWK Field Separators Vanishing

邮差的信 提交于 2020-01-30 06:07:40
问题 I am trying to use AWK to modify a long SQL script after removing a column from the database. The lines I am trying to modify: INSERT INTO `table` (`one`, `two`, `three`, `four`, `five`, `six`) VALUES ('alpha', 'bravo', 'charlie', 'delta', 'echo', 'foxtrot'); My AWK script: BEGIN { FS="," } /`table`/ { $3=$8=""; print } Which I run with: awk -f script.awk in.sql > out.sql This does ALMOST what I want it to do, except it seems to remove all of the Field Separators(commas): INSERT INTO `table`

AWK Field Separators Vanishing

穿精又带淫゛_ 提交于 2020-01-30 06:06:30
问题 I am trying to use AWK to modify a long SQL script after removing a column from the database. The lines I am trying to modify: INSERT INTO `table` (`one`, `two`, `three`, `four`, `five`, `six`) VALUES ('alpha', 'bravo', 'charlie', 'delta', 'echo', 'foxtrot'); My AWK script: BEGIN { FS="," } /`table`/ { $3=$8=""; print } Which I run with: awk -f script.awk in.sql > out.sql This does ALMOST what I want it to do, except it seems to remove all of the Field Separators(commas): INSERT INTO `table`

模块三、企业实战案例

二次信任 提交于 2020-01-29 01:17:39
十四、企业实战案例 (一)使用for循环在/oldboy目录下批量创建10个html文件,其中文件名包含10个随机小写字母加固定字符串0ldoby。 思路分析,产生随机数字方法: ①核心是创建10个随机小写字母。 echo $RANDOM 范围是0-32767 openssl rand -base64 100 date +%s%N head /dev/urandom/cksum uuidgen cat /proc/sys/kernel/random/uuid mkpasswd (yum install expect -y) -l:长度 -d:数字 -c:小写字母 -C:大写字母 -s:特殊字符 [root@centos6-kvm3 shili]# cat 14-01.sh #!/bin/bash path="oldboy" [ -d /path ] || mkdir -p oldboy/ for n in {1..10} do random=`echo "OLDBOY$RANDOM" | md5sum | tr '0-9' 'm-z' | cut -c 2-11` touch $path/${random}_oldboy.html done [root@centos6-kvm3 shili]# (二) 、批量改名特殊案例。将以上面试题1中结果

Linux Shell常用技巧(五)

不打扰是莪最后的温柔 提交于 2020-01-28 19:25:40
十一. awk编程: 1. 变量: 在awk中变量无须定义即可使用,变量在赋值时即已经完成了定义。变量的类型可以是数字、字符串。根据使用的不同,未初始化变量的值为0或空白字符串" ",这主要取决于变量应用的上下文。下面为变量的赋值负号列表: 符号 含义 等价形式 = a = 5 a = 5 += a = a + 5 a += 5 -= a = a - 5 a -= 5 *= a = a * 5 a *= 5 /= a = a / 5 a /= 5 %= a = a % 5 a %= 5 ^= a = a ^ 5 a ^= 5 /> awk '$1 ~ /Tom/ {Wage = $2 * $3; print Wage}' filename 该命令将从文件中读取,并查找第一个域字段匹配Tom的记录,再将其第二和第三个字段的乘积赋值给自定义的Wage变量,最后通过print命令将该变量打印输出。 /> awk ' {$5 = 1000 * $3 / $2; print}' filename 在上面的命令中,如果$5不存在,awk将计算表达式1000 * $3 / $2的值,并将其赋值给$5。如果第五个域存在,则用表达式覆盖$5原来的值。 我们同样也可以在命令行中定义自定义的变量,用法如下: /> awk -F: -f awkscript month=4 year=2011