Hello everyone! In a shell script I read an input file, from which I want to print on an output file strings from different lines. I set as a landmark a line that start with the word ''DATA''. Now I want to print, on the output file:
FIRST a string 3 lines below my landmark line
SECOND a string 6 lines bellow my landmark line
LAST a string in the same line.
My problem is the output file doesn't follow the print order I determine, but print first the string that comes first in the imput file (= ^DATA/ {print $3,$4} ). How can I fix it?
@ChaosTheory,
it would help you and all of us if could provide a small representative sample of the input file AND the matching desired output. Otherwise we're (all) simply guessing what you're striving to achieve.
E.g. are the 3rd/6th lines after the "marker" line simply determined by their line numbered location OR they have some other "designation" (e.g. a certain string distinguishing them from the other lines?
Looking at a sample representative file (potentially covering edge cases) could demonstrate what you're after.
@ChaosTheory , I've edited your data sample using the proper markdown formatting for now.
Please make sure you format your data/code sample yourself going forward,
Please make sure you provide the exact as you have it - don't post partial data with ...-s.
I've guessed the best I could what I'd thought your data looked like.
Given the provided/edited/formatted data input and the suggestion by @MadeInGermany:
In a BEGIN section, and by defining a format in a variable with only %s elements, you can automatically align a header line.
Non-%s elements you can preformat with sprintf()
It looks like you are using awk to parse and process your input file, and you want to store some information in arrays matrix1 and matrix2, then print them out to the output file in a specific order. However, it seems that the order in which the information is printed to the output file is not the same as the order you specified.
To fix this issue, you can try modifying your awk script as follows:
Instead of storing the information you want to print in arrays matrix1 and matrix2, you can store them in separate variables, for example string1, string2, and string3.
After you have read and stored all the necessary information from the input file, you can print the variables in the desired order by using a single print statement.
END {
print string1
print string2
print string3
}
This should ensure that the strings are printed in the correct order in the output file.
I hope this helps! Let me know if you have any questions.