1

I have an output which looks like this:

ASCII file

I want to pick up the last element from the rows that contain the string "Fe\s2b". But the thing is each occurrence of "H\s\s1\s\s\s\s\s\s4861.33A" is a gridpoint and ideally I should have a "Fe\s2b" in the line next to the line containing "H\s\s1\s\s\s\s\s\s4861.33A" (they are to be alternate, in which case I can simply awk the last element for every even line).

Since this is not the case, I wish to keep an account of all the cases where the string "Fe\s2b" is searched in the next line for each "H\s\s1\s\s\s\s\s\s4861.33A" occurrence. Something like this (I know this doesn't work):

awk '{print NR, $0}' testing.txt | grep "H\s\s1\s\s\s\s\s\s4861.33A" testing.txt | awk '{
if ($1 == H && $(NF + 1)== H) 
 print "-99"; 
else 
 grep "Fe\s2b" testing.txt | awk "{print $NF}" testing.txt && <increment line=line+2>}' testing.txt

Here, I was trying to grab the first element of each line and compare with the first element of the next line, if they match then I assign a value '-99'; or else I awk the last element of the next line (i.e. the value corresponding to 'Fe\s2b') and jump to the n+2 line.

0

I found a way to do this in python:

data = open('testing.txt','r+')

for line in data:
    if line[0] == 'H':
        print(-99)
    else:
        if line[-8]==" ":
            print(line[-7:-1])
        else:
            print(line[-8:-1])
        try:
            data.__next__()
        except StopIteration:
            break

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.