File formatting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File formatting
# 1  
Old 06-19-2012
File formatting

Hi,

I have a file which contains data in this format
Code:
# User@Host: abc @  [3.210.193.65]  Id: 0000000
# Query_time: 0.000070  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
SET timestamp=00000000;
SELECT @@version, @@version_comment;
# User@Host: abcd @  [3.210.193.65]  Id: 00000000 
# Query_time: 0.000228  Lock_time: 0.000070 Rows_sent: 40  Rows_examined: 40
SET timestamp=00000000;
SELECT * FROM information_schema.CHARACTER_SETS;
# User@Host: abcde @  [3.210.193.65]  Id: 000000000
# Query_time: 0.000002  Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 0
SET timestamp=00000000;
SET NAMES 'utf8';
# User@Host: abcdef @  [3.210.193.76]  Id: 0000000
# Query_time: 0.000022  Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 0
SET timestamp=00000000;
SET NAMES 'utf8';

Want to take values which contains select queries only.

Output file should be like this
Code:
 
# User@Host: abc @  [3.210.193.65]  Id: 0000000
SELECT @@version, @@version_comment;

# User@Host: abcd @  [3.210.193.65]  Id: 00000000
SELECT * FROM information_schema.CHARACTER_SETS;

Please suggest.Smilie

---------- Post updated at 09:37 PM ---------- Previous update was at 09:36 PM ----------

the input file contains many other entries like this.Pasted only a part of that file.

Moderator's Comments:
Mod Comment Please use next time code tags for your code and data

Last edited by vbe; 06-19-2012 at 01:32 PM.. Reason: code tags, rm color, corrected typos
# 2  
Old 06-19-2012
Code:
awk '/SELECT/ { print ""; print UH; print; UH=""; next}; /#/ && /@/ { UH=$0 }' datafile

# User@Host: abc @  [3.210.193.65]  Id: 0000000
SELECT @@version, @@version_comment;

# User@Host: abcd @  [3.210.193.65]  Id: 00000000
SELECT * FROM information_schema.CHARACTER_SETS;

$

# 3  
Old 06-19-2012
Hi,

actually my input files contains some insert,update and delete queries also. I want them also in the same format as select.

But when i add insert,update and delete in that i am getting error.

please suggest.
# 4  
Old 06-19-2012
What exactly did you do, and what error did you get? Show details.
# 5  
Old 06-20-2012
hi,

I am getting this error when trying to find the insert,delete and update qureries along with the select queries.
Code:
sudo awk '/SELECT/INSERT/UPDATE/DELETE { print ""; print UH; print; UH=""; next}; /#/ && /@/ { UH=$0 }' test1_file_new1.txt >> test77.txt

awk: (FILENAME=test1_file_new1.txt FNR=1) fatal: division by zero attempted

Please suggest.
Smilie

Last edited by Franklin52; 06-20-2012 at 04:43 AM.. Reason: Please use code tags for data and code samples
# 6  
Old 06-20-2012
Code:
sudo awk '/SELECT/INSERT/UPDATE/DELETE

change to with and re-try
Code:
/(SELECT|INSERT|UPDATE|DELETE)/

# 7  
Old 06-20-2012
Modified the script in this way and it worked.
Code:
awk '/SELECT/||/UPDATE/||/INSER/||/DELETE/ { print ""; print UH; print; UH=""; next}; /#/ && /@/ { UH=$0 }' datafile


Last edited by Franklin52; 06-20-2012 at 06:04 AM.. Reason: Please use code tags for data and code samples
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Formatting data in a raw file by using another mapping file

Hi All, i have a requirement where i need to format the input RAW file ( which is CSV) by using another mapping file(also CSV file). basically i am getting feed file with dynamic headers by using mapping file (in that target field is mapped with source filed) i have to convert the raw file into... (6 Replies)
Discussion started by: ravi4informatic
6 Replies

2. Shell Programming and Scripting

Formatting file data to another file (control character related)

I have to write a program to read data from files and then format into another file. However, I face a strange problem related to control character that I can't understand and solve. The source file is compose of many lines with such format: T_NAME|P_NAME|P_CODE|DOCUMENT_PATH|REG_DATE ... (3 Replies)
Discussion started by: hk6279
3 Replies

3. Shell Programming and Scripting

Need help in formatting a file.

Hi, I have a file in the following format. cat input name: abcd_ef_1 234:343:343 343:234:343 name : abcdef_2 2343:2343:3434: w3243:wr43:2343 2343:2343:23343:3432 2343:34234:2343 name: 3432_343_3 23432:2343 2343:23432:32432 23432:23432:3432432 As you can see... (2 Replies)
Discussion started by: jpkumar10
2 Replies

4. Shell Programming and Scripting

File Formatting

Hi, I have requirement to format the file.My input file tab(\t) saperated. File format is:- 93 WARNING Sat Mar 17 20:31:59 2012 Sequential_File_0,0: Missing record delimiter "\r\n", saw EOF instead 94 WARNING Sat Mar 17 20:31:59 2012 Sequential_File_0,0: Import... (4 Replies)
Discussion started by: prasson_ibm
4 Replies

5. Shell Programming and Scripting

File formatting

I need to count the number of lines between two sets of pattern in a file and delete those lines from that file e.g From jyotiv@yahoo.com test test2 test3 test4 test5 test6 From Jyotiv@yahoo.com So count lines from test to test6 and delete it from the start of file till next From... (1 Reply)
Discussion started by: jyotiv
1 Replies

6. Shell Programming and Scripting

File Formatting

Hi, Need to delete all the records prior to pattern (INSERT/UPDATE/DELETE). If ' is available, then need to retain it. Input ====================== l_s := ' INSERT INTO TEST' l_P PD := ' UPDATE INTO TEST' l_D := ' DELETE INTO TEST' This is test Output ======================... (1 Reply)
Discussion started by: saurabhbaisakhi
1 Replies

7. Shell Programming and Scripting

File Formatting

Hi, Need to delete all the records prior to pattern (INSERT/UPDATE/DELETE). If ' is available, then need to retain it. Input ====================== l_s := ' INSERT INTO TEST' l_P PD := ' UPDATE INTO TEST' l_D := ' DELETE INTO TEST' This is test Input ======================... (1 Reply)
Discussion started by: saurabhbaisakhi
1 Replies

8. UNIX for Dummies Questions & Answers

Formatting a file.

I want to format a file to limit record length = 100, in each line. Any idea how i can do this? (1 Reply)
Discussion started by: abhilasha
1 Replies

9. Shell Programming and Scripting

Help with formatting of file.

I have a file with following file format - DMCRH|||83000171|||14022008||0430|||8956612.23|J|||3571235|1378452|23468|6894|9234| DMCRH|||83000215|||15092007||0480|||121.33|J|||LineID003|RefNumSP003|RefNumMem003|0004|0003| What i need done is - 1. Cut the firt four digits of the date (eg 1402... (3 Replies)
Discussion started by: divz
3 Replies

10. Shell Programming and Scripting

Formatting a file

Hi All, I have been trying to format a file using sed. I can't seem to get the syntax right. I want to append the next line delemited by a comma or a comma and double quotes. Here is an example of the file I'm tring to format: Before formatting: 00324 03A0312 BRI-u24 0000324 01 H-12... (4 Replies)
Discussion started by: cstovall
4 Replies
Login or Register to Ask a Question