Sponsored Content
Top Forums Shell Programming and Scripting Parse log file to insert into database Post 302952235 by Don Cragun on Sunday 16th of August 2015 02:33:17 AM
Old 08-16-2015
To create a pipe symbol (|) separated values file containing data from all of the files in a directory with names ending in .log, you could use the following:
Code:
awk '
BEGIN {	OFS = "|"
}
FNR == 1 {
	d = substr(FILENAME, 1, 10)
}
$2 ~ /^[AP]M$/ && $3 != "CPU" {
	print FNR, d, $1 " " $2, $4, $6, $9
}' *.log

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.

If there are files named 2015-08-14_hostname_cpu.log and 2015-08-15_hostname_cpu.log in a directory where you run the above script and each of those files contained the sample data shown in your 1st post in this thread, it produces the output:
Code:
4|2015-08-14|11:39:17 PM|0.09|0.07|99.75
5|2015-08-14|11:44:17 PM|0.04|0.03|99.92
6|2015-08-14|11:49:17 PM|1.49|0.49|97.96
7|2015-08-14|11:54:17 PM|23.27|0.51|76.14
10|2015-08-14|11:56:12 PM|0.17|0.13|99.69
4|2015-08-15|11:39:17 PM|0.09|0.07|99.75
5|2015-08-15|11:44:17 PM|0.04|0.03|99.92
6|2015-08-15|11:49:17 PM|1.49|0.49|97.96
7|2015-08-15|11:54:17 PM|23.27|0.51|76.14
10|2015-08-15|11:56:12 PM|0.17|0.13|99.69

I will leave it to you to redirect the output from the above script into a file you can use to load your database or pipe the output directly into a mysql statement to load your database.
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to insert data in database based on text file?

Hi....can you guys help me out in this script?? Below is a text file script....called Bukom.txt and it contains these: BUKOM 20060101 2.5 2.6 2.7 2.8 2.9 2.3 2.1 BUKOM 20060102 2.4 2.5 2.6 2.7 2.7 2.6 2.4 BUKOM 20060103 2.1 ... (9 Replies)
Discussion started by: forevercalz
9 Replies

2. Shell Programming and Scripting

How to insert data into MYSql database from a text file

Hi, Need to get help from you guys about this issue. I need to insert data into MySql database from a text file which is located in other server. The text file is something look like below: Date | SubscriberNo | Call Duration 20/7/07 | 123456788 | 20 20/7/07 | 123412344 | 30 The... (4 Replies)
Discussion started by: shirleyeow
4 Replies

3. Shell Programming and Scripting

how to insert data into database by reading it from a text file??

Hi....can you guys help me out in this script?? Below is a text file and it contains these: GEF001 000093625 MKL002510 000001 000000 000000 000000 000000 000000 000001 GEF001 000093625 MKL003604 000001 000000 000000 000000 000000 000000 000001 GEF001 000093625 MKL005675 000001... (4 Replies)
Discussion started by: pallavishetty
4 Replies

4. Shell Programming and Scripting

Shell script to parse a line and insert a word

Hi All, I have a file like this, data1,data2,,,data5,data6. i want to write a shell script to replace data3 with "/example/string". which means my data file should look like this . data1,data2,example/string],,data5,data6. Could you guys help me to get a sed command or any other command... (8 Replies)
Discussion started by: girish.raos
8 Replies

5. Shell Programming and Scripting

To Read a File and Insert a part of the lines into the database

Hi Guys I need to have a shell script which reads a log file and insert a part of each line into the database. Some sample lines in the file are as below. 20091112091359 MED_RQACK : user_data=60173054304,100232120,20091112091359,;ask_status=0;ask_reason=OK;msg_id=20091112091319... (5 Replies)
Discussion started by: Somanadh
5 Replies

6. Web Development

INSERT data to a Database Table from a text file

If you have a text file and if you want to Insert data to your Database Table, You can do it with these queries LOAD DATA LOCAL INFILE '/path/yourTextFile.txt' INTO TABLE yourTableName FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n' (0 Replies)
Discussion started by: sitex
0 Replies

7. Shell Programming and Scripting

Korn shell program to parse CSV text file and insert values into Oracle database

Enclosed is comma separated text file. I need to write a korn shell program that will parse the text file and insert the values into Oracle database. I need to write the korn shell program on Red Hat Enterprise Linux server. Oracle database is 10g. (15 Replies)
Discussion started by: shellguy
15 Replies

8. Shell Programming and Scripting

Parse through ~21,000 Database DDL statements -- Fastest way to perform search, replace and insert

Hello All: We are looking to search through 2000 files with around 21,000 statements where we have to search, replace and insert a pattern based on the following: 1) Parse through the file and check for CREATE MULTISET TABLE or CREATE SET TABLE statements.....and they always end with ON... (5 Replies)
Discussion started by: madhunk
5 Replies

9. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

10. Shell Programming and Scripting

Read latest log files and perform database insert

Hi Experts, I have a situation where I need to write a shell script to continuously monitor a log directory with multiple log files and perform following: 1. Read the latest log file continuously and grep "Success" OR "Failure" 2. As it capture either Success or Failure, it has to perform a... (1 Reply)
Discussion started by: rish_max
1 Replies
MMROFF(1)						      General Commands Manual							 MMROFF(1)

NAME
mmroff - reference preprocessor SYNOPSIS
mmroff [ -x ] groff_arguments DESCRIPTION
mmroff is a simple preprocessor for groff, it is used for expanding references in mm, see groff_mm(7). groff is executed twice, first with -z and -rRef=1 to collect all references and then to do the real processing when the reference file is up to date. -x Just create the reference file. This can be used to refresh the reference file, it isn't always needed to have accurate references and by using this option groff will only be run once. AUTHOR
Jorgen Hagg, Lund, Sweden <jh@axis.se>. FILES
/usr/share/groff/1.19.2/tmac/tmac.m /usr/share/groff/1.19.2/tmac/mm/*.cov /usr/share/groff/1.19.2/tmac/mm/*.MT /usr/share/groff/1.19.2/tmac/mm/locale SEE ALSO
groff_mm(7), groff_mmse(7), groff(1), troff(1), tbl(1), pic(1), eqn(1) Groff Version 1.19.2 25 September 2015 MMROFF(1)
All times are GMT -4. The time now is 02:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy