awk

How to get the validate the count with the group by data in unix

大憨熊 提交于 2020-01-14 06:04:34
问题 I have a list of records as following Source: a,yes a,yes b,No c,N/A c,N/A c,N/A d,xyz d,abc d,abc Output: a, Yes 2 b, No 1 c, N/A 3 d, xyz 1 d, abc 2 c, N/A "File is not correct" Here 'Yes' and 'No' are the acceptable words, If any other word count is greater than the 'Yes' or 'No' word count for an individual $1 value then we have issue a statement like "file is not good" I have tried the below script awk -F, '{a[$1]++;}END{for (i in a)print i, a[i];}' filetest.txt 回答1: If you are not

AWK usage in shell script. Hp unix

时光毁灭记忆、已成空白 提交于 2020-01-14 05:41:50
问题 I'm unable to use the below command using shell script. awk '{a[NR]=$0} $0~s {f=NR} END {for (i=f-B;i<=f+A;i++) print a[i]}' B=1 A=5 s="5S5SDF" testfile Looking for a string "5S5SDF" in testfile. cat myscript #!/bin/ksh echo "The output is" awk '{a[NR]=$0} $0~s {f=NR} END {for (i=f-B;i<=f+A;i++) print a[i]}' B=1 A=4 s= "5S5SDF" testfile The system doesn't display anything, after i pause break i see error myscript[5]: 77144447 Quit myscript[6]: 5S5SDF: not found What am i missing? 来源: https:/

Can printf “%x\n” \'a be performed in awk?

筅森魡賤 提交于 2020-01-14 04:15:30
问题 All printable characters' hex code values can be displayed this way in bash. printf "%x\n" \'a 61 awk 'BEGIN{printf("%x\n",\\'a)}' awk 'BEGIN{printf("%x\n",\'a)}' None of them can be performed in awk,is there no way to do in awk? awk doesn't provide this kind of printf format such as in bash? awk -v var="a" 'BEGIN{printf("%x\n", var)}' 0 echo -n a|xxd 0000000: 61 It is simple to get the a printable characters' hex code value with echo -n a|xxd ,my question is to ask does awk provide this kind

Replacing an SQL query with unix sort, uniq and awk

非 Y 不嫁゛ 提交于 2020-01-14 03:57:22
问题 We currently have some data on an HDFS cluster on which we generate reports using Hive. The infrastructure is in the process of being decommissioned and we are left with the task of coming up with an alternative of generating the report on the data (which we imported as tab separated files into our new environment) Assuming we have a table with the following fields. Query IPAddress LocationCode Our original SQL query we used to run on Hive was (well not exactly.. but something similar) select

using awk sed to parse update puppet file

房东的猫 提交于 2020-01-14 02:57:18
问题 I have a puppet file with a number of lines of code that has a section that looks like this: $defaultrepo=myrepo $defaultbranch=mybranch gitmod::pullstuff {'othergitcode': gitcommit => "b54123be540adrwer3b65872384e0101c5f94c926b81", gitorg => "${defaultrepo}", branch => "${defaultbranch}", } gitmod::pullstuff {'mygitcode': gitcommit => "b54123be540adrfer3b65872384e0101c5f94c926b81", gitorg => 'awesomerepo', branch => "master", } It can have any number of blocks that will look similar to the

How to remove lines above and below an inverse grep match?

你离开我真会死。 提交于 2020-01-13 20:25:27
问题 I'm working with some output that is more verbose than I'd like, so I was trying to use grep to whittle it down. The output looks something like this… path/to/file1: No Problems Found path/to/file3: Problem Found I'd like to filter out all the output concerning files without problems. I'm able to remove one line of it by piping the output through grep -v "No Problems Found" . I thought I'd then be able to use -B and -A along the lines of grep -B 1 -A 1 -v "No Problems Found" but it turns out

output matching column from multiple input in awk

元气小坏坏 提交于 2020-01-13 20:23:32
问题 Assumes there are some data from these two input which I only want, which is "A" from inputA.txt and "B" from inputB.txt ==> inputA.txt <== A 10214027 6369158 A 10214028 6369263 A 10214029 6369321 A 10214030 6369713 A 10214031 6370146 A 10214032 6370553 A 10214033 6370917 A 10214034 6371322 A 10214035 6371735 A 10214036 6372136 So I only want the data with A's ==> inputB.txt <== B 50015214 5116941 B 50015215 5116767 B 50015216 5116577 B 50015217 5116409 B 50015218 5116221 B 50015219 5116044 B

Calculate average of each column in a file

旧城冷巷雨未停 提交于 2020-01-13 11:30:10
问题 I have a text file with n number of rows (separated by commas) and columns and I want to find average of each column, excluding empty field. A sample input looks like: 1,2,3 4,,6 ,7, The desired output is: 2.5, 4.5, 4.5 I tried with awk -F',' '{ for(i=1;i<=NF;i++) sum[i]=sum[i]+$i;if(max < NF)max=NF;};END { for(j=1;j<=max;j++) printf "%d\t",sum[j]/max;}' input But it treats consecutive delimiters as one and mixing columns. Any help is much appreciated. 回答1: You can use this one-liner: $ awk

awk stumper: regex substitution within a field

孤人 提交于 2020-01-13 10:14:14
问题 I'm new to awk, and I can't seem to figure this one out. How can I substitute in a single field using a regular expression? In perl, I could assign the field of interest to a variable, then $myvar =~ s/foo/bar/g . Of course also in perl I have to do my own field management, and that's easier in awk. For the issue at hand just now, it's European money records and I want to change commas to periods in the amount field. But I need to target only that field, so I don't mangle the other fields

awk stumper: regex substitution within a field

☆樱花仙子☆ 提交于 2020-01-13 10:13:55
问题 I'm new to awk, and I can't seem to figure this one out. How can I substitute in a single field using a regular expression? In perl, I could assign the field of interest to a variable, then $myvar =~ s/foo/bar/g . Of course also in perl I have to do my own field management, and that's easier in awk. For the issue at hand just now, it's European money records and I want to change commas to periods in the amount field. But I need to target only that field, so I don't mangle the other fields