grep

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

How to delete 5 lines before and 6 lines after pattern match using Sed?

核能气质少年 提交于 2020-01-13 06:02:16
问题 I want to search for a pattern "xxxx" in a file and delete 5 lines before this pattern and 6 lines after this match. How can i do this using Sed? 回答1: This might work for you (GNU sed): sed ':a;N;s/\n/&/5;Ta;/xxxx/!{P;D};:b;N;s/\n/&/11;Tb;d' file Keep a rolling window of 5 lines and on encountering the specified string add 6 more (11 in total) and delete. N.B. This is a barebones solution and will most probably need tailoring to your specific needs. Questions such as: what if there are

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

linux 查进程、杀进程、起进程,常用命令

[亡魂溺海] 提交于 2020-01-12 04:36:09
1.查进程 ps命令查找与进程相关的PID号: ps a 显示现行终端机下的所有程序,包括其他用户的程序。 ps -A 显示所有程序。 ps c 列出程序时,显示每个程序真正的指令名称,而不包含路径,参数或常驻服务的标示。 ps -e 此参数的效果和指定"A"参数相同。 ps e 列出程序时,显示每个程序所使用的环境变量。 ps f 用ASCII字符显示树状结构,表达程序间的相互关系。 ps -H 显示树状结构,表示程序间的相互关系。 ps -N 显示所有的程序,除了执行ps指令终端机下的程序之外。 ps s 采用程序信号的格式显示程序状况。 ps S 列出程序时,包括已中断的子程序资料。 ps -t<终端机编号> 指定终端机编号,并列出属于该终端机的程序的状况。 ps u 以用户为主的格式来显示程序状况。 ps x 显示所有程序,不以终端机来区分。 最常用的方法是ps aux,然后再通过管道使用grep命令过滤查找特定的进程,然后再对特定的进程进行操作。 grep 是搜索 ps -ef | grep java 表示查看所有进程里CMD是java的进程信息 ps -aux | grep java -aux 显示所有状态 ps -ef|grep java|grep -v grep 显示出所有的java进程,去处掉当前的grep进程。 2.杀进程 使用kill命令结束进程:kill

linux 查进程、杀进程、起进程

橙三吉。 提交于 2020-01-12 04:35:09
关键字: linux 查进程、杀进程、起进程 1.查进程 ps命令查找与进程相关的PID号: ps a 显示现行终端机下的所有程序,包括其他用户的程序。 ps -A 显示所有程序。 ps c 列出程序时,显示每个程序真正的指令名称,而不包含路径,参数或常驻服务的标示。 ps -e 此参数的效果和指定"A"参数相同。 ps e 列出程序时,显示每个程序所使用的环境变量。 ps f 用ASCII字符显示树状结构,表达程序间的相互关系。 ps -H 显示树状结构,表示程序间的相互关系。 ps -N 显示所有的程序,除了执行ps指令终端机下的程序之外。 ps s 采用程序信号的格式显示程序状况。 ps S 列出程序时,包括已中断的子程序资料。 ps -t<终端机编号> 指定终端机编号,并列出属于该终端机的程序的状况。 ps u 以用户为主的格式来显示程序状况。 ps x 显示所有程序,不以终端机来区分。 最常用的方法是ps aux,然后再通过管道使用grep命令过滤查找特定的进程,然后再对特定的进程进行操作。 ps aux | grep program_filter_word,ps -ef |grep tomcat ps -ef|grep java|grep -v grep 显示出所有的java进程,去处掉当前的grep进程。 2.杀进程 使用kill命令结束进程:kill xxx 常用

nfs主主模式

倖福魔咒の 提交于 2020-01-11 21:26:05
1.解决了nfs单点故障; 2.使用nfs+keepalived做了主备;   1.当nfs服务器宕机时~主备切换,依据监控keepalived主上的nfs挂了,就停掉主keepalived;依靠keepalived的nfs_check.sh监控(脚本:nfs_check.sh)   nfs_check.sh:     1.监控keepalived是否为主,为主:检查挂载是可用     2.检查此机器上的nfs是否正常,不正常,关闭keepalived   2.通过keepalived的notify_master、notify_backup、notify_fault、notify_stop的4个状态,分别对应的脚本来决定数据的同步方向 3.通过rsync+inotify实现主备间的共享目录进行同步。 VIP:192.168.109.136 master-nfs:192.168.109.137 backup-nfs:192.168.109.138 client:192.168.109.139 nfs与所有挂载机做免密安全认证,为后面判断挂载读写正常检测提供(略) ssh-keygen ssh-copy-id IP 一、master-nfs、backup-nfs 安装nfs服务;client安装客户端(略):参考: https://www.cnblogs.com/NGames/p

【转载】shell基础知识

a 夏天 提交于 2020-01-11 18:33:03
变量的定义: a=1 b=hello c="hello world !" d='hello "反启" !' e=`ls` (注意:这是反引号)   备注:=号左右不能有空格;如果内容有空格需要使用单引号或者双引号;双引号支持转译 $开头的变量会被自动替换 变量替换:   ${value:-word}:如果变量名存在且非null,返回变量值;否则,返回word字符串   用途:如果变量未定义,则返回默认值   ${value:=word}:如果变量名存在且非null,返回变量值;否则,设置这个变量值为word   用途:如果变量未定义,,则设置变量为默认值,并返回默认值   ${value:?"not defined"}:如果变量名存在且非null,返回变量值;否则显示“not defined”,并退出当前命令或脚本。   用途:用于捕捉由于变量未定义而导致的错误,并退出程序。   ${value:+word}:如果变量名存在且非null,返回word,否则返回null。   用途:测试变量是否存在。 变量的使用:   echo $a   echo ${a}   echo "$a"   备注:使用$var 或 ${var}来访问变量,后者更为严谨,$var_x和${var}_x是不同的;变量不需要定义也可以使用,引用未定义的变量,默认为空值。 预定义变量: echo $USER

How to perform a 'grep' search that excludes line starting with “//”and its preceding spaces? [closed]

ⅰ亾dé卋堺 提交于 2020-01-11 13:51:11
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . How to perform a 'grep' search which doesn't display any lines that have the comment marks "//" in the front of the line, but also ignore whitespace in front of the "//" marks? I attempted grep your_search_pattern' where_to_look | grep -v '^// , but it does not ignore lines that have spaces before "//". 回答1:

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 $

how to remove lines from file that don't match regex?

China☆狼群 提交于 2020-01-11 12:44:06
问题 I have a big file that looks like this: 7f0c41d6-f9c6-47aa-a034-d40bc629c973.csv 159890 159891 24faaed6-62ee-4175-8430-5d73b09911c8.csv 159907 5bad221f-25ef-44fa-9086-fd152e697928.csv 642e4ac3-3d46-4b4c-b5c8-aa2fa54d0b04.csv d0e145a5-ceb8-4d4b-ae47-11e0c9a6548d.csv 159929 ba678cbd-af57-493b-a69e-e7504b4bc328.csv 7750840f-9bf9-4a68-9f25-a2ba0968d481.csv 159955 159959 And I'm only interesting in *.csv files, can someone point me how to remove files that do not end with .csv . Thank you. 回答1: