awk

How to do a if else match on pattern in awk

亡梦爱人 提交于 2020-01-13 10:02:32
问题 i tried the below command: awk '/search-pattern/ {print $1}' How do i write the else part for the above command ? 回答1: Classic way: awk '{if ($0 ~ /pattern/) {then_actions} else {else_actions}}' file $0 represents the whole input record. Another idiomatic way based on the ternary operator syntax selector ? if-true-exp : if-false-exp awk '{print ($0 ~ /pattern/)?text_for_true:text_for_false}' awk '{x == y ? a[i++] : b[i++]}' awk '{print ($0 ~ /two/)?NR "yes":NR "No"}' <<<$'one two\nthree four

Split text file into multiple files

醉酒当歌 提交于 2020-01-13 08:15:30
问题 I am having large text file having 1000 abstracts with empty line in between each abstract . I want to split this file into 1000 text files. My file looks like 16503654 Three-dimensional structure of neuropeptide k bound to dodecylphosphocholine micelles. Neuropeptide K (NPK), an N-terminally extended form of neurokinin A (NKA), represents the most potent and longest lasting vasodepressor and cardiomodulatory tachykinin reported thus far. 16504520 Computer-aided analysis of the interactions

awk - 提取包含某个关键字的段落

☆樱花仙子☆ 提交于 2020-01-13 00:54:45
前提 AWK是一种处理文本文件的语言,是一个强大的文本分析工具。 本文将使用命令awk将具有某个关键字的段落提取出来。 准备数据 "Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007fb2dc1aa800 nid=0x63f6 in Object.wait() [0x00007fb2be61f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x00000000d6708ed8> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144) - locked <0x00000000d6708ed8> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165) at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java

How can I find all the merges that had conflicts in a Git repository?

邮差的信 提交于 2020-01-12 23:28:22
问题 I am working on a research to study merges in open source projects. I recently asked How can I find all the commits that have more than one parent in a Git repository?, and got very good answers. Now I need to narrow my query to only find the commits that had conflicts. With conflicts I mean that the same file was modified in two contributing commits. Also, it would be very useful if I can find a git or bash (grep, awk?) command that gives me only those commits in which the same line was

awk命令详解

我与影子孤独终老i 提交于 2020-01-12 20:16:45
awk 工作原理和基本用法说明 常见选项: -F “分隔符” 指明输入时用到的字段分隔符 -v var=value 变量赋值 动作:printf 说明: 逗号分隔符 输出item可以字符串,也可是数值;当前记录的字段、变量或awk的表达式 如省略item,相当于print $0 范例: [ root@I | 158 | ~ ] #awk -F: '{print $1,$3}' /etc/passwd root 0 bin 1 daemon 2 adm 3 lp 4 sync 5 shutdown 6 halt 7 mail 8 operator 11 games 12 ftp 14g nobody 99 [ root@I | 160 | ~ ] #awk -F: '{print $1"\t"$3}' /etc/passwd root 0 bin 1 daemon 2 adm 3 lp 4 sync 5 shutdown 6 halt 7 mail 8 operator 11 games 12 ftp 14 [ root@I | 162 | ~ ] #grep "^UUID" /etc/fstab UUID = 7e896ac6-4ee4-4533-ad7e-578c71560735 / xfs defaults 0 0 UUID = a063884c-6421-4581-841b

5 郭晓阳

瘦欲@ 提交于 2020-01-12 03:54:38
第五周 第一天 数组 declare -a title = ( [ 0 ] = "ceo" [ 1 ] = "coo" [ 2 ] = "cto" ) echo ${title[1]} coo echo ${title} ceo echo ${title[2]} cto echo ${title[3]} echo ${title[@]} ceo coo cto echo ${title[*]} ceo coo cto echo ${#title[*]} (数组长度) 3 echo ${title[*]} ceo coo cto unset title [ 1 ] ( 删除数组) echo ${title[*]} ceo cto unset title echo ${title[*]} 数据处理 [ root@centos8 ~ ] #num=({0..10}) [ root@centos8 ~ ] #echo ${num[*]:2:3} (跳过两个取三个) 2 3 4 [ root@centos8 ~ ] #echo ${num[*]:6} (跳过6个) 6 7 8 9 10 [ root@centos8 ~ ] #num[${#num[@]}]=11 (追加元素) [ root@centos8 ~ ] #echo ${#num[@]} 12 [ root@centos8 ~ ]

Nginx日志分析脚本

百般思念 提交于 2020-01-12 01:06:11
运维工作是一个比较复杂的工作,有时候面对上万条的日志,如何作分析?难道一条条的分析? 聪明的人会选择脚本,这就是为什么现在提倡自动化运维的原因吧,废话不多说,直接上脚本。 vim /data/scripts/log_analysis.sh #!/bin/bash ############################################### # Desc :nginx日志分析脚本 # # Author : Bertram # # Date : 2019-12-21 # # Copyright : Personal belongs # ############################################### public(){ echo "" read -p "请输入要分析的访问日志: " log_file echo "" if [ ! -f $log_file ];then echo "未找到: ${log_file}" exit 1 fi if [ ! -s $log_file ];then echo "${log_file}是空文件" exit 1 fi #输出日志访问量排名前top_num条数据,可自定义 top_num=5 input_file=`echo $log_file | awk -F '/' '{print $(NF)}'`

awk基本用法

跟風遠走 提交于 2020-01-11 23:03:19
awk cat /etc/passwd | awk -F: '{print}' 默认全部字段,即列 cat /etc/passwd | awk -F: '{print $1 , $3 }' 以:为分隔符取1,3列 cat /etc/passwd | awk -F: '{print $1 "\t" $3 }' 以:为分隔符取1,3列并且以tab建隔开 df | awk -F "[[:space:]]+|%" '/^\/dev\/sd/{print $5 }' 取分区利用率 cat /etc/Passwd | awk -F ":" -v OFS = " " '/\<0\>/,/\<50\>/{print $1 , $NF }' 以:为分隔符取0-50uid用户的shell并以空格隔开 统计/etc/fstab文件中每个文件系统类型出现的次数 cat /etc/fstab | awk -F " " '/^UUID/{print $3 }' | sort | uniq -c 统计/etc/fstab文件中每个单词出现的次数 cat /etc/fstab | grep -o '\<[a-z]\+\>' | sort | uniq -c | sort -nr 提取出字符串Yd$C@M05MB%9&Bdh7dq+YVixp3vpw中的所有数字 echo "Yd $C@M05MB %9

How to insert name field in mailx

ぐ巨炮叔叔 提交于 2020-01-11 14:43:28
问题 My sample data File is $ cat /fullpath/myfile.csv a@gmail.com, A Singh k@gmail.com, K Singh I am using script.sh #!/bin/bash while IFS= read -r line do email=$(echo $line | awk -F, '{print $1 }') name=$(echo $line | awk -F, '{print $2 }') echo | mailx -v -s "Helo $name" -S smtp-use-starttls -S ssl-verify=ignore -S smtp-auth=login -S smtp=smtp://smtp.gmail.com:587 -S from="xxxx@gmail.com(John Smith)" -S smtp-auth-user=xxxx@gmail.com -S smtp-auth-password=xxxxpassword -S ssl-verify=ignore -S

Grabbing specific sections of text from a string

↘锁芯ラ 提交于 2020-01-11 12:52:09
问题 I've got a string that I need to grab two section out - they vary as it's a PHP language file. Hoping someone can help, the string is: $_LANG['FIELD1'] = "FIELD2"; I need to grab FIELD1 and FIELD2 回答1: This should do it: # space seperated $ sed -n "s/.*_LANG\['\([^']*\)'] = .\(\w*\).*/\1 \2/p" file FIELD1 FIELD2 # newline seperated $ sed -n "s/.*_LANG\['\([^']*\)'] = .\(\w*\).*/\1\n\2/p" file FIELD1 FIELD2 Or using grep with positive lookbehind: $ grep -Po "(?<=_LANG\[')[^']*" file FIELD1 $