awk

Remove all lines between two strings

ぃ、小莉子 提交于 2021-02-07 09:00:35
问题 In a sh shell script. Given data in a text file: string1 string2 gibberish gibberish string3 gibberish string4 How could you use awk or sed to remove all lines between string2 (inclusive) and string3 (not including string3 )? to end up with: string1 string3 string4 回答1: you can try this. Anything before "string2" will not be deleted. awk 'BEGIN{f=0} { match($0,"string2") if(RSTART){ print substr($0,1,RSTART-1) f=1 next } match($0,"string3") if(RSTART){ $0=substr($0,RSTART) f=0 } } f==0{print}

awk sort multidimensional array [duplicate]

放肆的年华 提交于 2021-02-07 08:54:33
问题 This question already has an answer here : Appending an element to associative array awk (1 answer) Closed 4 years ago . GNU awk supports multidimensional arrays: q[1][1] = "dog" q[1][2] = 999 q[2][1] = "mouse" q[2][2] = 777 q[3][1] = "bird" q[3][2] = 888 I would like to sort the "second column" of q such that I am left with: q[1][1] = "mouse" q[1][2] = 777 q[2][1] = "bird" q[2][2] = 888 q[3][1] = "dog" q[3][2] = 999 as you can see the "first column" values moved to keep with the second. I

awk sort multidimensional array [duplicate]

痴心易碎 提交于 2021-02-07 08:54:15
问题 This question already has an answer here : Appending an element to associative array awk (1 answer) Closed 4 years ago . GNU awk supports multidimensional arrays: q[1][1] = "dog" q[1][2] = 999 q[2][1] = "mouse" q[2][2] = 777 q[3][1] = "bird" q[3][2] = 888 I would like to sort the "second column" of q such that I am left with: q[1][1] = "mouse" q[1][2] = 777 q[2][1] = "bird" q[2][2] = 888 q[3][1] = "dog" q[3][2] = 999 as you can see the "first column" values moved to keep with the second. I

sort a file based on a column in another file

大憨熊 提交于 2021-02-07 08:46:12
问题 I have two files both in the format of: loc1 num1 num2 loc2 num3 num4 The first column is the location and I want to use the order of the locations in the first file to sort the second file so that I can put the two files together where the numbers are right for the location. I can write a perl script to do this but I felt there might be some quick/easy shell/awk command to achieve this. Do you have any ideas? Thanks. Edits: Here is the input, now I actually want to use column 2 in file 1 to

sort a file based on a column in another file

試著忘記壹切 提交于 2021-02-07 08:45:25
问题 I have two files both in the format of: loc1 num1 num2 loc2 num3 num4 The first column is the location and I want to use the order of the locations in the first file to sort the second file so that I can put the two files together where the numbers are right for the location. I can write a perl script to do this but I felt there might be some quick/easy shell/awk command to achieve this. Do you have any ideas? Thanks. Edits: Here is the input, now I actually want to use column 2 in file 1 to

normalize column data with maximum value of that column

隐身守侯 提交于 2021-02-07 07:58:22
问题 I have a data file with two columns. I want to find out the maximum data value from the second column and divide each entries of second column witht he maximum value. (So I will get all the entries in second column <= 1.00). I tried with this command below: awk 'BEGIN {max = 0} {if ($2>max) max=$2} {print ($2/max)}' angleOut.dat but I get error message as below. awk: (FILENAME=angleOut.dat FNR=1) fatal: division by zero attempted note: There are some data in the second column which is zero

normalize column data with maximum value of that column

非 Y 不嫁゛ 提交于 2021-02-07 07:57:50
问题 I have a data file with two columns. I want to find out the maximum data value from the second column and divide each entries of second column witht he maximum value. (So I will get all the entries in second column <= 1.00). I tried with this command below: awk 'BEGIN {max = 0} {if ($2>max) max=$2} {print ($2/max)}' angleOut.dat but I get error message as below. awk: (FILENAME=angleOut.dat FNR=1) fatal: division by zero attempted note: There are some data in the second column which is zero

normalize column data with maximum value of that column

*爱你&永不变心* 提交于 2021-02-07 07:57:19
问题 I have a data file with two columns. I want to find out the maximum data value from the second column and divide each entries of second column witht he maximum value. (So I will get all the entries in second column <= 1.00). I tried with this command below: awk 'BEGIN {max = 0} {if ($2>max) max=$2} {print ($2/max)}' angleOut.dat but I get error message as below. awk: (FILENAME=angleOut.dat FNR=1) fatal: division by zero attempted note: There are some data in the second column which is zero

normalize column data with maximum value of that column

寵の児 提交于 2021-02-07 07:57:07
问题 I have a data file with two columns. I want to find out the maximum data value from the second column and divide each entries of second column witht he maximum value. (So I will get all the entries in second column <= 1.00). I tried with this command below: awk 'BEGIN {max = 0} {if ($2>max) max=$2} {print ($2/max)}' angleOut.dat but I get error message as below. awk: (FILENAME=angleOut.dat FNR=1) fatal: division by zero attempted note: There are some data in the second column which is zero

AWK: Comparing two different columns in two files

ε祈祈猫儿з 提交于 2021-02-07 07:20:06
问题 I have these two files File1: 9 8 6 8 5 2 2 1 7 0 6 1 3 2 3 4 4 6 File2: (which has over 4 million lines) MN 1 0 JK 2 0 AL 3 90 CA 4 83 MK 5 54 HI 6 490 I want to compare field 6 of file1, and compare field 2 of file 2. If they match, then put field 3 of file2 at the end of file1 I've looked at other solutions but I can't get it to work correctly. Desired output: 9 8 6 8 5 2 0 2 1 7 0 6 1 0 3 2 3 4 4 6 490 My attempt: awk 'NR==FNR{a[$2]=$2;next}a[$6]{print $0,a[$6]}' file2 file1 program just