awk

Awk基本入门[4] Awk Conditional Statements and Loops

≡放荡痞女 提交于 2020-02-18 19:16:24
1、Simple If Statement 语法: if (conditional-expression) action 如果是多个action,则语法如下: if (conditional-expression) { action1; action2; } Print all the items with quantity <=5: $ awk -F "," '{ if ($5 <= 5) \ print "Only",$5,"qty of",$2, "is available"; }' \ items.txt Only 2 qty of Refrigerator is available Only 5 qty of Laser Printer is available 2、If Else Statement 语法: if (conditional-expression) action1 else action2 该语法与三元运算符类似: conditional-expression ? action1 : action2 ; The following example displays the message "Buy More" when the total quantity is <= 5, and prints "Sell More" when the total

sed awk

不问归期 提交于 2020-02-17 09:56:55
1 替换文件某一行 将文件./config/patchconfig/patch_config.json 的第二行替换为 "path": "../patchfiles", 将文件aaa.txt中的第三行替换为888 content='"path": "../patchfiles", ' sed -i "2c${content}" ./config/patchconfig/patch_config.jsonsed -i '3c888' ./aaa.txt 参考: https://blog.csdn.net/nanaranran/article/details/81203905 https://www.cnblogs.com/seasonzone/p/11274788.html https://zhidao.baidu.com/question/878349927740895212.html 来源: https://www.cnblogs.com/wolbo/p/12320327.html

Using AWK to filter out column with numerical ranges

本秂侑毒 提交于 2020-02-17 07:00:17
问题 I'm relatively new to BASH and I'm trying to use awk to filter out column 1 data based on the 4th column of a text file. If the 4th column of data matches the range of x, then it'll output column 1 data. "x" is suppose to be a range of numbers 1-10 (1,2,3..10). awk -F: '{ if($4=="x") print $1}' filename.txt filename.txt sample1 0 0 4 sample2 0 0 10 sample3 0 0 15 sample4 0 0 20 Actual use: awk -F: '{ if($4=="1-10") print $1}' sample.txt output = sample1, sample2, sample3, sample4 It should be

Using AWK to filter out column with numerical ranges

一世执手 提交于 2020-02-17 06:59:33
问题 I'm relatively new to BASH and I'm trying to use awk to filter out column 1 data based on the 4th column of a text file. If the 4th column of data matches the range of x, then it'll output column 1 data. "x" is suppose to be a range of numbers 1-10 (1,2,3..10). awk -F: '{ if($4=="x") print $1}' filename.txt filename.txt sample1 0 0 4 sample2 0 0 10 sample3 0 0 15 sample4 0 0 20 Actual use: awk -F: '{ if($4=="1-10") print $1}' sample.txt output = sample1, sample2, sample3, sample4 It should be

Using AWK to filter out column with numerical ranges

泪湿孤枕 提交于 2020-02-17 06:59:05
问题 I'm relatively new to BASH and I'm trying to use awk to filter out column 1 data based on the 4th column of a text file. If the 4th column of data matches the range of x, then it'll output column 1 data. "x" is suppose to be a range of numbers 1-10 (1,2,3..10). awk -F: '{ if($4=="x") print $1}' filename.txt filename.txt sample1 0 0 4 sample2 0 0 10 sample3 0 0 15 sample4 0 0 20 Actual use: awk -F: '{ if($4=="1-10") print $1}' sample.txt output = sample1, sample2, sample3, sample4 It should be

convert a fixed width file from text to csv

南笙酒味 提交于 2020-02-17 05:44:28
问题 I have a large data file in text format and I want to convert it to csv by specifying each column length. number of columns = 5 column length [4 2 5 1 1] sample observations: aasdfh9013512 ajshdj 2445df Expected Output aasd,fh,90135,1,2 ajsh,dj, 2445,d,f 回答1: GNU awk (gawk) supports this directly with FIELDWIDTHS , e.g.: gawk '$1=$1' FIELDWIDTHS='4 2 5 1 1' OFS=, infile Output: aasd,fh,90135,1,2 ajsh,dj, 2445,d,f 回答2: I would use sed and catch the groups with the given length: $ sed -r 's/^(.

Awk基本入门[6] Additional Awk Commands 3

核能气质少年 提交于 2020-02-16 21:33:20
1、Argument Processing (ARGC, ARGV, ARGIND) The built-in variables we discussed earlier, FS, NFS, RS, NR, FILENAME, OFS, and ORS, are all available on all versions of awk (including nawk, and gawk). • The environment variables discussed in this hack are available only on nawk and gawk. • Use ARGC and ARGV to pass some parameters to the awk script from the command line. • ARGC contains the total number of arguments passed to the awk script. • ARGV is an array contains all the arguments passed to the awk script in the index from 0 through ARGC • When you pass 5 arguments, ARGC will contain the

Calculate median of a fIle with many columns using awk

…衆ロ難τιáo~ 提交于 2020-02-16 08:11:49
问题 I tried to calculate the median (not the mean) for many columns in a file. I wrote this (an adaptation from a code that works for only 1 column). sort -n <infile | awk '{for (i = 1; i <= NF; ++i); count[NR] = $i;}END {for (i = 1; i <= NF; ++i); if (NR % 2) {print count[(NR + 1) / 2];} else {print (count[(NR / 2)] + count[(NR / 2) + 1]) / 2;}}' Composite cg00000029 cg00000108 cg00000109 cg00000165 TCGA-G4-6298-11A 0.309164840970903 0.108696904309357 TCGA-G4-6311-11A 0.284214936998384 0

Calculate median of a fIle with many columns using awk

主宰稳场 提交于 2020-02-16 08:09:06
问题 I tried to calculate the median (not the mean) for many columns in a file. I wrote this (an adaptation from a code that works for only 1 column). sort -n <infile | awk '{for (i = 1; i <= NF; ++i); count[NR] = $i;}END {for (i = 1; i <= NF; ++i); if (NR % 2) {print count[(NR + 1) / 2];} else {print (count[(NR / 2)] + count[(NR / 2) + 1]) / 2;}}' Composite cg00000029 cg00000108 cg00000109 cg00000165 TCGA-G4-6298-11A 0.309164840970903 0.108696904309357 TCGA-G4-6311-11A 0.284214936998384 0

Bash awk 基本入门

会有一股神秘感。 提交于 2020-02-15 08:07:46
Bash awk 基本入门 Awk ‘patten {action}’ file Akw command is used to handle fields in one line. Analize every line in the file, and if the line match the patten, then do the action. pattern field is optional. $0 indicates the whole line. The first field is $1. [braveyly@m-net ~]$ cat awk.txt liwei 27 male hust lijing 24 femaile cnnu hujuan 22 female cnnu hurui 18 male hust [braveyly@m-net ~]$ awk '$2 > 23 {print $1, $3, $4}' ./awk.txt liwei male hust lijing femaile cnnu [braveyly@m-net ~]$ awk '$4 == "hust" {print $0}' ./awk.txt liwei 27 male hust hurui 18 male hust [braveyly@m-net ~]$ awk '$4 =