user790049
user790049

Reputation: 1254

whats wrong with in my awk command?

Can anybody tell what wrong with this command?

file.txt contains some numbers

I want to print next number to zero i.e. 0 (exact match) of each occurrence and I try the below command

awk '/^0/ {for(i=1; i<=1; i++) {getline; printf("%s,",$0);}}' file.txt

and I got the expected result i.e.,

 8,3,31,8,32,33,1,23,16,19,33,15,34,25,14,15,19,16,27,

but when i try to get all the next numbers to 1, then the result is not as expect

awk '/^1/ {for(i=1; i<=1; i++) {getline; printf("%s,",$0);}}' file.txt

Result

20,12,16,9,12,13,2,34,12,9,26,15,28,30,10,29,20,25,24,33,22,7,16,22,33,11,16,11, 10,8,27,14,33,16,2,33,23,11,11,15,17,36,8,16,35,16,3,26,25,35,33,26,17,19,10,4,29,20,3,28,21,29,13,6,15,35,21,31,0,16,2,8,13,12,6,14,7,8,13,15,30,5,15,20,35,27,10,32,16,19,24,35,12,28,18,35,24,7,32,8,4,17,35,17,11,24,0,22,12,15,0,8,10,2,33,25,25,31,28,35,14,19,27,7,31,15,26,17,18,36,1,9,28,19,7,12,0,4,30,28,20,21,14,0,22,19,15,35,7,8,5,1,30,21,

expected result for 1

12,28,10,2,17,10,4,29,16,0,31,27,0,28,10

likewise if i try to get next number for occurrence of 2,3,4,.....

can anybody tell whats wrong?

updated with $0 - still having the same problem Thanks

Upvotes: 0

Views: 65

Answers (4)

vkersten
vkersten

Reputation: 161

Try:

awk '/^0$/ {getline ; printf("%s,",$1)} ' 

and for values following 1:

awk '/^1$/ {getline ; printf("%s,",$1)} ' 

Upvotes: 0

Kent
Kent

Reputation: 195059

I think your problem is the continuous lines beginning with (or equals to) 1 (or other). you want the first line not beginning with 1.

I won't use getline in this case, it just makes the problem more complicated. check this:

0 case:

kent$  awk '/^0/{f=1;next}f{f=0;printf "%s, ", $0}' f 
8, 3, 31, 8, 32, 33, 1, 23, 16, 19, 33, 15, 34, 25, 14, 15, 19, 16, 27,

1 case: (exact match)

kent$  awk '$0=="1"{f=1;next}f{f=0;printf "%s,", $0}' f 
12,28,10,2,17,10,4,29,16,0,31,27,0,28,10,

1 case: (regex match)

kent$  awk '/^1/{f=1;next}f{f=0;printf "%s,", $0}' f
20,35,32,9,5,5,2,34,9,26,8,28,30,4,29,20,25,24,33,22,7,22,33,2,25,0,8,27,33,5,2,33,23,3,23,5,36,8,35,5,3,26,25,35,33,26,21,27,30,4,29,20,3,28,21,29,6,2,35,21,31,0,27,2,8,35,6,7,8,8,36,30,5,34,20,35,27,34,32,9,22,24,35,28,25,35,24,7,32,8,4,5,35,23,24,0,22,0,32,0,8,2,33,25,25,31,28,35,7,27,7,31,25,26,20,36,0,9,28,7,0,4,30,28,20,21,22,0,22,21,9,35,7,8,5,30,21

you can pass argument for the 0 or 1, to make your awk command generic.

Upvotes: 1

H&#229;kon H&#230;gland
H&#229;kon H&#230;gland

Reputation: 40758

You may try the following code:

awk -vval=0 '
$1 == val {
   getline
   printf("%s,",$0)
}
END {
    print ""
}' file.txt

Upvotes: 0

fedorqui
fedorqui

Reputation: 289725

Maybe this is better:

awk '/^1$/ {for(i=1; i<=1; i++) {getline; printf("%s,",0);}}' file.txt

With /^1$/ you indicate that the whole line starts with 1... and it is just 1. With your current solution, /^1/ can match any number starting with 1: 1, 10, 11, 12423432...

Although I would go for:

$ awk '$0==1 {getline; print}' file
12
28
10
2
17
10
4
29
16
0
31
27
0
28
10

Upvotes: 2

Related Questions