awk

centos7之sed和awk常用

我怕爱的太早我们不能终老 提交于 2020-01-23 03:57:10
sed 是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。 sed的选项、命令、替换标记 命令格式 sed [options] 'command' file(s) sed [options] -f scriptfile file(s) 常用选项: -n∶使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN的资料一般都会被列出到萤幕上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。 -e∶直接在指令列模式上进行 sed 的动作编辑; -f∶直接将 sed 的动作写在一个档案内, -f filename 则可以执行 filename 内的sed 动作; -r∶sed 的动作支援的是延伸型正规表示法的语法。(预设是基础正规表示法语法) -i∶直接修改读取的档案内容,而不是由萤幕输出。 常用命令: a ∶新增, a 的后面可以接字符串

Changing the case of a string with awk

一曲冷凌霜 提交于 2020-01-22 18:33:27
问题 I'm an awk newbie, so please bear with me. The goal is to change the case of a string such that the first letter of every word is uppercase and the remaining letters are lowercase. (To keep the example simple, "word" is defined here as strictly alphabetic characters; all others are considered separators.) I learned a nice way to make the first letter of every word uppercase from another post on this website using the following awk command: echo 'abce efgh ijkl mnop' | awk '{for (i=1;i <= NF;i

sys_check

橙三吉。 提交于 2020-01-22 17:31:24
#!/bin/bash # auth:kaliarch # func:sys info check # version:v1.0 # sys:centos6.x/7.x [ $(id -u) -gt 0 ] && echo "请用root用户执行此脚本!" && exit 1 sysversion=$(rpm -q centos-release|cut -d- -f3) line="-------------------------------------------------" [ -d logs ] || mkdir logs sys_check_file="/var/log/$(ip a show dev ens192|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}')-`date +%Y%m%d`.txt" # 获取系统cpu信息 function get_cpu_info() { Physical_CPUs=$(grep "physical id" /proc/cpuinfo| sort | uniq | wc -l) Virt_CPUs=$(grep "processor" /proc/cpuinfo | wc -l) CPU_Kernels=$(grep "cores" /proc/cpuinfo|uniq|

How can I count the frequency of letters

柔情痞子 提交于 2020-01-22 16:54:21
问题 I have a data like this >sp|Q96A73|P33MX_HUMAN Putative monooxygenase p33MONOX OS=Homo sapiens OX=9606 GN=KIAA1191 PE=1 SV=1 RNDDDDTSVCLGTRQCSWFAGCTNRTWNSSAVPLIGLPNTQDYKWVDRNSGLTWSGNDTCLYSCQNQTKGLLYQLFRNLFCSYGLTEAHGKWRCADASITNDKGHDGHRTPTWWLTGSNLTLSVNNSGLFFLCGNGVYKGFPPKWSGRCGLGYLVPSLTRYLTLNASQITNLRSFIHKVTPHR >sp|P13674|P4HA1_HUMAN Prolyl 4-hydroxylase subunit alpha-1 OS=Homo sapiens OX=9606 GN=P4HA1 PE=1 SV=2

change a line with awk

冷暖自知 提交于 2020-01-22 13:59:45
问题 Im trying to make a substitution of a single line in a file with awk, for example changing this: e1 is (on) e2 is (off) to: e1 is (on) e2 is (on) use command: awk '/e2/{gsub(/off/, "on")};{print}' ~/Documents/Prueba > ~/Documents/Prueba this makes the substitution but the file ends blank! 回答1: Another answer, using a different tool (sed, and the -i (in place) flag) sed -i '/e2/ s/off/on/' ~/Documents/Prueba 回答2: Your awk is correct, however you are redirecting to the same file as your

三剑客-sed awk

谁都会走 提交于 2020-01-22 13:47:04
目录 1.sed 2.awk 1.sed //sed命令 #语法说明:命令 参数 条件+处理= (指令) 处理文件信息 字符流编辑工具(行编辑工具)==按照每行中的字符进行处理操作 1). 擅长对行进行操作处理 2). 擅长将文件的内容信息进行修改调整/删除 3). 指令信息 p print 输出信息 i insert 插入信息,在指定信息前面插入新的信息 a append 附加信息,在指定信息后面附加新的信息 d delete 删除指定信息 s substitute 替换信息 s###g(全局替换) c 替换修改指定的一整行信息 4). 参数信息 -n 取消默认输出 -r 识别扩展正则 -i 真实编辑文件(将内存中的信息覆盖到磁盘中) -e 识别sed命令多个操作指令 1.文件中添加信息的能力 2.文件中删除信息的能力 1)删除单行信息 2)删除多行信息 3)删除有xx信息的行 4)删除第n行和第m行 5)取消空行显示 3.文件中修改信息的能力 1)修改信息 2)取出特定内容 3)进行备份 4)ni参数 4.文件中查询信息的能力 1)根据行号查询 a)显示单行信息 b)显示多行信息(连续) c)显示多行信息(不连续) 2)根据内容查询 a)输出单行消息 b)输出多行消息(连续) c)输出多行消息(不连续) [root@lbz01 ~]# cat >person.txt<<EOF

awk - split only by first occurrence

时光怂恿深爱的人放手 提交于 2020-01-22 13:43:08
问题 I have a line like: one:two:three:four:five:six seven:eight and I want to use awk to get $1 to be one and $2 to be two:three:four:five:six seven:eight I know I can get it by doing sed before. That is to change the first occurrence of : with sed then awk it using the new delimiter. However replacing the delimiter with a new one would not help me since I can not guarantee that the new delimiter will not already be somewhere in the text. I want to know if there is an option to get awk to behave

awk - split only by first occurrence

試著忘記壹切 提交于 2020-01-22 13:43:08
问题 I have a line like: one:two:three:four:five:six seven:eight and I want to use awk to get $1 to be one and $2 to be two:three:four:five:six seven:eight I know I can get it by doing sed before. That is to change the first occurrence of : with sed then awk it using the new delimiter. However replacing the delimiter with a new one would not help me since I can not guarantee that the new delimiter will not already be somewhere in the text. I want to know if there is an option to get awk to behave

How do I convert a tab-separated values (TSV) file to a comma-separated values (CSV) file in BASH?

二次信任 提交于 2020-01-22 12:54:10
问题 I have some TSV files that I need to convert to CSV files. Is there any solution in BASH, e.g. using awk , to convert these? I could use sed , like this, but am worried it will make some mistakes: sed 's/\t/,/g' file.tsv > file.csv Quotes needn't be added. How can I convert a TSV to a CSV? 回答1: Update : The following solutions are not generally robust , although they do work in the OP's specific use case; see the bottom section for a robust, awk -based solution . To summarize the options

How do I convert a tab-separated values (TSV) file to a comma-separated values (CSV) file in BASH?

拜拜、爱过 提交于 2020-01-22 12:53:27
问题 I have some TSV files that I need to convert to CSV files. Is there any solution in BASH, e.g. using awk , to convert these? I could use sed , like this, but am worried it will make some mistakes: sed 's/\t/,/g' file.tsv > file.csv Quotes needn't be added. How can I convert a TSV to a CSV? 回答1: Update : The following solutions are not generally robust , although they do work in the OP's specific use case; see the bottom section for a robust, awk -based solution . To summarize the options