Reputation: 447
I am trying to find everywhere my data has a 90 in column 2 and two lines above change the value of column 2. For example in my data below, if I see 90 at line 11 I want to change my column 2 value at line 9 from 11 to 5. I have a predetermined set of values I want to change the number to; the values will always be 10,11,12,30,31,32 to 1,2,3,4,5,6 respectably.
My data
# Type Response Acc RT Offset
1 70 0 0 0.0000 57850
2 31 0 0 0.0000 59371
3 41 0 0 0.0000 60909
4 70 0 0 0.0000 61478
5 31 0 0 0.0000 62999
6 41 0 0 0.0000 64537
8 70 0 0 0.0000 65106
9 11 0 0 0.0000 66627
10 21 0 0 0.0000 68165
11 90 0 0 0.0000 68700
12 31 0 0 0.0000 70221
What I want
# Type Response Acc RT Offset
1 70 0 0 0.0000 57850
2 31 0 0 0.0000 59371
3 41 0 0 0.0000 60909
4 70 0 0 0.0000 61478
5 31 0 0 0.0000 62999
6 41 0 0 0.0000 64537
8 70 0 0 0.0000 65106
9 5 0 0 0.0000 66627
10 21 0 0 0.0000 68165
11 90 0 0 0.0000 68700
12 31 0 0 0.0000 70221
I have been trying to store the previous line and use that as a reference but I can only go back one line, and I need to go back two. Thank you for your help.
Upvotes: 1
Views: 132
Reputation: 6856
This version maintains your original formatting
awk 'BEGIN{ new[" 1"]="10"; new[" 2"]="11"; new[" 3"]="12"
new[" 4"]="30"; new[" 5"]="31"; new[" 6"]="32" }
{ line[-2]=line[-1]; line[-1]=line[0]; line[0]=$0 }
$2==90 { if( match( line[-2], /^ *[0-9]+ +[1-6] / ) ) {
old=substr( line[-2], RLENGTH-2,2 )
line[-2]=substr( line[-2], 1, RLENGTH-3 ) new[old] \
substr( line[-2], RLENGTH ) } }
NR>2 { printf("%s\n",line[-2]) }
END { printf("%s\n%s\n",line[-1],line[0]) }' file.in
Upvotes: 0
Reputation: 28578
This should work:
function pra(a) {
for(e in a) {
printf "%s ", a[e];
}
print "";
}
BEGIN {
vals[10] = 1;
vals[11] = 2;
vals[12] = 3;
vals[30] = 4;
vals[31] = 5;
vals[32] = 6;
}
NR == 1 { split($0, a, " ") }
NR == 2 { split($0, b, " ") }
NR > 2 {
if($2 == "90") {
a[2] = vals[a[2]];
}
pra(a);
al = 0;
for(i in a) al++;
for(i = 1; i <= al; i++) {
a[i] = b[i];
}
split($0, b, " ");
}
END {
pra(a);
pra(b);
}
The rundown of how this works:
* BEGING block - assign the translation values to vals
* NR == 1 and NR == 2 - remember the first two lines into split arrays a
and b
* NR > 2 - for all lines after the first two
* If the second column has value 90
, change it using the translation array
* Move elements of array b
to a
and split the current line into b
* END block - print a
and b
, which are basically last two lines
Sample run:
$ cat inp && awk -f mkt.awk inp
# Type Response Acc RT Offset
1 70 0 0 0.0000 57850
2 31 0 0 0.0000 59371
3 41 0 0 0.0000 60909
4 70 0 0 0.0000 61478
5 31 0 0 0.0000 62999
6 41 0 0 0.0000 64537
8 70 0 0 0.0000 65106
9 11 0 0 0.0000 66627
10 21 0 0 0.0000 68165
11 90 0 0 0.0000 68700
12 31 0 0 0.0000 70221
# Type Response Acc RT Offset
1 70 0 0 0.0000 57850
2 31 0 0 0.0000 59371
3 41 0 0 0.0000 60909
4 70 0 0 0.0000 61478
5 31 0 0 0.0000 62999
6 41 0 0 0.0000 64537
8 70 0 0 0.0000 65106
9 2 0 0 0.0000 66627
10 21 0 0 0.0000 68165
11 90 0 0 0.0000 68700
12 31 0 0 0.0000 70221
You can do something like this:
function pra(a) {
printf "%4d%8d%3d%5d%9.4f%6d\n", a[1], a[2], a[3], a[4], a[5], a[6]
}
BEGIN {
vals[10] = 1;
vals[11] = 2;
vals[12] = 3;
vals[30] = 4;
vals[31] = 5;
vals[32] = 6;
}
NR == 1 { print }
NR == 2 { split($0, a, " ") }
NR == 3 { split($0, b, " ") }
NR > 4 {
if($2 == "90") {
a[2] = vals[a[2]];
}
pra(a);
for(i = 1; i <= 6; i++) {
a[i] = b[i];
}
split($0, b, " ");
}
END {
pra(a);
pra(b);
}
To make it work for this specific case that includes formatting. Sample run:
$ cat inp && awk -f mkt.awk inp
# Type Response Acc RT Offset
1 70 0 0 0.0000 57850
2 31 0 0 0.0000 59371
3 41 0 0 0.0000 60909
4 70 0 0 0.0000 61478
5 31 0 0 0.0000 62999
6 41 0 0 0.0000 64537
8 70 0 0 0.0000 65106
9 11 0 0 0.0000 66627
10 21 0 0 0.0000 68165
11 90 0 0 0.0000 68700
12 31 0 0 0.0000 70221
# Type Response Acc RT Offset
1 70 0 0 0.0000 57850
2 31 0 0 0.0000 59371
4 70 0 0 0.0000 61478
5 31 0 0 0.0000 62999
6 41 0 0 0.0000 64537
8 70 0 0 0.0000 65106
9 2 0 0 0.0000 66627
10 21 0 0 0.0000 68165
11 90 0 0 0.0000 68700
12 31 0 0 0.0000 70221
Upvotes: 1