Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace record in the previous line.

Status
Not open for further replies.

shaokat

Technical User
Oct 8, 2002
11
IN
I have a script that matches two fields (at position 7 length 20 and position 479 length 6) and inserts character '01' and '02' on subsequent matching records. Works fine so far.

Now, I want to replace (say position 6) with 'Y' in the first record of the matching condition.

ie. When I get to the '02' matching result, I want to replace position 6 with 'Y' of the previous line that matched '01'

How do I do this?

-KAT




awk '{
curr1 = substr($0, 7, 20);
curr2 = substr($0, 479, 6);
if (curr1 == last1 && curr2 == last2)
{
printf("02%s\n", $0)
}
else
{
printf("01%s\n", $0)
last1 = curr1
last2 = curr2
}
}'
 
You will need to explain your problem a little better. Some sample input and output data would be helpful. CaKiwi
 
Code:
When you say "position 6" we have no clue as to what you 
are referring to..
If you are talking about substituting a record 
portion from a preceding NR you will need to load your 
file into an awk array, use numbered subscripts 
and create a method for reading and writing to file.




BEGIN {
   a = 1
   while ((getline array[a] < ARGV[1]) > 0) {
          a++;
   }
   close(ARGV[1])

   for (i=1 ; i <= a ; i++) {
        if (array[i] ~ matchingpattern) {
            do_change(array,(i - 1))
            changes++
        }
   }

   for (i=1 ; i <= a ; i++) {
       print array[i] >> ARGV[2]
   }
   close(ARGV[2])
   exit
}
 
Here is the problem with samples. Forget the &quot;position&quot; mentioned earlier.

Consider this as input:

ABCD 123456789
DERS 234234234
ERED 345634563
RTYR 343434343
TYUT 343434343
TERE 368876655

This is the expected output:

ABCD 123456789
DERS 234234234
ERED 455634563
RTYR 343434343 Y
TYUT 343434343
TERE 368876655

Thanks for your response earlier.
 
Untested.

{
if (NR > 0) {
printf s0
if (s2 == $2) printf &quot; Y&quot;
print &quot;&quot;
}
s0 = $0
s2 = $2
}
END {print s0}

Gotta go. CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top