awk

Awk column with pattern array

情到浓时终转凉″ 提交于 2021-01-28 11:39:02
问题 Is it possible to do this but use an actual array of strings where it says "array" array=(cat dog mouse fish ...) awk -F "," '{ if ( $5!="array" ) { print $0; } }' file I would like to use spaces in some of the strings in my array. I would also like to be able to match partial matches, so "snow" in my array would match "snowman" It should be case sensitive. Example csv s,dog,34 3,cat,4 1,african elephant,gd A,African Elephant,33 H,snowman,8 8,indian elephant,3k 7,Fish,94 ... Example array

Bash one-liner to mask data in file

こ雲淡風輕ζ 提交于 2021-01-28 07:42:11
问题 I have a file which is quite big. I need to mask all characters in specific postions and from a specific record type. I have searched all over the place but cannot find a solution of this quite simple task. Here is an example File name: hello.txt File: 0120140206INPUT FILE 1032682842 MR SIMPSON 20231458 742 Evergreen Terrace 3034560817 GREEN 1032682842 MR GRIFFIN 20231458 Spooner Street 3034560817 RED 3001 What I would like to do is to mask position 12-16 of all lines beginnning with "10".

how to using variables in search pattern in awk script

邮差的信 提交于 2021-01-28 07:10:59
问题 I want to print the pid when finding matched process while the match pattern is inputted: ps aux | awk -v in="$1" '/in/{print $1}' It seems the former awk sentence is not right. After checking many results in google like this, I change my script in the following but still cannot work: ps aux | awk -v in="$1" '/$0 ~ in/{print $1}' or ps aux | awk -v in="$1" '($0 ~ in) {print $1}' 回答1: You are fairly close in all your attempts. Problem is that in is a reserved keyword in awk . You can use: ps

Match regex across multiple lines in bash

巧了我就是萌 提交于 2021-01-28 07:02:10
问题 I want to match all patterns that start with [% and end with %] in a file. I've tried multiple tools such as awk, sed, pcregrep and none of them seem to work, although they are suggested as top answers on similar questions. [% FOREACH selection = selections -%] case SELECTION_ID_[% SELECTION_NAME %]: { const [% selectionType %]& source = this->[% selectionName %](); rc = bcem_AggregateUtil::toAggregate(result, d_selectionId, source); } break; [% END -%] [% foo ] [% INCLUDE

How to list all non ascii bytes with awk?

☆樱花仙子☆ 提交于 2021-01-28 06:31:05
问题 Here is the test file on google drive. sample :test file I want to list all bytes non ascii byte which beyond \x00-\x7f with awk in the test file. There are 12 bytes beyond \x00-\x7f. It is my try. awk 'BEGIN{FS=""}{for(i=1;i<=NF;++i)if($i~/[^\x00-\x7f]/)print i,$i}' test 146 “ 148 ” 181 “ 184 ” awk 'BEGIN{FS=""}{for(i=1;i<=NF;++i)if($i~/[^\x00-\x7f]/)printf("%d %x \n", i,$i)}' test 146 0 148 0 181 0 184 0 Failed,how to list all the 12 bytes in the file as below format. 146 e2 147 80 148 9c

awk convert ASCII to integer in context of awk command

妖精的绣舞 提交于 2021-01-28 06:08:56
问题 I want to do some manipulation on a file with values coded by ASCII symbols. I have a test file that looks something like this: a> b! I would like to use an awk script to to convert them to integer values and then do further manipulation. I've looked and found a method of converting ASCII to integer: for(n=0;n<256;n++)chr[n]=sprintf("%c",n) but I don't know how to pass my integer values from this array to another array wherein I want to do my numeric manipulation. Example output would be: 195

How to filter out a certain portion of ./gradlew project:dependencies command?

十年热恋 提交于 2021-01-28 05:25:01
问题 Our build.gradle files have your normal depndencies block, like so.. dependencies { compile("com.myco.service:service-discovery:+") compile("com.myco.common:some-common:1.0.84") compile("com.myco.cms:cms-service-client:1.0.145") compile("com.myco.stats:some123-stats-service-client:1.0.140") ... } I run this to get the dependency tree $ ./gradlew project:dependencies | grep com\.myco | sort | uniq and get +--- com.myco.canam:canam:0.1.4 +--- com.myco.cms:cms-service-client:1.0.145 (*) +--- com

Awk date validation

↘锁芯ラ 提交于 2021-01-28 05:13:44
问题 I have an awk script where I need to validate a large number of lines containing dates. I'm currently using either a regex based solution to do a basic validation (without testing for leap years or ) or calling the UNIX date command to validate it more accurately. The date command works well, but calling a system command is pretty expensive in terms of performance. I was hoping that someone here might be able to suggest a solution that is both accurate and is fast. Here's an example of my

GNU awk, FPAT and trouble with a duplicating FS

妖精的绣舞 提交于 2021-01-28 04:45:35
问题 I have a file : $ cat file 1,,"3.1,3.2",4,5 and because of the quotes I'm using FPAT = "([^,]*)|(\"[^\"]+\")") instead of just FS="," . I'm trying to replace a field, lets say $4 , with another value: $ gawk 'BEGIN{FPAT="([^,]*)|(\"[^\"]+\")"; OFS=","}{$4="new"; print}' file 1,,"3.1,3.2",new,,5 $ # right here ^ but I get a duplicated , ( OFS ) after the replaced field. It gets duplicated when modifying any field except last field or empty fields. Are you guys seeing this or is it just me in

awk and md5: replace a column

懵懂的女人 提交于 2021-01-27 21:07:59
问题 Starting from Awk replace a column with its hash value, I tried to hash(md5) a list of numbers: $ cat -n file 1 40755462755 2 40751685373 3 40730094339 4 40722740446 5 40722740446 6 40743802204 7 40730094339 8 40745188886 9 40740593352 10 40745561530 If I run: cat file | awk '{cmd="echo -n " $1 " | md5sum|cut -d\" \" -f1"; cmd|getline md5; $1=md5;print;}' | cat -n 1 29ece26ce4633b6e9480255db194cc40 2 120148eca0891d0fc645413d0f26b66b 3 cafc48d392a004f75b669f9d1d7bf894 4