mysql - Shell script extract data from a file -


i started programming in shell script , have file looks this:

stuff doesn't matter doesn't matter matter  *line 35*school: xxxxx -> name: xxxxx age: xxx description: xxxxxxxxxx school: yyyyy -> name: yyyyy age: yyy description: yyyyyyyyyy school: zzzzz -> name: zzzzz age: zzz description: zzzzzzzzzz school: aaaaa -> name: aaaaa age: aaa description: aaaaaaaaaa 6 lines of stuff after important information 

my main goal in migrate students mysql database, code this:

nstudents=(( $(wc -l file | cut -d ' ' -f1) - 41) #41 comes 35+6 i=1 while [ $i != $nstudents ] $school=[i don't know how extract school number $i] $name=[i don't know how extract name number $i] $age=[i don't know how extract age number $i] $desc=[i don't know how extract description number $i] mysql #upload $i= (( $i + 1 )) done 

i know need use sed or it, can't figure out how. in advance.

just point in shell-ish solution problem basd on best guess @ data looks like, try this

cat inputfile stuff doesn't matter doesn't matter matter  *line 35*school: xxxxx -> name: xxxxx age: xxx description: xxxxxxxxxx school: yyyyy -> name: yyyyy age: yyy description: yyyyyyyyyy school: zzzzz -> name: zzzzz age: zzz description: zzzzzzzzzz school: aaaaa -> name: aaaaa age: aaa description: aaaaaaaaaa 6 lines of stuff after important information  awk -f: '/school/{     gsub(/ -> /, ""); sub(/school/,""); sub(/name/,"")     sub(/age/,""); sub(/description/,"")     printf("insert mytable values (\"%s\", \"%s\", \"%s\", \"%s\")\n",  $2, $3, $4, $5)      }' inputfile 

output

insert mytable values (" xxxxx", " xxxxx ", " xxx ", " xxxxxxxxxx") insert mytable values (" yyyyy", " yyyyy ", " yyy ", " yyyyyyyyyy") insert mytable values (" zzzzz", " zzzzz ", " zzz ", " zzzzzzzzzz") insert mytable values (" aaaaa", " aaaaa ", " aaa ", " aaaaaaaaaa") 

if this, @ grymoire awk tutorial

ihth


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -