awk

awk: negative exponential is not correctly interpreted

孤者浪人 提交于 2020-06-16 03:37:38
问题 I have this table: a 0 b 0 c 1.6149e-315 d 5.2587e-265 e 8.2045e-227 f 8.2045e-227 If I type $awk '($2<1){print}' my_file.txt it returns a 0 b 0 d 5.2587e-265 e 8.2045e-227 f 8.2045e-227 but it considers the value in the third row, 1.6149e-315, to be larger than 1: $awk '($2>1){print}' my_file.txt c 1.6149e-315 Which is the reason for this behaviour? Is a negative exponential <1e-300 too small so it removes the "e-" part? It looks so, since $awk '($2>1.6149){print}' my_file.txt c 1.6149e-315

How to swap the first line with last line in a text file using SED/AWK

点点圈 提交于 2020-06-15 05:50:44
问题 I am trying to swap the first line with last line in a text file in unix file has: line1 line2 line3 line4 line5 line6 I want like this: line6 line2 line3 line4 line5 line1 I am using sed -n -e '1 s/^.*$/$p' file . Which is not happening. 回答1: EDIT2: As per Ed sir's comment adding this solution too here which will be more suitable in case 2nd line is empty then also it will work. awk 'NR==1{first=$0;next} NR>2{val=val prev ORS} {prev=$0} END{print prev ORS val first} Input_file EDIT: To

Extract all ip addresses with sed and awk from a string

痴心易碎 提交于 2020-06-13 08:32:06
问题 It is simple to extract all ip addresses with grep from a string. string="221.11.165.237xxxx221.11.165.233\n 219.158.9.97ttttt219.158.19.137" echo $string |grep -oP "(\d+\.){3}\d+" 221.11.165.237 221.11.165.233 219.158.9.97 219.158.19.137 The regrex pattern is simple (\d+\.){3}\d+ . Do the same job with sed and awk. For sed: echo $string | sed 's/^\(\(\d\+\.\)\{3\}\d\+\)$/\1/g' 221.11.165.237xxxx221.11.165.233\n 219.158.9.97ttttt219.158.19.137 For awk: echo $string |gawk 'match($0,/(\d+\.){3}

Using output of awk to run command

帅比萌擦擦* 提交于 2020-06-09 16:47:49
问题 I am brand new to shell scripting and cannot seem to figure out this seemingly simple task. I have a text file (ciphers.txt) with about 250 lines, and I would like to use the first column of each line as an argument in a command. Any help would be greatly appreciated! the command is: openssl s_client -connect host:port -cipher argument It works fine when I do one at a time but I do not really want to run the same command 250+ times. Here is my script so far: awk '{command = "openssl s_client

Select row and element in awk

淺唱寂寞╮ 提交于 2020-06-09 07:21:39
问题 I learned that in awk, $2 is the 2nd column. How to specify the ith line and the element at the ith row and jth column? 回答1: To print the second line: awk 'FNR == 2 {print}' To print the second field: awk '{print $2}' To print the third field of the fifth line: awk 'FNR == 5 {print $3}' Here's an example with a header line and (redundant) field descriptions: awk 'BEGIN {print "Name\t\tAge"} FNR == 5 {print "Name: "$3"\tAge: "$2}' There are better ways to align columns than "\t\t" by the way.

Select row and element in awk

点点圈 提交于 2020-06-09 07:21:06
问题 I learned that in awk, $2 is the 2nd column. How to specify the ith line and the element at the ith row and jth column? 回答1: To print the second line: awk 'FNR == 2 {print}' To print the second field: awk '{print $2}' To print the third field of the fifth line: awk 'FNR == 5 {print $3}' Here's an example with a header line and (redundant) field descriptions: awk 'BEGIN {print "Name\t\tAge"} FNR == 5 {print "Name: "$3"\tAge: "$2}' There are better ways to align columns than "\t\t" by the way.

AWK print command for specific rows

点点圈 提交于 2020-06-09 07:14:45
问题 I have millions of records in my file, what i need to do is print columns 1396 to 1400 for specific number of rows, and if i can get this in excel or notepad. Tried with this command awk {print $1396,$1397,$1398,$1399,$1400}' file_name But this is running for each row. 回答1: You need a condition to specify which rows to apply the action to: awk '<<condition goes here>> {print $1396,$1397,$1398,$1399,$1400}' file_name For example, to do this only for rows 50 to 100: awk 'NR >= 50 && NR <= 100

AWK print command for specific rows

蓝咒 提交于 2020-06-09 07:14:30
问题 I have millions of records in my file, what i need to do is print columns 1396 to 1400 for specific number of rows, and if i can get this in excel or notepad. Tried with this command awk {print $1396,$1397,$1398,$1399,$1400}' file_name But this is running for each row. 回答1: You need a condition to specify which rows to apply the action to: awk '<<condition goes here>> {print $1396,$1397,$1398,$1399,$1400}' file_name For example, to do this only for rows 50 to 100: awk 'NR >= 50 && NR <= 100

How to find out the max value of the third field according to the first two fields using awk

醉酒当歌 提交于 2020-06-09 04:34:41
问题 The file content is as follows: 333379266 834640619 88 333379280 834640621 99 333379280 834640621 66 333376672 857526666 99 333376672 857526666 78 333376672 857526666 62 The first two columns may be duplicate, and I want to output the first two columns and the corresponding max value of the third column.In this case,The result file should be as follows: 333379266 834640619 88 333379280 834640621 99 333376672 857526666 99 My attemp is: awk '{d[$1" "$2]=$3;if ($3>=d[$1" "$2]){num[$1" "$2]=$3}

How to find out the max value of the third field according to the first two fields using awk

走远了吗. 提交于 2020-06-09 04:34:04
问题 The file content is as follows: 333379266 834640619 88 333379280 834640621 99 333379280 834640621 66 333376672 857526666 99 333376672 857526666 78 333376672 857526666 62 The first two columns may be duplicate, and I want to output the first two columns and the corresponding max value of the third column.In this case,The result file should be as follows: 333379266 834640619 88 333379280 834640621 99 333376672 857526666 99 My attemp is: awk '{d[$1" "$2]=$3;if ($3>=d[$1" "$2]){num[$1" "$2]=$3}