awk, declare array embracing FNR and field, output -


i declare array of number of lines, means line 10 line 78, example. other number, example. sample gives me range of lines on stdout sets "1" in between lines. can tell me how rid of "1"? sample follows should go stdout , embraces named lines.

awk ' myarr["range-one"]=nr~/^2$/ , nr~/^8$/; {print myarr["range-one"]};' /home/$user/uplog.txt; 

that giving me output:

0 12:33:49 3:57, 2 users, load average: 0,61, 0,37, 0,22 21.06.2014 1 12:42:02 4:06, 2 users, load average: 0,14, 0,18, 0,19 21.06.2014 1 12:42:29 4:06, 2 users, load average: 0,09, 0,17, 0,19 21.06.2014 1 12:43:09 4:07, 2 users, load average: 0,09, 0,16, 0,19 21.06.2014 1 

second question: how set in array 1 field of fnr or line? when way there comes field wanted

awk ' nr~/^1$/ , nr~/^7$/ {print $3, $11; next} ; ' /home/$user/uplog.txt; 

but need array, thats why i'm asking. hints? in advance.

what example script does

awk ' myarr["range-one"]=nr~/^2$/ , nr~/^8$/; {print myarr["range-one"]};' 

your script 1 of more convoluted , decidedly less-than-obvious pieces of awk i've ever seen. let's take simple input file:

line  1 line  2 line  3 line  4 line  5 line  6 line  7 line  8 line  9 line 10 line 11 line 12 

the output is:

0 line  2 1 line  3 1 line  4 1 line  5 1 line  6 1 line  7 1 line  8 1 0 0 0 0 

dissecting script, appears first line:

myarr["range-one"]=nr~/^2$/ , nr~/^8$/; 

is equivalent to:

myarr["range-one"] = (nr ~ /^@$/, nr ~ /^8$/) { print } 

that is, value assigned myarr["range-one"] 1 inside range of line numbers nr equal 2 , equal 8, , 0 outside range; further, when value 1, line printed.

the second line:

{print myarr["range-one"]}; 

print value in myarr["range-one"] each line of input. thus, on first line, value 0 printed. lines 2 8, line printed followed value 1; lines after that, value 0 printed once more.

what question asks for

the question not clear. appears lines 10 78 should printed. in awk, there no variable declarations (we can debate function parameters, functions don't seem figure in this). therefore, declaring array not option.

awk -v lo=10 -v hi=78 'nr >= lo && nr <= hi { print }' 

this print lines between line 10 , line 78. feasible save values in array (a in examples below). said array indexed nr or separate index starting @ 0 or 1:

awk -v lo=10 -v hi=78 'nr >= lo && nr <= hi { a[nr] = $0 }'   # indexed line number awk -v lo=10 -v hi=78 'nr >= lo && nr <= hi { a[i++] = $0 }'  # indexed 0 awk -v lo=10 -v hi=78 'nr >= lo && nr <= hi { a[++i] = $0 }'  # indexed 1 

presumably, you'd have end block data.

the semicolons in original both unnecessary. blank line ignored, of course.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -