---
title: "Insert into mysql DB from text files"
description: "Bash script using file descriptors, sed, awk, and MySQL CLI to parse words and meanings from text files and batch-insert."
url: "https://h3manth.com/scribe/insert-mysql-db-text-files/"
canonical_url: "https://h3manth.com/scribe/insert-mysql-db-text-files/"
markdown_url: "https://h3manth.com/scribe/blog/insert-mysql-db-text-files.md"
publishDate: "2010-03-05T00:00:00.000Z"
author: "Hemanth HM"
category: "web"
tags: ["bash","mysql","linux"]
word_count: 116
reading_time_minutes: 1
x_markdown_tokens: 290
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Insert into mysql DB from text files

> Bash script using file descriptors, sed, awk, and MySQL CLI to parse words and meanings from text files and batch-insert.

```bash
#!/bin/bash
#==================================================================
#          FILE:  insertDb.sh
#         USAGE:  ./insert.sh
#   DESCRIPTION:  Illustration of inserting data from text to DB
#        AUTHOR:  Hemanth H.M, hemanth.hm@gmail.com
#       VERSION:  1.0
#       CREATED:  03/06/2010 12:30:36 AM IST
#==================================================================
# Example scenario: assume a list with words and meanings with space
# as IFS, or two files with words in one and meanings in the other.
# sed -e 's/ /#/' list | awk -F "#" '{print $1}' > words
# sed -e 's/ /#/' list | awk -F "#" '{print $2}' > means

while read f1 <&7
do
  read f2 <&8
  mysql -uroot -proot dict <<< "insert into words values ('$f1','$f2');"
  # dict is the table
done 7<words 8<means
```
