awk+MySQL


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk+MySQL
# 1  
Old 08-19-2008
awk+MySQL

Hi everyone,
I have a large text file that is output of mysql select, like:
pet:
+--------------+-------------+---------+------+------------+
| name | owner | species | sex | birth |
+--------------+-------------+---------+------+------------+
| Mickey Mouse | Walt Disney | mouse | m | NULL |
| Tom | Walt Disney | cat | m | 1960-03-03 |
| Jery | Walt Disney | mouse | m | NULL |
| Spoky | Trey Parker | fish | NULL | 2001-06-13 |
| Fluffy | Harold | Hamster | f | 1993-02-04 |
.....skip......
Now I wanna to insert the same data into another MySQL table, but I have only this text file output.
I believe it is possible to make this file suitable for using MySQL's 'load data' statement by means either awk or sed, it means suitable format like
Mickey Mouse Walt Disney mouse m \N
Tom Walt Disney cat m 1960-03-03
.......
with tab as a delimiter,ending each line with EOL character and replace each 'NULL' value with '\N'
My knowledge of awk is weak, please any help?
# 2  
Old 08-19-2008
Hi, that's quite easy

Let's suppose that the file is as you told with a header of 4 lines 'pet:\n+--...' and has been put in the file "pet_1.txt"

then a simple small script will do it (not clever, nor optimised, but it works) :

Code:
sed -e '1,4d' -e 's/[ ]*|[ ]*/|/g' -e "s/^|//" -e "s/|$//" pet_1.txt | awk -F "|" '\
{ \
  s = ""; \
  for( i = 1; i <= NF; i++) { \
    if ($i == "NULL") $i = "\\N"; \
    if (s == "" ) s = $i; \
    else s = sprintf("%s %s", s, $i); \
  } \
  print s; }' > new_pet.txt

but beware, there are items here that contain spaces ! don't know the MySQL load format, but you should do somthing for it because it won't load as you want (maybe by enclosing each element with '"' cars thus preceeding the code
Code:
if (s == "") s = $i; \
else  s = sprintf("%s %s", s, $i); \

with that small condition
Code:
p = match($i,"[ ]"); \
if ( p != 0 ) $i = sprintf("\"%s\"", $i); \


Last edited by Locki; 08-19-2008 at 04:09 PM..
# 3  
Old 08-19-2008
awk '{gsub(/[ ]*\|[ ]*/,"\t"); gsub(/NULL/,"\\N")}; {print }' file

P.S. : Use nawk on Solaris.

Last edited by sudhamacs; 08-19-2008 at 04:02 PM..
# 4  
Old 08-19-2008
Thanks Locki! It works great.
You save me a lot of time and give important hints about further learn sed & awk scripting
# 5  
Old 08-19-2008
I think my code produced what you want but not what MySQL is expecting when there blanks within the fields. If MySQL accept compound-strings surrounded by "double quote" cars. In that case, you should use

Code:
sed -e '1,4d' -e 's/[ ]*|[ ]*/|/g' -e "s/^|//" -e "s/|$//" pet_1.txt | awk -F "|" '\
{ \
  s = ""; \
  for( i = 1; i <= NF; i++) { \
    if ($i == "NULL") $i = "\\N"; \
\
    p = match($i,"[ ]"); \
    if ( p != 0 ) $i = sprintf("\"%s\"", $i); \
\
    if (s == "" ) s = $i; \
    else s = sprintf("%s\t%s", s, $i); \
  } \
  print s; }' > new_pet.txt

sudhamacs, clever solution but :
  1. if a field has "NULL" in it (as in "this is a NULL case"), it won't work (I agree, it's rather a stupid case Smilie )
  2. the 4 first lines of the header are still there
  3. your method adds a \t at the begining of the record ... don't know if it is accepted Smilie

Last edited by Locki; 08-19-2008 at 04:26 PM..
# 6  
Old 08-19-2008
If the field contains "NULL" string then u can do this :

awk '{gsub(/\|[ ]*NULL[ ]*/,"|NULL"); gsub(/\|NULL/,"|\\N"); gsub(/[ ]*\|[ ]*/,"\t");}; {print }' file
# 7  
Old 08-19-2008
in that case, you would better use a single sed script lol

Code:
content for "script.sed" :
----------------------------
1,4d
s/[ ]*|[ ]*/|/g
s/|NULL|/|\\N|/g
s/|$//
s/^|//
s/|/    /g

command to issue:
---------------------
sed -f script.sed pet.txt > new_pet.txt

(notice : for the last line of the script it's a tab car, not 3 blanks; writing '\t' won't do it)

Last edited by Locki; 08-19-2008 at 04:58 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk cmd for vlookup in Mysql

Hi, Is there possible to do vlookup in Mysql one table from another table based on one column values and placed the data in same table? if it is possible in mysql itself pls share links for reference. Here is the ex: i need to vlookup the cus.id in table to and place the cus.name in 4th... (3 Replies)
Discussion started by: Shenbaga.d
3 Replies

2. Shell Programming and Scripting

Help: Change date to datetime mysql using awk

#cat file1.log 10.51.61.38;Thu Nov 1 08:06:12 2012;Logout 10.51.62.21;Thu Nov 1 08:06:16 2012;Login output result: 10.51.61.38;2012-11-01 08:06:12;Logout 10.51.62.21;2012-11-01 08:06:16;Login how to write script using awk, need help (1 Reply)
Discussion started by: pillawa
1 Replies

3. UNIX and Linux Applications

MySQL Daemon failed to start - no mysql.sock file

After doing a yum install mysql mysql-server on Fedora 14 I wasn't able to fully install the packages correctly. It installed MySQL 5.1. I was getting the following error when running the: mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)... (3 Replies)
Discussion started by: jastanle84
3 Replies

4. Shell Programming and Scripting

awk and mysql

Helloo, I have worked with some files and so far I got to to point there I have a fileA: Person1_Operation20060611090814 Person4_Operation20060512090811 Person6_Operation20060613090214 Person2_Operation20060115090815 Person9_Operation20060617100814 .. ... ... so I was thinking... (3 Replies)
Discussion started by: amon
3 Replies

5. UNIX for Advanced & Expert Users

mysql would not start: missing mysql.sock

I recently installed mysql-standard-5.0.21-solaris9-sparc-64bit.pkg on a Solaris 9 machine (SunOS 5.9 Generic_118558-19 sun4u sparc SUNW,Ultra-250). The package installation went very smooth, however, starting mysql is a different story. I get the message below everytime I try to start mysql: #... (2 Replies)
Discussion started by: xnightcrawl
2 Replies

6. UNIX for Advanced & Expert Users

MySQL problem >> missing mysql.sock

MySQL on my server is down.... I figured out that the mysqld process isn't running. When I try to run it, it says it can't find mysql.sock Any suggestions? Here's what I can't do: can't be root don't have physical access (do stuff via SSH) reinstall MySQL (need to keep the current MySQL... (8 Replies)
Discussion started by: _hp_
8 Replies
Login or Register to Ask a Question