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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie - get fields from multiple lines to 1 line 1

Status
Not open for further replies.

dbern

MIS
Apr 19, 2006
3
US
I have a number of log files that look like the folllowing:
======
Load started at:14-Mar-06 23:21:15 EST

Tablename: accounts

Statistics

number of records read: 10117683
number of bad records: 2
number of discarded records: 2
-------------------------------------------------
number of records loaded: 10117681

Elapsed Time (sec): 122.0
================

If I input the file above (along with other similiar ones), the output I want (print) is:

accounts 10117683 2 2 10117681 122.0
customers 78952 0 0 78592 18.5

I can pull out the fields with awk, but don't know how to get them all on one line for each file.
This is on Linux. If you have non-awk ideas that would be appreciated.
 
If you know how to get fields, you can put the fields in your own variables.

Then on reading the line with "Elapsed Time", after you store that last variable, print the variables in any format you prefer with printf

example:

Code:
awk '/Tablename:/ {tabnam=$NF}
/number of records/ {numrec=$NF}
/Elapsed Time (sec):/ {elasec=$NF; printf "%-10.10s %10d %5.1f\n", tabnam, numrec, elasec}' /path/to/file

HTH,

p5wizard
 
I'm missing something on the syntax. Each of the statements below work individually (I put in the variable).


awk '/Tablename:/ {tabname=$2; print tabname}' file

awk '/number of records read:/ {numrec=$5; print numrec}' file

awk '/Elapsed Time/ {elasec=$4; print elasec}' file

But when I try to put it together, I get no output:

awk '/Tablename:/ {tabnam=$2}
/number of records/ {numrec=$5}
/Elapsed Time (sec):/ {elasec=$4; printf "%-10.10s %10d %5.1f\n", tabnam,numrec,elasec}' file
 
/Elapsed Time [!]\[/!](sec[!]\[/!]):/

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for the great posts. I've got it now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top