combine multiple awk statements into single line
I'm mining logs in an attempt to create a pipe delimited file. I have the below awk statement, but I can't seem to get the appropriate output onto the correct line:
Quote:
The desired output is: Quote:
|
print always ends with ORS, usually a newline character. Use printf instead. See also my signature.
What is the end for, by the way? |
The end was most likely left over from previous attempts at this bit of functionality. printf gets everything into a single line. I need to have my output as above. Some tables will have migrate dates while others do not. If a table has a migrate date, then I need a new line. If a table does not have a migrate date, I also need a new line to start with the next table name. Thanks for your help and also the link provided. I will continue to research.
Code:
awk '/nz_migrate table/ {printf $3}/.....migration process started/ {printf $5,$6}' tony.log |
If you want a newline, add it to the printf - "\n".
|
Convert the first print into printf. Add code that prints a newline when the input line doesn't contain /migration process started/.
|
I believe I've already converted the first print to a printf. Additionally, that last input line will not always contain /migration process started/. My biggest challenge throughout the logfile is that not all the input will be identical in format. Thanks for your assistance, I will continue to research.
|
ie
Code:
nz_migrate table table1 |
It's a bit tricky to get the newlines alright, especially in the first and the last section (output line).
The following uses a variable nl that is empty in the first line. At the END a pending newline is printed. Code:
awk '/nz_migrate table/ {printf "%s", nl $3} /.....migration process/ {printf "|%s %s", $5, $6} {nl="\n"} END {printf nl}' logfile |
This has me very close to where I need to be:
Code:
awk '/nz_migrate table/ {printf $3"|"}/.....migration process started/{if ($1) print $5" "$6} end {printf "\n"}' tony.log Code:
table1|2015-11-29 07:00:09 |
This still gives me the same result:
Code:
awk '/nz_migrate table/ {printf $3"|"}/.....migration process started/{if (length($1)==0) {print "\n";} else {print $5" "$6;}} end {printf "\n"}' tony.log |
There are innumerable ways to fix this. Testing for a condition that doesn't exist is not a good one.
Simplest probably just to force a newline first in the initial printf. Will give you a null line as first line of output, but in need that can be worked around using something like the nl in post #8. Better to use regex rather than a fixed number of blanks in the test too ... output formats have been known to change without warning. |
You can actually check for the absence of a pattern, see http://www.gnu.org/software/gawk/man...ssion-Patterns or use the !~ operator.
But it might be better to work with next statements in the actions - that is, when you are done with the processing of /migrate table/ lines, next; when you are done with the proceesing of /migration process/ lines, next, and then print a newline. Depends on the overall logic of the program. |
Code:
awk '/nz_migrate/{printf (x?"\n":"")$3"|";x=1}/migration process\s*started/{print $5,$6;x=0}' |
I like that too grail, but difficult to explain to someone not well versed.
|
syg00, you are correct. I am not terribly well versed on awk programming. However, I sincerely appreciate everyone's comments on my issue. Grails solution provides what I need however I submitted only two search conditions for my scripts as I thought it would a good jumping off point for the several other strings I need to search for also:
Code:
awk ' Thanks! |
All times are GMT -5. The time now is 02:13 AM. |