The short answer is yes.
But, I'm not sure I understand your requirements. If you want help creating an awk script to perform this taks, please answer the following questions:
Do you want input fields 2 and 4 to be removed from every input line?
If the value in input field 1 is not a constant in each set of four input lines, what happens?
[list=a]
Is that set skipped? If so, should an error be printed?
Should the value from the first line in the set be printed?
Should the value from the last line in the set be printed?
[/list]
If the value in input field 3 is not a constant in each set of four input lines, what happens?
[list=a]
Is that set skipped? If so, should an error be printed?
Should the value from the first line in the set be printed?
Should the value from the last line in the set be printed?
[/list]
Is the number of fields a constant for a given input file?
Hi Don, thanks for taking the time. Below, are the answers to your questions:
Do you want input fields 2 and 4 to be removed from every input line?
Yes - they need to be removed
If the value in input field 1 is not a constant in each set of four input lines, what happens?
-Is that set skipped? If so, should an error be printed?
-Should the value from the first line in the set be printed?
-Should the value from the last line in the set be printed?
Basically, values in fields 1 and 3 will remain constant for every set of 4 lines. Hence for every group of four lines, I need the values in the first line for these fields.
If the value in input field 3 is not a constant in each set of four input lines, what happens?
-Is that set skipped? If so, should an error be printed?
-Should the value from the first line in the set be printed?
-Should the value from the last line in the set be printed?
As stated above... the values in input fields 1 and 3 will remain constant for every set of four lines
4.Is the number of fields a constant for a given input file?
Yes the number of fields is constant for a given input file.
The following is the code that I am working with right now - though it isnt working and does not have all the features I need
The following awk script is a little more complex than you requested. It allows processing of multiple input files, prints an end of file separator if more than one input file is given, and prints a warning if there are lines left at the end of a file that don't make up a complete 4 line set.
Since you said all values in a 4 line set are constant in fields 1 and 3, I used the values in the last line of the set instead of in the 1st line of the set (it saved me from needing to create two more variables). If you really need the 1st line's values instead of the last line's values, it won't be hard to change this script to do that.
As always, if you're using a Solaris/SunOS system, use /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk instead of awk .
awk '
FNR == 1 {
# Check for incomplete set at end of previous file.
if(l) {
printf("%d line(s) skipped at end of %s.\n", l, file)
for(i = 5; i <= n; i++) s = 0
}
# Print file trailer if more than 1 file has been seen.
if(nf++) printf("================== End of data from file %s\n", file)
# Process headers: Print output headers, determine field count.
printf("%s %s ", $1, $3)
for(i = 5; i <= NF; i++) printf("%s%s", $i, i == NF ? "\n" : " ")
n = NF # set number of fields for this file
l = 0 # set number of lines in current set
file = FILENAME # save filename for diagnostics
next
}
{ for(i = 5; i <= n; i++) s += $i}
++l == 4 {
l = 0
printf("%d %d ", $1, $3)
for(i = 5; i <= NF; i++) {
printf("%6.4f%s", s, i == NF ? "\n" : " ")
s = 0
}
}
END { if(l) printf("%d line(s) skipped at end of %s.\n", l, file)
if(nf > 1) printf("================== End of data from file %s\n", file)
}' input