awk

Insert multiple lines into a file after specified pattern using shell script

泪湿孤枕 提交于 2020-01-09 06:05:05
问题 I want to insert multiple lines into a file using shell script. Let us consider my input file contents are: input.txt: abcd accd cdef line web Now I have to insert four lines after the line 'cdef' in the input.txt file. After inserting my file should change like this: abcd accd cdef line1 line2 line3 line4 line web The above insertion I should do using the shell script. Can any one help me? 回答1: Another sed , sed '/cdef/r add.txt' input.txt input.txt: abcd accd cdef line web add.txt: line1

awk

北城以北 提交于 2020-01-08 11:43:52
awk grep和egrep:文本过滤 sed:流编辑器,实现编辑的 awk:文本报告生成器,实现格式化文本输出 awk是一种优良的文本处理工具, linux和unix环境中现有的功能最强大的数据处理引擎之一 awk是三个人的姓的缩写 在linux下的awk实际上是gawk(gun awk) [root@localhost ~]# which awk /usr/bin/awk [root@localhost ~]# ll /usr/bin/awk lrwxrwxrwx. 1 root root 4 Oct 24 01:54 /usr/bin/awk -> gawk 语法格式 任何awk语句都是由 模式 和 动作 组成,一个awk脚本可以有多个语句 模式决定动作语句的触发条件和触发时间 模式 正则表达式 /root/ 匹配含有root的行 关系表达式 < > && || + * 匹配表达式 ~ !~ 动作 变量 命令 内置函数 流控制语句 语法结构 awk [options] 'BEGIN{ print 'start' } pattern{ commands } END{ print 'end' }' file BEGIN和END是awk的关键字,因此必须大写 BEGIN语句设置计数和打印头部信息,在任何动作之前进行 END语句输出统计结果,在完成动作之后执行 awk工作的三个步骤

Linux中AWK基础

蹲街弑〆低调 提交于 2020-01-08 03:47:47
AWK是一个强大的文本分析工具,算是Linux系统特别有用的命令了,在日志分析、文件内容分析中扮演特别重要的角色。 AWK说明 简单来说awk就是把文件逐行的读入,以指定的分隔符将每行分割,分割后的部分再进行各种分析处理。 先看下AWK的命令的说明 内置变量 说明 $0 当前记录(这个变量中存放着整个行的内容) $1 $n 当前记录的第n个字段,字段间由FS分隔 FS 输入字段分隔符 默认是空格或Tab NF 当前记录中的字段个数,就是有多少列 NR 已经读出的记录数,就是多少行 FNR 当前记录数,与NR不同的是,这个值会是各个文件自己的行号 RS 输入的记录分隔符, 默认为换行符 OFS 输出字段分隔符, 默认也是空格 ORS 输出的记录分隔符,默认为换行符 FILENAME 当前输入文件的名字 AWK使用 看下网站access.log。 tail -f /home/wwwlogs/access.log 148.70.179.32 - - [15/Nov/2019:05:46:28 +0800] "POST /wp-cron.php?doing_wp_cron=1573767987.5338680744171142578125 HTTP/1.1" 200 31 "http://www.test.com.cn/wp-cron.php?doing_wp_cron

系统巡检

被刻印的时光 ゝ 提交于 2020-01-07 20:55:05
#!/bin/bash ** 系统信息 ** getsys(){ #系统类型 os_type= uname #系统版本 os_ver= cat /etc/redhat-release #系统内核 os_ker= uname -a|awk '{print $3}' #当前时间 os_time= date +%F_%T #运行时间 os_run_time= uptime |awk '{print $3,$4}'|awk -F ',' '{print $1}' #最后重启时间 os_last_reboot= who -b|awk '{print $3}' #本机名称 os_hostname= hostname echo “系统类型: ${os_type}” echo “系统版本: ${os_ver}” echo “系统内核: ${os_ker}” echo “当前时间: ${os_time}” echo “运行时间: ${os_run_time}” echo “最后重启时间: ${os_last_reboot}” echo “本机名称: ${os_hostname}” } main(){ getsys } main 网络信息 getnet(){ ipaddr=( ifconfig |grep -w inet|awk '{print $2}' ) echo “本机的ip地址:$

使用awk命令筛选日志文件中执行时间超过200ms的SQL日志信息

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-07 20:40:36
以后再面试的时候,如果有面试官再问我这个问题:“请说一下你常用的linux命令”,那么 awk 就可以作为答案之一了。 日志中要筛选的具体日志信息如下,其中包含了mapper以及SQL执行的时间 2019-12-20 15:05:14.133 29958268 [http-nio-8080-exec-64] INFO com.simple.page.MyBatisSqlCostInterceptor.intercept[46] - []The SQL request method is : [com.simple.module.infopanel.mapper.InfoPanelMapper.getSimStatQuery],execute the SQL cost 457 ms 现在就是要把日志所有执行时间超过200ms的筛选出来。 具体的脚本如下: cat service.log | awk '/execute the SQL cost ([0-9]+) ms/' | awk '{costms=gensub(/.+execute the SQL cost ([0-9]*) ms/,"\\1",1);if(costms+0 > 200) print NR, $0}' >> out.log 简单解释一下: cat service.log 读取日志文件,使用管道命令把读取的内容作为

Error in shell script and how to write to a file [closed]

℡╲_俬逩灬. 提交于 2020-01-07 09:56:07
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . I am writing a shell script that is extracting the data from a command: I have tried running the script in both the vi and vim editor. But everything in vain. Please help me out. And how write the output of this in a file. It may be noted that this is just a starting point so the script will

multiple commands inside a awk

China☆狼群 提交于 2020-01-07 09:23:03
问题 sry opening a new thread but awk is driving me nuts! >< im trying to run a few command assignments inside a single awk but i cant get it to work please help if this is ez for u :P i can't get the syntax to work edit: im using /bin/bash for f in `seq $nlinpE $loopE`; do awk -F ","' BEGIN {} '$f' { dataI2[$f]=$2; dataI3[$f]=$3; dataI4[$f]=$4; noD1[$f]=$dataI1[$f]; noD2[$f]=$dataI2[$f]; noD3[$f]=$dataI3[$f]; noD1i[$f]=`echo "$nlinpN1 + $dataI1"|bc -l`; noD2i[$f]=`echo "$nlinpN1 + $dataI2"|bc -l`

multiple commands inside a awk

梦想与她 提交于 2020-01-07 09:22:06
问题 sry opening a new thread but awk is driving me nuts! >< im trying to run a few command assignments inside a single awk but i cant get it to work please help if this is ez for u :P i can't get the syntax to work edit: im using /bin/bash for f in `seq $nlinpE $loopE`; do awk -F ","' BEGIN {} '$f' { dataI2[$f]=$2; dataI3[$f]=$3; dataI4[$f]=$4; noD1[$f]=$dataI1[$f]; noD2[$f]=$dataI2[$f]; noD3[$f]=$dataI3[$f]; noD1i[$f]=`echo "$nlinpN1 + $dataI1"|bc -l`; noD2i[$f]=`echo "$nlinpN1 + $dataI2"|bc -l`

Creating Pivot table in UNIX

久未见 提交于 2020-01-07 08:00:27
问题 Below is my input data, I'm trying create to a Pivot table. input.txt ID,CreateDate,Category,Region,PublishDate,Code,Listing,Type,ModifiedDate FRU426131598,22-Aug-16,SELLING,COUNTRY,22-Aug-16,1,SAMPLE,GRAPE,22-Aug-16 FRU426175576,23-Aug-16,SELLING,COUNTRY,23-Aug-16,1,SAMPLE,APPLE,23-Aug-16 FRU427163049,26-Aug-16,SELLING,COUNTRY,26-Aug-16,1,SAMPLE,APPLE,26-Aug-16 FRU427163049,26-Aug-16,SELLING,COUNTRY,26-Aug-16,1,SAMPLE,APPLE,26-Aug-16 FRU427163049,26-Aug-16,SELLING,COUNTRY,26-Aug-16,1,SAMPLE

Adding a new column to a CSV file

放肆的年华 提交于 2020-01-07 06:40:21
问题 I have a CSV file that looks like this: "12345","TestTest","1.2","TestTest " I want to add a date such as YYYY-MM-DD HH:MM:SS to either the beginning or the end of the CSV file. As you can see in the example above, the double quote (") encases the values on the columns and the comma (,) delineates each column. The problem is the double quote at the end of the line is always on a new line. This means that when I've tried to use sed to search/replace based on a single double quote, my