awk

How can I find if a row exists in a file and add a column with the filename using awk?

醉酒当歌 提交于 2020-03-05 04:27:26
问题 I'm trying to find if a row in a file already exists in another file, and, in that case, add a column with the filename. File1: CHROM POS REF ALT chr1 10 T A chr1 12 T G chr1 12 T C File2: CHROM POS REF ALT chr1 12 T C chr1 13 A T I want to check if any row in file2 is in file1. Expected output: CHROM POS REF ALT chr1 10 T A chr1 12 T G chr1 12 T C file2 I've tried with this code: `awk -F"\t" 'FNR==NR { seen[$0];next }($0 in seen) { delete seen[$0] }; END{ for (x in seen);$(NF+1)="file";print

How can I find if a row exists in a file and add a column with the filename using awk?

折月煮酒 提交于 2020-03-05 04:27:04
问题 I'm trying to find if a row in a file already exists in another file, and, in that case, add a column with the filename. File1: CHROM POS REF ALT chr1 10 T A chr1 12 T G chr1 12 T C File2: CHROM POS REF ALT chr1 12 T C chr1 13 A T I want to check if any row in file2 is in file1. Expected output: CHROM POS REF ALT chr1 10 T A chr1 12 T G chr1 12 T C file2 I've tried with this code: `awk -F"\t" 'FNR==NR { seen[$0];next }($0 in seen) { delete seen[$0] }; END{ for (x in seen);$(NF+1)="file";print

combining and processing 2 tab separated files in awk and make a new one

只谈情不闲聊 提交于 2020-03-05 04:18:08
问题 I have 2 tab separated files with 2 columns. column1 1 is number and column 2 is ID. like these 2 examples: example file1: 188 TPT1 133 ACTR2 420 ATP5C1 942 DNAJA1 example file1: 91 PSMD7 2217 TPT1 223 ATP5C1 156 TCP1 I want to find the common rows of 2 files based on column 2 (column ID) and make a new tab separated file in which there are 4 columns: column1 is ID (common ID) column2 is the number from file1, column3 is the number from file2 and column4 is the log2 values of ratio of columns

awk

南笙酒味 提交于 2020-03-04 22:58:51
1.awk语法格式 awk [options] 'commands' filename awk [options] -f awk-script-file filenamesBEGIN行处理前动作 {}行处理 END行处理后动作BEGIN{print 1/2} {print "ok"} END{print "Over"}如:awk 'BEGIN{print 1/2} {print "ok"} END{print "Over"; print "Over2"}' /etc/passwd匹配awk 'pattern' filenameawk '/root/' /etc/passwd处理动作awk '{action}' filenameawk -F: '{print $1}' /etc/passwd匹配+处理动作awk 'pattern {action}' filenameawk 'BEGIN{FS=":"} /root/ {print $1,$3}' /etc/passwd加判断语句df | awk '/\boot/ {if ($2 < 300000) {print "1"} else {print "2"}}' 2. awk的工作原理 awk将文件中的每一行作为输入,awk会将每一行赋给内部变量$0,以换行符结束 awk开始进行字段分解,每个字段存储在已编号的变量中,从$1开始

Linux之特殊符号与正则表达式

吃可爱长大的小学妹 提交于 2020-03-04 12:21:34
Linux中常用的特殊符号 '' 所见即所得,吃啥吐啥 "" 特殊符号会被解析运行 `` ==== $() 先运行里面的命令 把结果留下 > 重定向符号 先清空文件的内容 然后追加文件的最后 >> 追加重定向 追加文件的最后 2> 错误重定向 只有错误的信息 才会通过这个漏洞进入文件中 2>> 错误追加重定向 ~ 当前用户的家目录 root ~ /root oldboy ~ /home/oldboy ! 查找并运行历史命令 !awk 包含awk的命令 最近的一条运行 history |grep awk # 注释 root用户的命令提示符 $ 取出变量的内容 awk $取某一列的内容 普通用户的命令提示符 * 所有 任何东西 \ 撬棍 转义字符 && 前一个命令执行成功然后在执行后一个命令 ifdown eth0 && ifup eth0 || 前一个命令支持失败了再执行后面的命令 通配符 通配符是用来查找文件的。如:‘*.txt’ 表示匹配所有以 . txt结尾的文件##1. * 所有,任意 找出文件名包含oldboy的文件 mkdir -p /oldboy cd /oldboy touch oldboy.txt oldboy oldboyfile oldboy.awk eduoldboy [root@oldboyedu01-nb oldboy]# find /oldboy/

Multiple Field separator in awk script

拥有回忆 提交于 2020-03-04 08:40:12
问题 i have following code that gives me output as number of lines and words in a file. how can i use one more FS(file separator) that can be used to count total characters.?? (the output should be same as wc file command ) BEGIN { FS="\n| "; } { for(i=1;i<=NF;i++) w++ l++ } END { print "Total no of Lines:"l; print "Total no of words:"w; } 回答1: You can use the built in variable "$0" and function "length" BEGIN { FS="\n| "; } { for(i=1;i<=NF;i++) w++ l++ c += length($0)+1 } END { print "Total no of

常用AWK命令

左心房为你撑大大i 提交于 2020-03-03 23:32:18
常用AWK命令 Awk is a programming language which allows easy manipulation of structured data and the generation of formatted reports. Awk stands for the names of its authors Aho , Weinberger , and Kernighan . 一、什么是AWK USAGE: Unix: awk '/pattern/ {print "$1"}' # standard Unix shells DOS/Win: awk '/pattern/ {print "$1"}' # okay for DJGPP compiled awk "/pattern/ {print \"$1\"}" # required for Mingw32 二、AWK 查找/Search 与 替换/Substitute 参考: 1、 Awk One-Liners Explained, Part II: Text Conversion and Substitution 2、 awk cheat sheet 三、AWK编程 REFER: 使用AWK去掉重复的单词 參考: 1、 The GNU Awk User’s Guide 2、 8 Powerful Awk

[100]第三波常用命令

梦想的初衷 提交于 2020-03-02 08:19:29
用到的时候措手不及,不用的时候一大坨. 基于这个原因,打算重整旗鼓,经常用到的命令和栗子整理如下 像是割草一样,我不信搞不彻底.搞不顺手. find+xargs/sed&sed后向引用+awk多匹配符+过滤行绝招总结&&产生随机数 sort-uniq awk运算-解决企业统计pv/ip问题 1.mkdir 2.ls -l -d 显示目录 -F 给文件夹结尾加/标识 -a 以.开头的都是隐藏文件 -rt 按照修改时间倒序排列(最新修改的在最下) ls -lrth 3.cd 4.pwd 5.touch 6.vi 7.vim 8.echo 配合 > >> -n 不换行 -e 内容携带转义(\n回车 \t tab) - 不换行 [root@n6 ~]# echo -n '123' 123[root@n6 ~]# - 让\n等转义 默认是: [root@n6 ~]# echo 'mao\ntai' mao\ntai 加-e后 [root@n6 ~]# echo -e 'mao\ntai' mao tai 9.cat -n 显示行号 10.xargs: http://man.linuxde.net/xargs -n max-args 多少个一组,默认是1 -i [replace-str] 后向引用 - 用法展示 echo stu{1..20}|xargs -n 2 > 2.md - 单行输出

awk分析nginx日志里面的接口响应时间

杀马特。学长 韩版系。学妹 提交于 2020-03-02 04:33:25
最近,有客户反应客户端卡,老板集合技术人员开会讨论,找出慢的原因,由此产生了分析nginx响应时间,由于线上环境nginx日志格式带上了引号,处理起来有点麻烦,以下是处理过程 一、nginx日志格式 log_format main ‘$remote_addr – $remote_user [$time_iso8601] “$request” ‘ ‘$status $body_bytes_sent “$http_referer” ‘ ‘”$http_user_agent” “$http_x_forwarded_for” ‘ ‘ “$upstream_addr” “$upstream_status” “$request_time” ‘; 二、nginx访问日志 (/var/log/ nginx ) 12.124.127.44 – – [29/Jul/2014:20:54:20 +0800] “GET / HTTP/1.1″ 200 211 “-” “Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)” “-” “127.0.0.1:8081″ “200” “0.001” 115.29.113.101 – – [29/Jul/2014:20:54:22 +0800] “GET / HTTP

(9)awk getline用法详解

岁酱吖の 提交于 2020-03-01 17:28:06
getline用法详解 除了可以从标准输入或非选项型参数所指定的文件中读取数据,还可以使用getline从其它各种渠道获取需要处理的数据,它的用法有很多种。 getline的返回值: 如果可以读取到数据,返回1 如果遇到了EOF,返回0 如果遇到了错误,返回负数。如-1表示文件无法打开,-2表示IO操作需要重试(retry)。在遇到错误的同时,还会设置 ERRNO 变量来描述错误 为了健壮性,getline时强烈建议进行判断。例如: 上面的getline的括号尽量加上,因为 getline < 0 表示的是输入重定向,而不是和数值0进行小于号的比较。 无参数的getline getline无参数时,表示从当前正在处理的文件中立即读取下一条记录保存到$0中,并进行字段分割,然后继续执行后续代码逻辑。 此时的getline会设置NF、RT、NR、FNR、$0和$N。 next也可以读取下一行。 getline:读取下一行之后,继续执行getline后面的代码 next:读取下一行,立即回头awk循环的头部,不会再执行next后面的代码 它们之间的区别用伪代码描述,类似于: # next exec 9<> filename while read -u 9 line;do ...code... continue # next ...code... # 这部分代码在本轮循环当中不再执行